Lvalues and rvalues

Expressions can be categorized into one of the following value categories:
Lvalue
An expression can appear on the left side of an assignment expression if the expression is not const qualified.
C++11XvalueC++11

An rvalue reference that is to expire.

C++11(Prvalue)C++11 rvalue
A C++11non-xvalueC++11 expression that appears only on the right side of an assignment expression.
C++11Rvalues include both xvalues and prvalues. Lvalues and xvalues can be referred as glvalues.C++11
Notes:
  • Class C++11(prvalue)C++11 rvalues can be cv-qualified, but non-class C++11(prvalue)C++11 rvalues cannot be cv-qualified.
  • Lvalues C++11and xvaluesC++11 can be of incomplete types, but C++11(prvalue)C++11 rvalues must be of complete types or void types.
An object is a region of storage that can be examined and stored into. An lvalue C++11or xvalueC++11 is an expression that refers to such an object. An lvalue does not necessarily permit modification of the object it designates. For example, a const object is an lvalue that cannot be modified. The term modifiable lvalue is used to emphasize that the lvalue allows the designated object to be changed as well as examined. Lvalues of the following object types are not modifiable lvalues:
  • An array type
  • An incomplete type
  • A const-qualified type
  • A structure or union type with one of its members qualified as a const type
Because these lvalues are not modifiable, they cannot appear on the left side of an assignment statement, C++ onlyexcept where a suitable assignment operator existsC++ only.

C onlyC defines a function designator as an expression that has function type. A function designator is distinct from an object type or an lvalue. It can be the name of a function or the result of dereferencing a function pointer. The C language also differentiates between its treatment of a function pointer and an object pointer.C only

C++ onlyA function call that returns an lvalue reference is an lvalue. Expressions can produce an lvalue, C++11an xvalueC++11, a C++11(prvalue)C++11 rvalue, or no value. C++ only

Certain C++built-inC++ operators require lvalues for some of their operands. The following table lists these operators and additional constraints on their usage.
Operator Requirement
& (unary) Operand must be an lvalue.
++ -- Operand must be a modifiable lvalue. This applies to both prefix and postfix forms.
= += -= *= %= <<= >>= &= ‸= |= Left operand must be a modifiable lvalue.

For example, all assignment operators evaluate their right operand and assign that value to their left operand. The left operand must be a modifiable lvalue.

The address operator (&) requires an lvalue as an operand while the increment (++) and the decrement (--) operators require a modifiable lvalue as an operand. The following example shows expressions and their corresponding lvalues.
Expression Lvalue
x = 42 x
*ptr = newvalue *ptr
a++ a
C++ only f() The function call to f()
C++11
The following expressions are xvalues:
  • The result of calling a function whose return type is of an rvalue reference type
  • A cast to an rvalue reference
  • A nonstatic data member of a non-reference type accessed through an xvalue expression
  • A pointer to member access expression in which the first operand is an xvalue expression and the second operand is of a pointer to member type
See the following example:
int a;
int&& b= static_cast<int&&>(a);

struct str{
   int c;
};

int&& f(){
   int&& var =1;
   return var;
}

str&& g();
int&& rc = g().c;
In this example, The initializer for rvalue reference b is an xvalue because it is a result of a cast to an rvalue reference. A call to the function f() produces an xvalue because the return type of this function is of the int&& type. The initializer for rvalue reference rc is an xvalue because it is an expression that accesses a nonstatic non-reference data member c through an xvalue expression.
C++11