Re: C++0x -- fun(Args&...) and fun(Args const&...)
On 20 Dez., 18:55, er wrote:
Hello, I'm hoping someone could explain the part next to the comment
"How so?!" below. Thanks.
#include <iostream>
template<typename...Args> void f(Args&...){}
This ought to be just a simple non-template:
inline void f() {}
template<typename U, typename...Args>
void f(U& u, Args&... args){
std::cout << "lvalue, ";
f(args...);
}
template<typename U, typename...Args>
void f(U const& u, Args&... args){
std::cout << "clvalue, ";
f(args...);
}
template<typename...Args>
void g(Args&...args)
{
f(args...);
}
template<typename...Args>
void h(Args const&...args)
{
f(args...);
}
int main()
{
int a = 1;
int const b = 2;
g(a, a); std::cout << std::endl;
// lvalue, lvalue // OK
g(a, b); std::cout << std::endl;
// lvalue, lvalue // How so?!
Name lookup. You expect the first function template f to use the
second one. But this is not what's happening. If you declare the
second function template before the first one it'll work as expected:
template<typename U, typename...Args>
void f(U const& u, Args&... args);
template<typename U, typename...Args>
void f(U& u, Args&... args){
std::cout << "lvalue, ";
f(args...);
}
template<typename U, typename...Args>
void f(U const& u, Args&... args){
std::cout << "clvalue, ";
f(args...);
}
(tested using g++ 4.5.1)
Cheers!
SG