Variable length template arguments list?

℡╲_俬逩灬. 提交于 2019-12-03 05:15:30

问题


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?


回答1:


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




回答2:


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... {};



回答3:


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
{
...
};



回答4:


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.



来源:https://stackoverflow.com/questions/1511532/variable-length-template-arguments-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!