C++: Inherit class from template parameter

后端 未结 4 1097
臣服心动
臣服心动 2021-01-30 14:18

I recently saw the following C++ code-snippet

template 
class A : public B
{
   ...
};

and I am wondering in which setting such

4条回答
  •  青春惊慌失措
    2021-01-30 15:06

    It's often used to realize static polymorphism.

    Use cases are:

    • Policy-based design
    • Curiously recurring template pattern
    • Barton–Nackman trick

    In general you have the benefits from dynamic polymorphism, without the extra runtime costs of virtual functions. But it's only useful if the concrete type can be determined at compile time.

提交回复
热议问题