Thursday, July 9, 2020

C++ Value Categories: each C++ expression has a type and a value category - one of prvalue (pure right value), xvalue (eXpiring value) or lvaue (left value)

So much has changed in C++ semantics.

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: prvalue, xvalue, and lvalue.
  • a glvalue (“generalized” lvalue) is an expression whose evaluation determines the identity of an object, bit-field, or function;
  • a prvalue (“pure” rvalue) is an expression whose evaluation either
  • computes a value that is not associated with an object
  • creates a temporary object and denotes it
(until C++17)
  • computes the value of the operand of an operator or is a void expression (such prvalue has no result object), or
  • initializes an object or a bit-field (such prvalue is said to have a result object). With the exception of decltype, all class and array prvalues have a result object even if it is discarded. The result object may be a variable, an object created by new-expression, a temporary created by temporary materialization, or a member thereof;
(since C++17)
  • an xvalue (an “eXpiring” value) is a glvalue that denotes an object or bit-field whose resources can be reused;
  • an lvalue (so-called, historically, because lvalues could appear on the left-hand side of an assignment expression) is a glvalue that is not an xvalue;
  • an rvalue (so-called, historically, because rvalues could appear on the right-hand side of an assignment expression) is a prvalue or an xvalue.
Note: this taxonomy went through significant changes with past C++ standard revisions


Example xvalue:

xvalue

The following expressions are xvalue expressions:
  • a function call or an overloaded operator expression, whose return type is rvalue reference to object, such as std::move(x);
  • a[n], the built-in subscript expression, where one operand is an array rvalue;

No comments:

Post a Comment