CPP & Value Category

Source: Internet
Author: User

Objective:

Why do you want to translate this article? The answer is that the Chinese version provided by the official website is simply not able to read, so when the porter. I feel this article is very good, although not so easy to understand, some places a bit obscure. However, as a reference document is still possible. Translated a few paragraphs, because English is not very good, some places may express not in place even is wrong, hoped the big God tells me, I good improvement. Now began to regret that did not learn English well, a lot of things can be understood but will not express, so the translation is relatively slag. Have the ability or suggest to look at English, lest mislead you.
Original link

Value categories (Values category)

Each C + + expression (an operator with its arguments, a literal, a variable name, etc) are characterized by and independent PROPERTIES:A type and a value category. Each expression have some non-reference type, and each expression belongs to exactly one of the three primary value Categor ies.

C++the characteristics of each expression (operation with parameters, literal, variable name) are two independent properties, one is the other is the value. Each expression has a non-reference type and must belong to one of three base value types.

Primary Categories1.lvalue

An lvalue was an expression of that identifies a Non-temporary object or a non-member function.

An lvalue is an expression that is identified as a non-temporary object or a non-member function.

The following expressions is lvalues:
The name of a variable or function in scope, regardless of type, such as Std::cin or Std::endl. Even if the variable ' s type is rvalue reference, the expression consisting of its name was an lvalue expression.

The variable name, function in the scope. such as cin . Even the type of the variable is an rvalue reference type as long as it is named so it is an lvalue.

function call or overloaded operator expression if the function ' s or overloaded operator ' s return type was an lvalue refere NCE, such as Std::getline (Std::cin, str) or std::cout << 1 or str1 = STR2 or ++iter

function calls or some overloaded operations, as long as their return type is an lvalue reference type, is also an lvalue.

Built-in pre-increment and Pre-decrement, dereference, assignment and compound assignment, subscript (except on an array x Value), member access (except for non-static non-reference members of XValues, member enumerators, and Non-static member F unctions), member access through pointer to data member if the left-hand operand is lvalue, comma operator if the right-ha nd operand is lvalue, ternary conditional if the second and third operands are lvalues.

The effect is that the built-in increment-decrement operator results in an lvalue.

Cast expression to Lvalue reference type.

An expression that is converted to an lvalue reference type is also an lvalue.

String literal

string literal value.

function call expression if the function ' s return type was rvalue reference to function type (rare)

If the return type of the function is an rvalue reference type, then the function call expression can be an lvalue. Relatively rare.
Cast expression to rvalue reference to function.
A function expression that is converted to an rvalue reference type.

(Since c++11)

Properties: (Attribute)
Same as Glvalue (below)

Consistent with Glvae.
Address of an lvalue is taken: &++i[1] and &std::endl are valid expressions.
Can be taken to the site.
A modifiable Lvalue may be used as the left-hand operand of the built-in assignment operator.
Modifiable Lvalue can be used as the left operand (object) of the built-in assignment operator
An lvalue is used to initialize an lvalue reference; This associates a new name with the object identified by the expression.
An lvalue can be used to initialize an lvalue reference, which is equivalent to an alias for an lvalue.

Rvalue (until c++11)/Prvalue (since c++11)

A prvalue** ("pure" rvalue) * * is an expression, a identifies a temporary object (or a subobject thereof) or is a value n OT associated with any object.

A pure-right value is an expression that is considered a temporary object or a child object in it, or a value that has no associated object.

The following expressions is prvalues:

The following expressions are all pure right values:

Literal (except string Literal), such as or true or nullptr.

Literal values, such as 42,true,nullptr.

function call or overloaded operator expression if the function ' s or the overloaded operator ' s return type was not a refere NCE, such as STR.SUBSTR (1, 2) or STR1 + str2

The return type of a function call is not a reference type, such as Str.substr or STR1+STR2. It is not clear that the SUBSTR function can be used to examine the function prototype.

Built-in post-increment and post-decrement, arithmetic and logical operators, comparison operators, address-of operator, member access for a member enumerator, a non-static member function, or a non-static non-reference data member of an Rvalu E, member access through pointer to a data member of Rvalue or to a non-static member function, comma operator where the R Ight-hand operand is rvalue, ternary conditional where either second or third operands aren ' t lvalues.

Built-in post-increment-decrement operators, arithmetic operators, logical operators, and so on, result in a pure-right value.

Cast expression to any type other than reference type.

Converts an expression to any type other than a reference type.

Lambda expressions, such as [] (int x) {return x*x;}

The lambda expression is also a pure right value, and it is unclear to refer to the previous blog post.

(Since c++11)
Properties:
Same as rvalue (below)

Consistent with the right value.

A prvalue cannot be polymorphic:the dynamic type of the object it identifies are always the type of the expression.

A pure right value cannot be polymorphic. It is considered that the dynamic type is always the type of the expression

A non-class prvalue cannot be cv-qualified.

A non-class, pure right value cannot be local cv-qualified . CV represents Const/volatile,.

A prvalue cannot has incomplete type (except for type void, see below, or if used in decltype specifier)

A pure right value cannot be an incomplete type. In addition to the void types described below.

Xvalue

An xvalue was an expression of that identifies an "expiring" object, which is, the object of May was moved from. The object identified by an xvalue expression is a nameless temporary, it may is a named object in scope, or any other Kind of object, but if used as a function argument, xvalue'll always bind to the Rvalue reference overload if available .

The Xvalue value is a type of object that is considered to be disappearing, meaning that the object will be moved. This object may be an anonymous temporary object, or it may be a named object or other type of object in a scope. But when Xvalue is used as a parameter to a function, Xvlaue is always bound to an rvalue reference type, provided that such a function exists and the formal parameter is an rvalue reference type.

Only the following expressions is XValues: (only the following expression is XValues).

A function call or overloaded operator expression if the function ' s or the overloaded operator ' s return type was an rvalue Reference to object type, such as Std::move (Val)
A cast expression to the Rvalue reference to object type, such as static_cast

Mixed categories (mixed category) Glvalue

A Glvalue ("generalized" lvalue) is an expression of that was either an lvalue or an xvalue.

Neither the Lvalue nor the Xvalue type is gvalue.
Properties (Note:these apply to pre-c++11 lvalues as well)
A Glvalue May is implicitly converted to Prvalue with Lvalue-to-rvalue, array-to-pointer, or Function-to-pointer implicit Conversion.
A Glvalue may polymorphic:the dynamic type of the object it identifies are not necessarily the static type of the Expre Ssion.
A Glvalue can has incomplete type, where permitted by the expression

Rvalue

An rvalue was an expression of that was either a prvalue or an xvalue.
Properties (note, these apply to both XValues and prvalues, which means they apply to the pre-c++11 rvalues as well)
Address of an rvalue is taken: &int (), &i++[2], &42, and &std::move (val) are invalid.
Rvalues can ' t be used as the left-hand operand of the built-in assignment or compound assignment operator.
An rvalue is used to initialize a const lvalue reference, in which case the lifetime of the object identified by the R Value is extended until the scope of the reference ends.
An rvalue is used to initialize a rvalue reference, in which case the lifetime of the object identified by the Rvalue is extended until the scope of the reference ends.
When used as a function argument and when the overloads of the function is available, one taking rvalue reference Paramet Er and the other taking Lvalue reference to const parameter, rvalues bind to the Rvalue reference overload (thus, if both Copy and move constructors is available, rvalue arguments invoke the move constructor, and likewise with copy and move as Signment operators).
(Since c++11)

Special categories (Special category) Pending member function call

The expressions Obj.func and Ptr->func, where Func is a non-static member function, and the Expressions OBJ.*MFP and PT R->*MFP where MFP is a pointer to member function, being classified as prvalue expressions, but they cannot being used to in Itialize references, as function arguments, or for any purpose @ all, except as the left-hand argument of a function call expression, e.g. (POBJ->*PTR) (args).

Void expressions

Function call expressions returning void, cast expressions to void, and throw-expressions is classified as Prvalue Expres Sions, but they cannot is used to initialize references or as function arguments. They can is used in some contexts (e.g. on a line of it own, as the left argument of the comma operator, etc) and in the Return statement in a function returning void. In addition, Throw-expressions is used as the second and the third operands of the conditional operator?: (Other void Prvalues can is only used if appearing as both 2nd and 3rd operands).

Bit fields

An expression, that designates a bit field (e.g. s.x where s are an object of type struct s {int x:3;};) was an lvalue expr Ession (or Xvalue if S is one): it could be used on the left hand side of the assignment operator, but its address cannot be Taken and a non-const lvalue reference cannot is bound to it. A const lvalue reference can be initialized from a Bit-field lvalue, but a temporary copy of the Bit-field would be made:i T won ' t bind to the bit field directly.

Footnotes

↑assuming I have built-in type or the pre-increment operator is overloaded to return by Lvalue reference
↑assuming I have built-in type or the postincrement operator is isn't overloaded to return by Lvalue reference

CPP & Value Category

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.