C++ 可变长度模板参数列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1511532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:13:51  来源:igfitidea点击:

Variable length template arguments list?

c++templatesc++11variable-length

提问by sold

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.

也就是说,X 继承自作为模板参数传递的类型名的可变长度列表。当然,这段代码是假设的。

I can't find any reference for this, though. Is it possible? Is it C++0x?

不过,我找不到任何参考资料。是否可以?是 C++0x 吗?

回答by Daniel Earwicker

You can do it in current C++. You give the template a "large enough" number of parameters, and you give them defaults:

您可以在当前的 C++ 中做到这一点。您为模板提供“足够大”数量的参数,并为它们提供默认值:

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.

注意在基类中,您如何继承 X 但您错过了第一个参数。所以他们都沿着一个地方滑动。最终它们都将是默认值,并且特化将开始,它不继承任何东西,从而终止递归。

Update:just had a strange feeling I'd posted something like this before, and guess what...

更新:只是有一种奇怪的感觉我以前发布过这样的东西,你猜怎么着......

回答by Steve Guidi

Sounds like you are referring to C++0x Variadic Templates. You can also achieve the same effect using Alexandrescu's TypeListconstruct from Loki.

听起来您指的是 C++0x Variadic Templates。您还可以使用Loki 的Alexandrescu 的TypeList构造来实现相同的效果。

I believe the variadic template syntax in question would look like the following.

我相信有问题的可变参数模板语法如下所示。

template <typename...T>
class X : public T... {};

回答by éric Malenfant

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

正如其他人已经回答的那样,可变参数模板是下一个标准的一部分,但可以在当前的 C++ 中进行模拟。一种方便的工具是使用Boost.MPL库。在您的代码中,您编写了一个模板参数(让我们将其命名为“Typelist”),您的模板的用户将类型列表包装在一个 MPL 序列中。例子:

#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_linearlyas:

在“YourType”的实现中,您可以使用各种元函数访问 Typelist 中的元素。例如,at_c<Typelist, N>N列表的第 th 个元素。再举一个例子,你问题中的“X”类可以写成inherit_linearly

//Warning: Untested
namespace bmpl = boost::mpl;
template<class Typelist>
class X : bmpl::inherit_linearly<Typelist, bmpl::inherit<bmpl::_1, bmpl::_2> >::type
{
...
};

回答by Leandro T. C. Melo

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.

可变数量的模板是下一个 C++ 标准的一部分。但是,如果您使用 GCC(从 4.3 版开始),您可以体验一下。这是GCC 中可用的 C++0x 功能列表。您正在寻找可变参数模板。

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.

顺便说一句,如果您需要有关如何实现 Earwicker 描述的继承机制的正式参考,请参阅C++ Templates一书。