How to properly use references with variadic templates

Posted by Hippicoder on Stack Overflow See other posts from Stack Overflow or by Hippicoder
Published on 2010-04-11T04:48:45Z Indexed on 2010/04/11 4:53 UTC
Read the original article Hit count: 303

Filed under:
|
|
|
|

I have something like the following code:

   template<typename T1, typename T2, typename T3>
   void inc(T1& t1, T2& t2, T3& t3) { ++t1; ++t2; ++t3; }

   template<typename T1, typename T2>
   void inc(T1& t1, T2& t2) { ++t1; ++t2; }

   template<typename T1>
   void inc(T1& t1) { ++t1; }

I'd like to reimplement it using the proposed variadic templates from the upcoming standard. However all the examples I've seen so far online seem to be printf like examples, the difference here seems to be the use of references. I've come up with the following:

template<typename T>
void inc(T&& t) { ++t; }

template<typename T,typename ... Args>
void inc(T&& t, Args&& ... args) { ++t inc(args...); }

What I'd like to know is:

  • Should I be using r-values instead of references?

  • Possible hints or clues as to how to accomplish what I want correctly.

  • What guarantees does the new proposed standard provide wrt the issue of the recursive function calls, is there some indication that the above variadic version will be as optimal as the original? (should I add inline or some-such?)

© Stack Overflow or respective owner

Related posts about c++

Related posts about variadic