Variable length template arguments list?

后端 未结 4 1488
情话喂你
情话喂你 2021-02-01 10:03

I remember seing something like this being done:

template 
class X : public ListOfTypenames {};

that is, X inherits from

相关标签:
4条回答
  • 2021-02-01 10:31

    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... {};
    
    0 讨论(0)
  • 2021-02-01 10:32

    Variable number of templates is part of the next C++ standard. However, you can get a taste of it if you're using GCC (from version 4.3). Here's a list of available C++0x features in GCC. You're looking for Variadic Templates.

    By the way, if you need a formal reference on how to achieve the inheritance mechanism as described by Earwicker, it's on the book C++ Templates.

    0 讨论(0)
  • 2021-02-01 10:41

    As others already answered, variadic templates are part of the next standard, but can be emulated in current C++. One convenient tool for this is to use the Boost.MPL library. In your code, you write a single template parameter (let's name it "Typelist"), and the users of your template wrap the typelist in an MPL sequence. Example:

    #include "YourType.h"
    #include "FooBarAndBaz.h"
    #include <boost/mpl/vector.hpp>
    
    YourType<boost::mpl::vector<Foo, Bar, Baz> > FooBarBaz;
    

    In the implementation of "YourType", you can access the elements in Typelist with various metafunctions. For example, at_c<Typelist, N> is the Nth element of the list. As another example, the "X" class in your question could be written with inherit_linearly as:

    //Warning: Untested
    namespace bmpl = boost::mpl;
    template<class Typelist>
    class X : bmpl::inherit_linearly<Typelist, bmpl::inherit<bmpl::_1, bmpl::_2> >::type
    {
    ...
    };
    
    0 讨论(0)
  • 2021-02-01 10:54

    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...

    0 讨论(0)
提交回复
热议问题