How is operator+ called with rvalue reference?

Discussion in 'C++' started by Nephi Immortal, Sep 9, 2011.

  1. I read rvalue reference article. Here is the website:
    http://blogs.msdn.com/b/vcblog/arch...-references-c-0x-features-in-vc10-part-2.aspx.
    I wrote my code below. I can only see that rvalue reference is used
    in the first parameter of the operator+ function.

    int main()
    {
    string s1( "01" );
    string s2( "23" );

    string s3 = s1 + s2 + "AB";

    return 0;
    }

    After s3 executed, first function is called:

    basic_string<_Elem, _Traits, _Alloc> operator+(
    const basic_string<_Elem, _Traits, _Alloc>& _Left,
    const basic_string<_Elem, _Traits, _Alloc>& _Right)

    Then, it moves in the right direction and second function is called:

    basic_string<_Elem, _Traits, _Alloc> operator+(
    basic_string<_Elem, _Traits, _Alloc>&& _Left,
    const _Elem *_Right)

    Until move assignment operator is reached.

    Can you please be kind to demonstrate your code? How can two
    functions be called like below?

    basic_string<_Elem, _Traits, _Alloc> operator+(
    const _Elem *_Left,
    basic_string<_Elem, _Traits, _Alloc>&& _Right)

    and

    basic_string<_Elem, _Traits, _Alloc> operator+(
    basic_string<_Elem, _Traits, _Alloc>&& _Left,
    basic_string<_Elem, _Traits, _Alloc>&& _Right)
    Nephi Immortal, Sep 9, 2011
    #1
    1. Advertising

  2. Nephi Immortal

    SG Guest

    On 9 Sep., 04:45, Nephi Immortal wrote:
    > I read rvalue reference article.  Here is the website:
    > http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c....


    Unfortunately, this article is not up-to-date with respect to the
    current rvalue reference rules. But the same author also made a nice
    video tutorial on this topic not too long ago:

    http://channel9.msdn.com/Shows/Goin...-Lavavej-Standard-Template-Library-STL-9-of-n

    I can also recomment Scott Meyers' recent talk on this topic which is
    publicly available on the net.

    > I wrote my code below.  I can only see that rvalue reference is used
    > in the first parameter of the operator+ function.
    >
    > int main()
    > {
    >     string s1( "01" );
    >     string s2( "23" );
    >     string s3 = s1 + s2 + "AB";
    >     return 0;
    > }
    >
    > After s3 executed, first function is called:
    >
    > string operator+(
    >         const string& _Left,
    >         const string& _Right)
    >
    > Then, it moves in the right direction and second function is called:
    >
    > string operator+(
    >         string&& _Left,
    >         const char *_Right)
    >
    > Until move assignment operator is reached.


    That seems about right.

    > Can you please be kind to demonstrate your code?  
    > How can two functions be called like below?
    >
    > string operator+(
    >         const char *_Left,
    >         string&& _Right)
    >
    > and
    >
    > string operator+(
    >         string&& _Left,
    >         string&& _Right)


    OK, here it is:

    ----------8<----------

    string world();

    int main() {
    "Hello" + world(); // invokes operator+(const char*,string&&)
    world() + world(); // invokes operator+(string&&,string&&)
    return 0;
    }

    ----------8<----------

    Cheers!
    SG
    SG, Sep 9, 2011
    #2
    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. Gonzalo Aguirre

    rvalue / lvalue operator[]

    Gonzalo Aguirre, Jan 2, 2004, in forum: C++
    Replies:
    4
    Views:
    438
    Ron Natalie
    Jan 2, 2004
  2. Mark Stijnman
    Replies:
    2
    Views:
    467
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Apr 22, 2005
  3. Kavya
    Replies:
    9
    Views:
    497
    Dik T. Winter
    Oct 28, 2006
  4. Replies:
    4
    Views:
    314
    James Kanze
    Jun 27, 2008
  5. Juha Nieminen
    Replies:
    13
    Views:
    600
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page