Essence of C/C ++ left
(2) which expressions are left and right?
2. Right Expression
C defines the right value as the expression value, and the result of all complete expressions in C is the right value. Full expression refers to an expression that is not part of another expression or declaration. Conditional expressions and comma expressions do not generate left values. However, many people think that the intermediate results or temporary objects produced by subexpression calculation are all right values, but in fact, they are not necessarily the right value. For example:
Int A [5] = {1, 2, 3, 4, 5 };
Int * P =;
* P = 2;/* */
A [1] = 3;/* B */
* P;/* C */
A [1];/* D */
* P and a [1] In A and B are both subexpressions with built-in value assignment operators. Although they are all intermediate results, they all belong to the left value expression. Only when they are complete expressions, as shown in * P and a [1] In C and D, the final left value conversion is performed to make the result all right values. The essence of this phenomenon is that C converts all the results of the complete expression from the left to the right, this behavior can be understood as a process of full evaluation (full evaluation is not a standard term ).
However, the complete expression of c ++ does not require full evaluation. whether to retain the left value depends on your needs. What is this "need? In fact, it is mandatory for certain operators in C ++, such as built-in assignment operators, pre-increment and pre-subtraction operators. The results of such operators are forcibly set to the left value. For Conditional operators, only when both the second and third expressions are left and the type is the same can the result be left. There is no such reason. It is just a rule.
Is the right value an object? Because the left value requires an object, the misunderstanding is that the right value is not an object. But what if the function returns a structure?
Struct s {int ;};
Struct s fun (void) {struct s;...; return s}
Because the pod structure is a clustering, not a scalar, the pod structure is not a value, and the scalar is regarded as a value. AboveCodeIn, fun returns a temporary object of the pod structure S, and is a right value, that is, both C and C ++ have the right value object ,. Therefore, the right value is not an object, not an object.