Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++0x -- fun(Args&...) and fun(Args const&...)

Reply
Thread Tools

C++0x -- fun(Args&...) and fun(Args const&...)

 
 
er
Guest
Posts: n/a
 
      12-20-2010
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&...){}

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?!
}
{
h(a, a);
std::cout << std::endl;
// clvalue, clvalue // OK
}

return 0;

}
 
Reply With Quote
 
 
 
 
er
Guest
Posts: n/a
 
      12-20-2010
PS: GCC 4.4

 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      12-20-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments