Value categories
Three primary categories
Each C++ expression (an operator with its operands, a literal, a variable name, etc.) is characterized by two independent properties: a type and a value category. Each expression has some non-reference type, and each expression belongs to exactly one of the three primary value categories.
每一个C++表达式有两个特征:类型和值分类
Expressions that:
- have identity and cannot be moved from are called lvalue expressions;
- have identity and can be moved from are called xvalue expressions;
- do not have identity and can be moved from area called prvalue expression;(比如常量等)
- do not have identity and cannot be moved from are not used.
lvalue
An lvalue("left value") expression is an expression that has identity and cannot be moved from.
The naming is historic and reflects the use of lvalue expressions as the left-hand operand of the assignment operator in the CPL programming language.
The following expressions are lvalue expression:
- the name of a variable or a function in scope, regardless of type, such as
std::cin
orstd::endl
. Even if the variable's type is rvalue reference, the expression consisting of its name is an lvalue expression; a = b, a += b, a %= b
, and all other built-in assignment and compound assignment expressions++a and --a
, the built-in pre-increment and pre-decrement expressions;*p
, the built-in indirection expressiona[n] and p[n]
, the built-in subscript expressiona.m
, the member of object expression, except where m is a member enumerator or a non-static member function, or where a is an rvalue and m is a non-static data member of non-reference type;p->m
, the built-in member of pointer expression, except where m is a member enumerator or a non-static member function;a.*mp
, the pointer to member of object expression, where a is an lvalue and mp is a pointer to data member;- p->*mp, the built-in pointer to member of pointer expression, where mp is a pointer to data member;
a, b
, the built-in comma expression, where b is an lvalue;a ? b : c
, the ternary conditional expression for some a, b, and c;- a
string literal
, such as "Hello, world!"; - a cast expression to lvalue reference type, such as
static_cast<int&>(x);
Properties:
- Same as glvalue
- Address of lvalue may be taken: &++i and
std::endl
are valid expressions. - A modifiable lvalue may be used as the left-hand operand of the built-in assignment and compound assignment operators.
- An lvalue may be used to initialize an lvalue reference; this associates a new name with the object identified by the expression.
rvalue (until C++11) prvalue (since C++11)>
A prvalue ("pure rvalue") expression is an expression that does not have identity and can be moved from.
The following expressions are prvalue expression:
- a literal (except for string literal), such as 42,
true
ornullptr
; - a function call or an overloaded operator expression of non-reference return type, such as
str.substr(1, 2)
,str1 + str2
, orit++
; a++ and a--
, the built-in post-increment and post-decrement expressions;a + b, a % b, a & b, a << b
, and all other built-in arithmetic expressions;a && b, a || b, ~a
, the built-in logical expressions;a < b, a == b, a >= b
, and all other built-in comparison expressions;&a
, the built-in address-of expression;a.m
, the member of object expression, where m is a member enumerator or a non-static member function[3], or where a is an rvalue and m is a non-static data member of non-reference type (until C++11);p->m
, the built-in member of pointer expression, where m is a member enumerator or a non-static member function- a.*mp, the pointer to member of object expression, where mp is a pointer to member function[3], or where a is an rvalue and mp is a pointer to data member (until C++11)
p->*mp
, the built-in pointer to member of pointer expression, where mp is a pointer to member functiona, b
, the built-in comma expression, where b is an rvalue;a ? b : c
, the ternary conditional expression for some a, b, and c- a cast expression to non-reference type, such as
static_cast<double>(x), std::string{}, or (int)42
Properties:
- Same as rvalue
- A prvalue cannot be polymorphic: the dynamic type of the object it identifies is always the type of the expression.
- A non-class prvalue cannot be cv-qualified.
xvalue
An xvalue -("expiring value") expression is an expression that has identity and can be moved from.
The following expressions are xvalue expressions:
- a function call or an overloaded operator expression of rvalue reference to object return type, such as
std::move(x)
;
Properties
- Same as rvalue
- Same as glvalue
Mixed categories
glvalue
A glvalue ("generalized lvalue") expression is an expression that is either an lvalue or an xvalue. It has identity. It may or may not be moved from.
Properties:
- a glvalue may be implicitly converted to a prvalue with lvalue-to-rvalue,array-to-pointer,function-to-pointer implicit convention.
- A glvalue may be polymorphic: the dynamic type of the object it identifies is not necessarily the static type of the expression.
- A glvalue can have incomplete type, where permitted by the expression.
rvalue
An rvalue("rigthj value") expression is an expression that is either prvalue or an xvalue. It can be moved from. It may or may not have identity.
The naming is historic and reflects the use of rvalue expressions as the right-hand operand of the assignment operator in the CPL programming language.
Properties :
- Address of an rvalue may not be taken:
&int(), &i++, &42, and &std::move(x)
are invalid. - An rvalue can't be used as the left-hand operand of the built-in assignment or compound assignment operators.
- An rvalue may be used to initialize a const lvalue reference, in which case the lifetime of the object identified by the rvalue is extended until the scope of the reference ends.
Special categories
Pending member function call
The expressions a.mf
and p->mf
, where mf is a non-static member function, and the expressions a.*mfp
and p->*mfp
, where mfp is a pointer to member function, are classified as prvalue expressions
, but they cannot be used to initialize references, as function arguments, or for any purpose at all, except as the left-hand argument of the function call operator, e.g. (p->*mfp)(args)
.
Void expressions
Function call expressions returning void
, cast expressions to void
, and throw-expressions
are classified as prvalue expressions
, but they cannot be used to initialize references or as function arguments.
Bit fields
An expression that designates a bit field
(e.g. a.m
, where a is an lvalue of type struct A { int m: 3; }
) is an lvalue expression:
it may be used as the left-hand operand of the assignment operator, but its address cannot be taken and a non-const lvalue reference cannot be bound to it.
A const lvalue reference can be initialized from a bit-field lvalue, but a temporary copy of the bit-field will be made: it won't bind to the bit field directly.(不会直接绑定到原来值上的,而是一个临时的副本)