可变参数模板
我看过很多介绍可变参数模板的链接。但我从未见过任何可编译的示例来演示这种方法。
有人可以给我提供一些可以找到此类可编译示例的链接吗?
I have seen a lot of links introducing the variadic templates. But I have never seen any compilable example that demonstrates this approach.
Could someone provide me with some links in which such compilable examples can be found?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最简单的示例之一是以下
max
实现,它甚至没有在类型上进行模板化。规范的 printf 实现稍微复杂一些:
One of the simplest possible examples is the following implementation of
max
which isn't even templated on types.Only slightly more complex is the canonical
printf
implementation:可变参数模板是一项 C++0x 功能,主要针对通用库的作者。我不希望在“用户代码”中看到它们。 ,在 C++0x 标准库中,它们在很多地方使用:std::function、std::async、std::reference_wrapper、std::tuple、std::packaged_task...
例如 举个例子,我将向您展示如何针对可变参数模板实现引用包装器:
这并不完全符合标准草案,但它应该只需很少的修改即可编译。它演示了多个 C++0x 功能:
decltype
标准库函数模板declval
创建对象,以便为decltype
构建表达式(GCC 尚未提供此函数模板。您必须自己编写)可变成员模板的目的是将参数转发给
ptr
引用的对象。如果 T 是函数指针类型或具有重载函数调用运算符的类类型,这应该有效。干杯!
s
Variadic templates are a C++0x feature that primarily targets authors of generic libraries. I would not expect to see them in "user code". For example, in the C++0x standard library they are used in a lot of places: std::function, std::async, std::reference_wrapper, std::tuple, std::packaged_task, ...
To give you an example I'll show you how a reference_wrapper might be implemented with respect to variadic templates:
This is not perfectly conforming to the standard draft but it is supposed to be compilable with little modification. It demonstrates multiple C++0x features:
decltype
declval
to create objects for the purpose of building an expression fordecltype
(GCC does not yet offer this function template. You have to write it yourself)The purpose of the variadic member template is to forward arguments to the object referred to by
ptr
. This should work in case T is a function pointer type or a class type with overloaded function call operator.cheers!
s
可变参数模板的一个非常简单的例子:
假设我们想要一个函数,它接受可变数量的参数并将它们全部打印出来。例如:
为了使该功能正常工作,我们基本上需要两个函数:
第一个函数,它接受可变数量的参数:
一些解释:
1.)用省略号表示的参数包(... ),出现在参数列表中。
这意味着,这些都是相同的。
因此,您不必担心其中空白的正确位置。不过,IMO 最多应使用一个空格作为最佳实践。
2.) 包扩展:后跟省略号的模式。
3.) 参数包接受零个或多个模板参数。因此,
print(T t, Args...args)
接受一个或多个 参数。一旦你理解了这一点,我们就可以将调用流程可视化如下:
翻译为:
哪个调用
哪个调用
哪个调用
哪个调用
如果你仔细遵循了第 3 点,你一定已经意识到 print(T t, Args. ..args) 无法处理 0 级调用。
因此,我们需要另一个具有相同名称的函数来赶上任何级别 >=0 的情况。
第二个,一个抓取调用堆栈顶部调用的函数:
在级别 0 捕获:
或者,在级别 1 捕获:
或者,在级别 2 捕获:
所以关于...
这些中的任何一个都可以。希望这对您下次编写此类函数或类时有所帮助。
A very simple example of variadic template:
Suppose we want to have a function which takes variable number of arguments and prints them all. For ex:
For that functionality to work, we would basically require two functions:
First one, a function which takes variable number of arguments:
Some explanation:
1.) Parameter Packs denoted by ellipsis(...), that appear in parameter list.
That means, these all are same.
So, you don't have to worry about the correct position of the whitespace in there. Though, IMO at most one whitespace should be used as a best practice.
2.) Pack Expansion: A pattern followed by an ellipsis.
3.) Parameter pack accepts zero or more template args. So,
print(T t, Args... args)
accepts one or more args.Once you understand that, we can visualize the call flow as below:
translates into:
which calls
which calls
which calls
which calls
If you have followed the Point#3 carefully, you must have realized that
print(T t, Args... args)
can't handle call at Level 0.So we need another function here with same name to catch up at any level >=0.
Second one, a function to grab the call at the top of call stack:
Catch at level 0:
or, Catch at level 1:
or, Catch at level 2:
so on...
Any of these would work. Hope this helps you next time you go about writing such function or class.
这是我在博客上发布的可变参数模板的示例:
http://thenewcpp.wordpress.com/2011/ 11/23/variadic-templates-part-1-2/
它编译。它演示了从一组类型中找到最大的类型。
This is an example of variadic templates that I put up on my blog:
http://thenewcpp.wordpress.com/2011/11/23/variadic-templates-part-1-2/
It compiles. It demonstrates finding the largest type from a group of types.
可变参数模板是 C++0x 标准的一部分,但尚未正式发布。 gcc 从 4.3 版开始支持它们,但您需要通过添加编译器开关 -std=c++0x 来启用对 C++0x 的支持。
Variadic templates are part of the C++0x standard which is not yet officially released. They are supported by gcc since version 4.3, but you need to enable support for C++0x by adding the compiler switch -std=c++0x.
在 C++11 之前,只能使用固定数量的参数创建模板。
第一个具有一个参数的函数模板。
具有两个参数的函数的第二个模板。
...
即从C++11开始你只能编写一个模板,编译器将自行生成所需的函数。
好例子
http://eli.thegreenplace.net/2014/variadic-templates-in- c/
Before C++11, you can create template only with the fixed count of parameters.
Firts template for the function with one parameter.
Second template for the function with two parameters.
... i.e.
Since C++11 you can write only one template, compiler will generate required function itself.
Good example
http://eli.thegreenplace.net/2014/variadic-templates-in-c/
另一种语法:扩展,例如,
因此:
现在实际上是:
another syntax: expanding, e.g.
hence:
is now actually: