Summary--c++ Primer 5
First, left and right values (P121)
1. Concept
A C + + expression, or an rvalue, or an lvalue value.
These two nouns inherit from the C language, which in C is to help the memory: The left value can be on the left side of the assignment statement, and the right value cannot. in C + +, the difference is not so simple: When an object is used as an rvalue, the value of the object (content) is used, and when the object is used as an lvalue, the object's identity (in-memory position) is used.
An important principle (P470 with one exception): You can use an lvalue instead of an rvalue where you need it, but you cannot take the right value as an lvalue (that is, the position). When an lvalue is used as an rvalue, its content (value) is actually used.
The operator to use for the left value:
1. The assignment operator requires a (very) Lvalue as its left operand, and the resulting result is still a left-hand value.
2. The address character (&) acts on an lvalue operand and returns a pointer to the operand, which is an rvalue .
3. The evaluation result of the subscript operator for the built-in dereference operator , subscript operator, iterator dereference operator, string, and vector are all lvalue values .
4. The increment decrement operator for built-in types and iterators acts on an lvalue operand, and the result of the predecessor version is also an lvalue.
2. Left value is persistent; Right value is short
The Lvalue has a persistent state, and the right value is either a literal constant or a temporary object created during the evaluation of an expression.
Second, rvalue reference (P470)
1. Concept
To support the move operation, the new standard introduces a new reference type-rvalue reference. The so-called rvalue reference is a reference that must be bound to the right value.
Important Property : An rvalue reference can only be bound to an object that will be destroyed (literal constant/temporary object).
Because an rvalue reference can only be bound to a temporary object, you need to:
1. The referenced object will be destroyed
2. The object has no other user
2. Application
for Lvalue references, you cannot bind them to an expression that requires conversion, a literal constant or an expression that returns a right value. An rvalue reference can be bound to such an expression, but you cannot bind an rvalue reference directly to an lvalue.
Functions that return non-reference types, together with arithmetic, relationships, bits, and post increment/decrement operators, generate an rvalue. We cannot bind an lvalue reference to such an expression, but you can bind a const lvalue reference or an rvalue reference to such an expression.
1 intI= the;//i is a variable and the variable is an lvalue2 int&r=i;//correct: R references I3 int&&rr=i;//error: Cannot bind an rvalue reference to a left-hand value4 int&r2=i* the;//Error: I*42 is the right value5 Const int&r3=i* the;//correct: You can bind a const lvalue reference to a right value6 int&&rr2=i* the;//correct: Bind the RR2 to the multiplication result
3. The variable is the left value
A variable can be seen as an expression that has only one operand and no operator. variable expressions are left-hand values . then: You cannot bind an rvalue reference to a variable of an rvalue reference type.
int &&rr1=; // literal constants are right values int &&rr2=rr1; // error: Expression RR1 is a left-hand value
The variable is persistent until it leaves the scope and is destroyed.
C + +--left and right values