Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the double ampersand return type mean? [duplicate]

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.

like image 364
Vladp Avatar asked Aug 10 '14 15:08

Vladp


2 Answers

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
like image 91
R Sahu Avatar answered Oct 04 '22 21:10

R Sahu


In that case, the move function is using the so-called rvalue references - relatively new C++ feature. It is nicely explained in this article.

like image 38
DejanLekic Avatar answered Oct 04 '22 21:10

DejanLekic