For the third part of this article, please refer to the overview section: http://www.cnblogs.com/harrywong/p/4220233.html.
Rvalue reference
If x is any type, then x&& is referred to as an rvalue reference to X (rvalue reference). For a better distinction, the original reference x& is now also referred to as an lvalue reference (lvalue reference).
An rvalue reference is a very similar type of behavior as the original reference x&, but there are some exceptions. One of the most important is that when faced with a method overload resolution, the lvalue favors the old-fashioned lvalue reference, while the right value favors the new Rvalue reference.
void // Lvalue Reference Overloading void // rvalue Reference Overloading // parameter is lvalue, call foo (x&)// parameter is rvalue, call foo (x&&)
So the main point of this is:
Rvalue references allow a method to make a selection at compile time through overload resolution, based on: "Am I called by an lvalue or rvalue?" “
You can overload any method based on this rule, as shown below. However, in most cases, this form of overloading should only appear in the copy constructor and assignment operators in order to implement the move semantics.
x& x::operatorconst// General implementation x& X::operator= (x&& RHS) { // move semantics, exchange this and RHS contents return * this;}
Overloading of a copy constructor that implements an Rvalue-based reference is also similar.
Important : There are still some minor flaws in the fact that the first look is correct, which often happens in C + +. It turns out that in some cases, it is not good enough to simply swap the contents of this and RHS in the implementation of the copy assignment operator above. We will continue this discussion in section fourth, "Enforcing move Semantics".
Note : If you implement
void foo(X&);
Instead of
void foo(X&&);
Then of course there is no change in behavior: it can be called by an foo
lvalue, but it cannot be called by an rvalue.
If you realize
void foo(X const &);
Instead of
void foo(X&&);
Still, the behavior remains unchanged and foo
can be called by Lvalue and Rvalue, but it is not possible to differentiate between lvalue and rvalue values. This is only possible if the following implementation is the case
void foo(X&&);
The same, finally, if you realize
void foo(X&&(;
But neither is
void foo(X&);
And
void foo(X const &);
Then, depending on the final version of the c++11, foo
it can be called by an rvalue, but if you try to invoke it with an lvalue, a compiler error will be triggered.
Translation "c++ Rvalue References explained"c++ rvalue Reference detailed PART3: rvalue reference