C++0x 将参数传递给可变参数模板函数
通过引用获取可变数量的参数是什么意思?这是否意味着每个参数都是通过引用传递的?
例如,考虑以下函数,它们对其每个参数执行一些处理:
void f() // base case for recursion
{
}
template <typename Head, typename ... Tail>
void f(Head& head, Tail&... tail)
{
// Do processing on head
process(head);
// Now recurse on rest of arguments
f(tail...);
}
现在,如果我有:
int a, b, c;
...
f(a, b, c);
这会导致 f(int&, int&, int&)、f(int&, int&) 和最后 f 的实例化(整数&)?
如果我将 f() 的第二个参数更改为“Tail...”而不是“Tail&...”怎么样?现在实例化将是 f(int&, int, int)、f(int&, int) 和最后的 f(int&),这意味着例如 'c' 将通过前两次调用进行复制,最后一次调用将被复制修改副本而不是原始文件?
有人可以指出一个很好的参考来解释可变参数模板到底是如何工作的吗?
What does it mean to take a variable number of arguments by reference? Does it mean that each of the arguments are passed by reference?
Consider for example the following functions which performs some processing on each its arguments:
void f() // base case for recursion
{
}
template <typename Head, typename ... Tail>
void f(Head& head, Tail&... tail)
{
// Do processing on head
process(head);
// Now recurse on rest of arguments
f(tail...);
}
Now if I have:
int a, b, c;
...
f(a, b, c);
Will this result in instantiations of f(int&, int&, int&), f(int&, int&), and finally f(int&)?
How about if I change the second parameter of f() to be "Tail..." instead of "Tail&...". Will the instantiations now be f(int&, int, int), f(int&, int), and finally f(int&), meaning that e.g. 'c' will be copied through the first two calls and the last call will be modifying a copy instead of the original?
Could someone point to a good reference that explains how exactly variadic templates work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的直觉是正确的,您可以阅读当前 ISO 规范草案(尚未最终确定),您可以使用 GCC > 测试它们4.3
I think your intuition is correct, you can read all the details from the current draft ISO spec (it is not finalized yet) and you can test them out with GCC > 4.3