Re: Overloading vs C++ 0x variadic templates
On Aug 14, 2:24 am, er <er.ci.2...@gmail.com> wrote:
Thanks, very useful.
As it is though, calling g with an rvalue, say g(5), will not work
unless f specifically caters to rvalues.
I'm probably misunderstanding your statement above. The way I'm
reading that statement it can be reworded like so:
Calling g with an rvalue, say g(5), won't work, unless it does
work.
Sorry, I'm really not meaning to be obstructive. I'm truly not
understanding your problem domain yet.
So, I'd like to add that
twist to the initial problem. The old fashioned way it would be
template<typename F>
struct foo{
foo(){}
// n = 0
void g(){ this->f(); }
// n = 1
template<typename T0> void g(T0& a){ this->f<T0>( a ); }
template<typename T0> void g(T0 const& a){ this->f<T0
const>( a ); }
// n = 2
template<typename T0,typename T1> void g(T0& a,T1& b){
this->f( a, b );
}
template<typename T0,typename T1> void g(T0& a,T1 const& b){
this->f<T0, T1 const>( a,b );
}
// and <T0 const, T1> and <T0 const, T1 const>
// n = 3,...,N Usually done with a pp macro.
private:
F f;
};
The point, here, is that f need not be overloaded on lvalue/const.
lvalue is enough:
In the above formulation, g(5) can be called (binds to the const&
overload). Indeed, it is this 2^n "explosion" that perfect forwarding
is intended to avoid. Reference:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm#s3
template<typename T0> f(T0& );
template<typename T0,typename T1> f(T0&,T1&);
etc.
Now, how would Howard's code have to be modified to achieve this with
C
++0x?
If you would like to disallow rvalue arguments to g() you can do this:
template<typename F>
struct foo{
foo(){}
template<typename... Args>
void g(Args&... args)
{ this->f(args...); }
private:
F f;
};
But this isn't equivalent to your T0&, const T1& implementation as it
disallows g(5).
-Howard