Variable length template arguments list?

后端 未结 4 1487
情话喂你
情话喂你 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: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 
    
    YourType > FooBarBaz;
    

    In the implementation of "YourType", you can access the elements in Typelist with various metafunctions. For example, at_c 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 X : bmpl::inherit_linearly >::type
    {
    ...
    };
    

提交回复
热议问题