通过引用获取可变数量的参数是什么意思?这是否意味着每个参数都是通过引用传递的?
例如,考虑以下对每个参数执行一些处理的函数:
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(int&) 的实例化吗?
如果我将 f() 的第二个参数更改为“Tail...”而不是“Tail&...”,怎么样?现在实例化是 f(int&, int, int), f(int&, int),最后是 f(int&),这意味着例如“c”将通过前两个调用复制,最后一个调用将修改 a复制而不是原件?
有人可以指出一个很好的参考来解释可变参数模板的工作原理吗?