Essence of C/C ++ left
(2) which expressions are left and right?
4. suffix expression
In the left valuability of an expression, the suffix expression is complicated. There are many types of suffix expressions. Here we discuss suffix expressions in the form of e1.e2 and E1-> E2.
4.1 suffix expression in the form of e1.e2
If E2 is a static data member or a referenced data member, the e1.e2 result is a left value regardless of the Left value of E1. If E2 is a non-static non-referenced data member, both the C and C ++ standards stipulate that if E1 is the left value, e1.e2 is also the left value. When e1 is the right value, in principle, part of the right value object should also be a right value. Therefore, in C, e1.e2 is the right value regardless of the Left value of E2; what are the results in C ++? It should be justified as the right value, but it is surprising that neither C ++ 98 nor C ++ 2003 has specified this! Therefore, in C ++ 98 and C ++ 2003, no matter whether the compiler regards this situation as the left or right value, it does not violate the standard. This is obviously a vulnerability. During the development of C ++'s New Standard C ++ 11, wg21 experts
Reports acknowledged this and corrected the result to a prvalue (pure rvalue, pure right value) in C ++ 11 ).
Struct
{
A (Int & I): R (I ){}
Static int K;
Int & R;
Int J;
};
Int A: K = 10;
A foo (Int & R) {A (r); return ;}
......
Int I = 20;
A A (I );
A. k = 30; // static member, left Value
A. r = 40; // reference Member, left Value
A. J = 50; // A is the left value, so a. j is the left value.
Foo (I). k = 60; // static member, left value, although Foo (I) returns a right value
Foo (I). R = 70; // reference Member, left value, although Foo (I) returns a right value
Foo (I). J = 80; // The left and right values of Foo (I). J are valid because no standards are specified.
E2 is a function member. If E2 is a static member function, the result is left regardless of the Left value of E1. If E2 is a non-static member function, as described in section 3.1, because the non-static member function is not the left value, the e1.e2 result is the right value regardless of the Left value of E1.
4.2 E1-> E2 format suffix expression
Because the right address in C cannot be obtained, E1 in C always points to the left pointer, so the result of E1-> E2 extension expression in c is always the left value; however, C ++ is quite different. The E1-> E2 suffix expression in c ++ is converted to the equivalent form (* E1 ). (E2), so the left value of E1-> E2 is the same as that of e1.e2.