Named rvalue references are lvalues. Unnamed rvalue references are rvalues. This is important to understand why the std::move call is necessary in: foo&& r = foo(); foo f = std::move(r);
Take a look at this answer : https://stackoverflow.com/a/5481588/1394283 It explains it very well.
Take a look at this function :
void foo(X&& x)
{
X anotherX = x;
// ...
}
The interesting question is: which overload of X
's copy constructor gets called in the body of foo
? Here, x
is a variable that is declared as an rvalue reference. Therefore, it is quite plausible to expect that x
itself should also bind like an rvalue, that is,
X(X&& rhs);
should be called.
Allowing move sematics to be applied tacitly to something that has a name, as in
X anotherX = x;
// x is still in scope!
would be dangerously confusing and error-prone because the thing from which we just moved, that is, the thing that we just pilfered, is still accessible on subsequent lines of code. But the whole point of move semantics was to apply it only where it "doesn't matter," in the sense that the thing from which we move dies and goes away right after the moving.
That's why the designers of rvalue references have chosen a solution that is a bit more subtle than that:
Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue.
Source : http://thbecker.net/articles/rvalue_references/section_05.html