|
|
template<typename X,typename Y> void Rvalue_mod(X&& a, Y&& b)
int&& r = 23 ;
a temporary object of type int initialised with 23 is created and the rvalue reference is bound to that object. const int&& r = 23 ;
a temporary object of type const int initialised with 23 is created and the rvalue reference to const is bound to that objectint i = 23 ; int&& r = static_cast<int&&>(i) ;
no temporary object is created; the rvalue reference is bound directly to i
Within (the block scope of) the function the expressions (a) and (b) are lvalues.
??temporary object created |
Is an Rvalue Reference an Rvalue? ... 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. In the example above, the thing that is declared as an rvalue reference has a name, and therefore, it is an lvalue:
Here is an example of something that is declared as an rvalue reference and does not have a name, and is therefore an rvalue:
And here's the rationale behind the design: Allowing move sematics to be applied tacitly to something that has a name, as in
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. Hence the rule, "If it has a name, then it's an lvalue." http://thbecker.net/articles/rvalue_references/section_05.html |