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

Discussion in 'C++' started by er, Dec 20, 2010.

  1. er

    er Guest

    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;

    }
    er, Dec 20, 2010
    #1
    1. Advertising

  2. er

    er Guest

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

    PS: GCC 4.4
    er, Dec 20, 2010
    #2
    1. Advertising

  3. er

    SG Guest

    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
    SG, Dec 20, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,101
  2. dolphin
    Replies:
    4
    Views:
    315
    Jorgen Grahn
    Aug 25, 2007
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,111
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    886
    fungus
    Oct 31, 2008
Loading...

Share This Page