I remember seing something like this being done:
template <ListOfTypenames>
class X : public ListOfTypenames {};
that is, X inherits from a variable length list of typenames passed as the template arguments. This code is hypothetical, of course.
I can't find any reference for this, though. Is it possible? Is it C++0x?
You can do it in current C++. You give the template a "large enough" number of parameters, and you give them defaults:
class nothing1 {};
class nothing2 {};
class nothing3 {};
template <class T1 = nothing1, class T2 = nothing2, class T3 = nothing3>
class X : public T1, public T2, public T3 {};
Or you can get more sophisticated and use recursion. First you forward-declare the template:
class nothing {};
template <class T1 = nothing, class T2 = nothing, class T3 = nothing>
class X;
Then you specialise for the case where all the parameters are default:
template <>
class X<nothing, nothing, nothing> {};
Then you properly define the general template (which previously you've only forward-declared):
template <class T1, class T2, class T3>
class X : public T1, public X<T2, T3>
Notice how in the base class, you inherit X but you miss the first parameter. So they all slide along one place. Eventually they will all be defaults, and the specialization will kick in, which doesn't inherit anything, thus terminating the recursion.
Update: just had a strange feeling I'd posted something like this before, and guess what...
Sounds like you are referring to C++0x Variadic Templates. You can also achieve the same effect using Alexandrescu's TypeList construct from Loki.
I believe the variadic template syntax in question would look like the following.
template <typename...T>
class X : public T... {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With