开发者

C++0x passing arguments to variadic template functions

What does it mean to take a variable number of arguments by reference? Does it mean that each of the arguments a开发者_运维问答re 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜