C/C ++: essence of left value (2) which expressions are left values and right values? -- Right Expression

Source: Internet
Author: User

 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.