Value categories
From Cppreference
When a C++ expression (an operator with its arguments) is evaluated, it results in a value. Each expression belongs to exactly one of the primary value categories.
Contents |
[edit] Primary categories
[edit] lvalue
An lvalue is an expression that identifies a non-temporary object or a non-member function.
The following expressions are lvalues:
- The name of an object or function in scope, regardless of type, such as std::cin or std::endl. Even if the object's type is rvalue reference, the expression consisting of its name is an lvalue expression.
- Function call or operator expression if the function's or operator's return type is an lvalue reference, such as std::getline(cin, str) or cout << 1 or or ++n or *p
- Cast expression to lvalue reference type.
- Function call expression if the function's return type is rvalue reference to function type (rare)
- Cast expression to rvalue reference to function.
Properties:
- Address of an lvalue may be taken: &++i and &std::endl are valid expressions.
- An lvalue may be used to initialize an lvalue reference; this associates a new name with the object identified by the expression.
- An lvalue may be implicitly converted to rvalue with lvalue-to-rvalue, array-to-pointer, or function-to-pointer implicit conversions.
[edit] rvalue (until C++11) / prvalue (since C++11)
A prvalue is an expression that identifies a temporary object (or a subobject thereof) or is a value not associated with any object.
The following expressions are prvalues:
- Literal, such as 42 or true or nullptr.
- Function call/operator expression if the function's or the operator's return type is not a reference, such as str.substr(1, 2) or 2+2
- Cast expression to any type other than reference type.
Properties:
This section is incomplete Reason: something unique to prvalues but not xvalues |
[edit] xvalue (since C++11)
An xvalue is an expression that identifies an "expiring" temporary object, that is, the object that may be moved from.
Only the following expressions are xvalues:
- A function call expression, if the function's return type is an rvalue reference to object type, such as std::move(val)
- A cast expression to an rvalue reference to object type, such as static_cast<T&&>(val) or (T&&)val
- a non-static class member access expression, in which the object expression is an xvalue
- A pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.
Properties:
- An xvalue may be (and is usually expected to be) moved from, that is, used as the argument to a move constructor or move assignment operator.
[edit] Mixed categories
[edit] glvalue(since C++11)
A glvalue is an expression that is either an lvalue or an xvalue.
Properties:
This section is incomplete Reason: needs something in common for all glvalues |
[edit] rvalue (since C++11)
An rvalue is an expression that is 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 may not be taken: &i++ and &42 and &std::move(val) are invalid.
- 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.
- An rvalue may be used to initialize an rvalue reference, in which case the lifetime of the object identified by the rvalue is extended until the scope of the reference ends.