Tutorial :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 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?


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »