Rvalue reference and move operations are new concepts proposed by C++11, which can reduce the consumption of copy operations. Let's take a brief look at the left and right values.
An lvalue generally refers to an object, or a persistent value, such as the return value of an assignment, subscript operation, dereference, and pre-increment, and so on.
The right value is a short value, such as the evaluation result of an expression, the return value of a function, and a literal.
To differentiate, referring to a generic reference called an lvalue reference, a reference that must be bound to an rvalue is called an rvalue reference, and is represented by a &&. Rvalue references have these two characteristics:
an rvalue reference can only be bound to a temporary object, the object being referenced will be destroyed, and no other user .
This also means that we are free to take over the resources of the referencing object.
Usually a variable can be considered an lvalue, or an object. So how do you get the right value? You can display the conversion to a corresponding rvalue reference, or you can call the standard library function Std::move ().
Move () promises that we no longer use a post-move source , and do not guarantee the state and value of the source after it is moved.
In fact, the move operation steals the control of the object's resources, thus avoiding unnecessary copies.
int 1 ; int&& rr = Std::move (i);
After moving the source object must be destructor, and to ensure that the object is still valid, it can be given a new value, but does not guarantee the value left in the source object after the move.
In fact, simply calling Std::move () does not change the object, and only the other object takes over the resource before the move is really complete.
<<c++ primer>> the concrete implementation and work of std::move in detail in the template operation.
Template<typename t> TypeName remove_reference<t>::type&& Move (t&& T) { return static_cast<typename remove_reference<t>::type&&> (T);}
The parameter of the move is a t&&, and any parameter with a name is actually an lvalue. That is, this parameter can be matched to any type of argument and can be passed to an lvalue or to a right value.
C + + rvalue reference and move operation