[005] reference in C ++-left and right values

Source: Internet
Author: User

In fact, I did not specifically want to study the left and right values of C ++. However, when the study group cited the study, it found that the right value references this big pitfall, the reference to the right value was proposed only after C ++ 11. After reading it, I would like to summarize it.

1. Definitions of left and right values

In different C ++ standards, the left and right values are defined differently.

C ++ 03:

① For an expression, either the left value or the right value;

② The left value is an object (pointer, object instance) or a function (for example, a referenced function is returned. Here, it is very important that it cannot be a left value only when the declaration is not initialized, such as char * P; initialization is blank * P is not the left value, because * P is not an object ). Some right values are also objects (some other right values are macro-defined constants, integer literal, string literal, floating-point literal, etc );

③ Some built-in operators and function calls generate the left value. The pointer type of E. (E * P = new E; then after E calls dereference operator *,) * E is the left value expression. For example, if the function calls Int & F (), the left value is also generated. If F () is called, the left value expression is used.

④ When the returned value is not a referenced function, the object is the right value, and the user-defined operator is a common function. Do these functions return the left value, whether the left value of a parameter is determined by the function parameters and return values;

⑤ Conversion (including explicit it type conversion, forced conversion, etc.) from a temporary variable but the result is not a reference expression is the right value;

⑥ When the left value appears where the right value is needed, the left value is converted to the right value;

7. the right value must be of the complete class definition or void type. In addition, the left value can be of a non-complete defined type;

The left value must be used to modify the object. Of course, there is also a special case where the right-value object modifies itself by calling its own member functions.

We can see that in the C ++ 03 standard, the left and right values are not clearly defined, but the expressions are not left or right, the function returns a reference to the left value expression, but returns a non-Reference to the right value expression. There are also a series of methods to determine whether it is the left value or the right value.

So what is the standard definition of the left and right values? Let's take a look at the definition of the left and right values in the latest C ++ 11 standard:

C ++ 11:

1. More popular words:

Both the left and right values are for expressions. The left value is a persistent object that exists after the expression ends. the right value is a temporary object that no longer exists at the end of the expression.

A convenient way to distinguish between the left and right values is to see if an address can be obtained for the expression. If yes, it is the left value; otherwise, it is the right value.

2. Definition of lvalue and rvalue in C ++ 11:

The so-called "value" is for expressions. It is the classification of expressions in the C ++ 11 standard:

1) lvalue is still the left value in the traditional sense;
2) xvalue (expiring value) is the final value that is about to die.

In fact, it is the value of some expressions that involve the reference of the right value. The original reference for C ++ 11 is: An xvalue is the result of certain kinds of Expressions Involving rvalue references.

For example, the return value of a function with the return type being the right value reference is xvalue.

3) prvalue refers to a temporary object, a sub-object of a temporary object, or a value not assigned to any object. Prvalue is the rvalue in the old standard.

For example, the return value of a function is of the normal type, and the return value is rvalue. For example, int Foo (); is not assigned to any object. Such as 5.3, true.

4) glvalue (generalized value) is called the Generalized Left value, including the traditional left value and xvalue.
5) rvalue (Right Value) is called the generalized right value, because in addition to the traditional right value, it also includes xvalue.

6) The concept of lvalue and prvalue is consistent with that of the left and right values in the traditional sense, but the concept of xvalue is added.

So what is the definition of this xvalue?

Literally, an xvalue is the result of certain kinds of Expressions Involving rvalue references, that is, the values of some expressions that involve the right value reference. For reference to the right value, please refer to the next blog.

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.