Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ OO design: Inheritance of template parameter

I have an inheritance chain with Base being the base class. I want to be able to write a class template which inherits Base and possible another Base-derived class. I could use virtual inheritance, but I found another solution. I'd like to know if it's common/considerable/legitimate class design:

Write a class template in which the template parameter is the class it derives from, i.e. it has to be Base or a Base-derived class. In the constructor I can use static assert to really make sure the user didn't use any illegal class as the template parameter.

If it works, I won't ever have virtual inheritance problems... the question is, it it ok to do that. I never saw it in other projects, so I want to make sure before I use it.

EDIT: Just to be sure I don't confuse you, here's some code:

class Base
{
};

class Derived : public Base
{
};

template <Class TheBase>
class MyDerived : public TheBase
{
};

Now I can use Base or any Base-derived class, e.g. Derived, as the TheBase parameter.

like image 337
cfa45ca55111016ee9269f0a52e771 Avatar asked Mar 20 '13 17:03

cfa45ca55111016ee9269f0a52e771


People also ask

Can template class inherit?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.

What is a template parameter C++?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

Which is a correct example of template parameters?

Template Parameters: These names are listed after the template keyword in a template declaration. For example, T is the single template parameter specified in our Stack example given earlier. Template Arguments: These entities are substituted for template parameters during specialization.


1 Answers

This is a valid design pattern. It is mixin inheritance, not CRTP. Mixin inheritance provides a way to simulate multiple inheritance safely by the programmer manually linearizing the inheritance hierarchy. The templated classes are the mixins. If you want to extend a class with multiple mixins you have to decide the order of the composition like Big<Friendly<Dog> >. Mixin programming in C++ is described in this Dr Dobb's article. Mixins can be used to implement a static version the GoF Decorator pattern as described here. Mixins play a simlar role in C++ that traits (not C++ traits) play in Scala & SmallTalk.

In CRTP it is the base class that is a template:

template <class Param>
class Base { ... };

class Derived : public Base<Derived> { ... };
like image 186
Daniel Mahler Avatar answered Oct 07 '22 01:10

Daniel Mahler