Objective
In order to understand the new property rvalue reference of C++11, we have to re-recognize the left and right values. At the beginning of the study, the quickest comprehension is words too literally, the right value is the value to the right of the assignment number, and the left value is the value to the left of the assignment number. In high school mathematics, we understand that the lvalue is equivalent to the value on the left side of the equals sign, the right value is equivalent to the value to the right of the equals sign, and when we continue to learn C, the equals sign is no longer called an equal sign, and the face is called an assignment number; So can we understand that in C + +? The answer is partly negative.
If you still understand this now, then please go on ...
What is Lvalue lvalue and rvalue rvalue in C + +?
lvalue lvalue: Data Objects that can be referenced, such as variables, array elements, struct members, references, and dereference pointers, are lvalue values. In the C language, the lvalue initially refers to the entity that appears to the left of the assignment statement, but this is the case before the const is introduced. Now, both regular and const variables can be treated as lvalue because they can be accessed by address. Regular variables are modifiable lvalue values, and const variables are non-modifiable lvalue values. The left value is basically not as much a change from the previous cognition.
rvalue rvalue: literal constants ( except strings enclosed in parentheses, because they represent addresses ), expressions that contain multiple items, and functions that return values ( conditions are not references returned by the function ).
Simple difference between left and right values:
The right value is a temporary variable (explained in detail later), only the temporary address space, the left value has its address space, in other words, the use of the address character & the address of a value, if the address is not available, it is the right value!
For example:
int a = 0;
cout << &a << Endl; Ok! Lvalue
cout << &404; error! Rvalue
For the attributes of the right value mentioned earlier:
1) allow calling member functions.
2) can only be pointed by the const reference.
3) The right value cannot be used as an lvalue, but the left value can be used as the right value
Temp variable
Temporary variables can be matched only if the function parameter is const reference.
When will temporary variables be created?
If the reference parameter is const, the compiler generates a temporary variable in the following two scenarios:
1. The type of the argument is correct, but not an lvalue
2. The type of the argument is incorrect, but can be converted to the correct type
Example:
DoubleCubeConst Double&RA) { returnRA * RA *RA;}DoubleSide =3.0;Double* PD = &side;Double& rd =side;LongEdge =5L;Doublelens[5] = {2.0,5.0,10.0,12.0};DoubleC1 =cube (side);DoubleC2 = Cube (lens[2]);DoubleC3 =Cube (RD);DoubleC4 = Cube (*PD);DoubleC5 = cube (edge);//RA is a temporary variableDoubleC6 = Cube (7.0);//RA is a temporary variableDoubleC7 = cube (side +4.0);//RA is a temporary variable
Parameters side, Lens[2], RD, and *PD are both named, double type data objects, so you do not need to use temporary variables to create references for them.
However, although the Edge has a name but the type is incorrect, just in case the temporary variable is generated 2 "the type of the argument is incorrect , but can be converted to the correct type ," the double reference cannot point to long. The types of parameters 7.0 and side + 4.0 are correct, but the wood has a name and belongs to the right value. In these cases, the compiler will generate a temporary anonymous variable and have the RA point to it. The lifetime of these variables is only during function calls, and the compiler is then arbitrarily deleted.
Why is a temporary variable or rvalue restriction on a const reference reasonable?
Take a look at the following example:
void swapr (intint& B) // SWAPR () Completion number of the interchange function { int temp; = A; = B; = temp;} Long 5 6 ; Swapr (A, b);
Here we use the disprove method, assuming that the call is a wooden error, then the type does not match, so the compiler will create two int temporary variables, initialize them to 3 and 5, and then swap the contents of the temporary variable, but this is only the exchange of two temporary variable values,A and B are not exchanged , This is contrary to the function done by the SWAPR () function.
In summary , if a function that accepts a reference parameter is intended to modify a variable passed as a parameter, creating a temporary variable will block the implementation of this intent. The workaround is to disallow the creation of temporary variables. Therefore, this call is also wrong . if the parameter of a function call is an rvalue or does not match the type of the corresponding const reference parameter, C + + creates an anonymous variable of the correct type, passes the value of the parameter of the function call to the anonymous variable, and lets the argument refer to the variable
Left-and right-hand values in C + + that are overlooked