Search code examples
c++c++11moveampersand

What does the double ampersand return type mean?


I've stumbled upon such syntax as this:

int&& move(int&& x)
{
    return x;
}

which is supposedly how the std::move function is implemented, but I don't quite understand what does the return type (&&) actually means.

I did googled it and failed to fail an answer, could someone please explain this to me?

EDIT:

Most of my confusion comes from the fact the return of a function is already an rvalue so I don't understand what && can change there.. not sure if I make sense or not.


Solution

  • From: http://www.stroustrup.com/C++11FAQ.html#rval

    The && indicates an "rvalue reference". An rvalue reference can bind to an rvalue (but not to an lvalue):

    X a;
    X f();
    X& r1 = a;      // bind r1 to a (an lvalue)
    X& r2 = f();    // error: f() is an rvalue; can't bind
    
    X&& rr1 = f();  // fine: bind rr1 to temporary
    X&& rr2 = a;    // error: bind a is an lvalue