Expressions

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++20)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static
Special member functions
Templates
Miscellaneous
 
 

An expression is a sequence of operators and their operands, that specifies a computation.

Expression evaluation may produce a result (e.g., evaluation of 2 + 2 produces the result 4) and may generate side-effects (e.g. evaluation of std::printf("%d",4) prints the character '4' on the standard output).

Each C++ expression is characterized by two independent properties: A type and a value category.

General

  • value categories (lvalue, rvalue, glvalue, prvalue, xvalue (since C++11)) classify expressions by their values
  • order of evaluation of arguments and subexpressions specify the order in which intermediate results are obtained

Operators

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b
a <=> b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
a ? b : c

Special operators

static_cast converts one type to another related type
dynamic_cast converts within inheritance hierarchies
const_cast adds or removes cv qualifiers
reinterpret_cast converts type to unrelated type
C-style cast converts one type to another by a mix of static_cast, const_cast, and reinterpret_cast
new creates objects with dynamic storage duration
delete destructs objects previously created by the new expression and releases obtained memory area
sizeof queries the size of a type
sizeof... queries the size of a parameter pack (since C++11)
typeid queries the type information of a type
noexcept checks if an expression can throw an exception (since C++11)
alignof queries alignment requirements of a type (since C++11)

Conversions

Memory allocation

Other

Primary expressions

The operands of any operator may be other expressions or primary expressions (e.g. in 1 + 2 * 3, the operands of operator+ are the subexpression 2 * 3 and the primary expression 1).

Primary expressions are any of the following:

(since C++11)
(since C++17)
(since C++20)

Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator. Parentheses preserve value, type, and value category.

Literals

Literals are the tokens of a C++ program that represent constant values embedded in the source code.

  • char or wchar_t
  • char16_t or char32_t
(since C++11)
  • char8_t
(since C++20)
  • const char[] or const wchar_t[]
  • const char16_t[] or const char32_t[]
(since C++11)
  • const char8_t[]
(since C++20)
(since C++11)

Full-expressions

A constituent expression is defined as follows:

  • The constituent expression of an expression is that expression.
  • The constituent expressions of a braced-init-list or of a (possibly parenthesized) expression list are the constituent expressions of the elements of the respective list.
  • The constituent expressions of a brace-or-equal-initializer of the form = initializer-clause are the constituent expressions of the initializer-clause.
int num1 = 0;
num1 += 1; // Case 1: the constitunent expression of `num += 1` is `num += 1`
 
int arr2[2] = {2, 22} // Case 2: the constitunent expressions
                      //         of `{2, 22}` are `2` and `22`
                      // Case 3: the constitunent expressions of ` = {2, 22}`
                      //         are the constitunent expressions of `{2, 22}`
                      //         (i.e. also `2` and `22`)

The immediate subexpressions of an expression E are

  • the constituent expressions of E’s operands,
  • any function call that E implicitly invokes,
  • if E is a lambda expression, the initialization of the entities captured by copy and the constituent expressions of the initializer of the captures,
  • if E is a function call or implicitly invokes a function, the constituent expressions of each default argument used in the call, or
  • if E creates an aggregate object, the constituent expressions of each default member initializer used in the initialization.


A subexpression of an expression E is an immediate subexpression of E or a subexpression of an immediate subexpression of E. Note that expressions appearing in the 'function body' of lambda expressions are not subexpressions of the lambda expression.

(since C++17)


A full-expression is an expression that is not a subexpression of another expression. In some contexts, such as unevaluated operands, a syntactic subexpression is considered a full-expression. (since C++14)

(until C++17)

A full-expression is

(since C++20)
  • a declarator of a simple declaration or a member initializer, including the constituent expressions of the initializer,
  • an invocation of a destructor generated at the end of the lifetime of an object other than a temporary object whose lifetime has not been extended, or
  • an expression that is not a subexpression of another expression and that is not otherwise part of a full-expression.
(since C++17)

If a language construct is defined to produce an implicit call of a function, a use of the language construct is considered to be an expression for the purposes of this definition. Conversions applied to the result of an expression in order to satisfy the requirements of the language construct in which the expression appears are also considered to be part of the full-expression.

A call to a destructor generated at the end of the lifetime of an object other than a temporary object whose lifetime has not been extended is an implicit full-expression.

(since C++11)
(until C++17)

For an initializer, performing the initialization of the entity (including evaluating default member initializers of an aggregate) is also considered part of the full-expression.

(since C++17)

Unevaluated expressions

The operands of the operators typeid, sizeof, noexcept, and decltype (since C++11) are expressions that are not evaluated (unless they are polymorphic glvalues and are the operands of typeid), since these operators only query the compile-time properties of their operands. Thus, std::size_t n = sizeof(std::cout << 42); does not perform console output.

The requires-expressions are also unevaluated expressions.

(since C++20)

Discarded-value expressions

A discarded-value expression is an expression that is used for its side-effects only. The value calculated from such expression is discarded. Such expressions include the full-expression of any expression statement, the left-hand operand of the built-in comma operator, or the operand of a cast-expression that casts to the type void.

Array-to-pointer and function-to-pointer conversions are never applied to the value calculated by a discarded-value expression. The lvalue-to-rvalue conversion is applied if and only if the expression is a volatile-qualified lvalue (until C++11)glvalue (since C++11) and has one of the following forms (built-in meaning required, possibly parenthesized):

  • id-expression,
  • array subscript expression,
  • class member access expression,
  • indirection,
  • pointer-to-member operation,
  • conditional expression where both the second and the third operands are one of these expressions,
  • comma expression where the right operand is one of these expressions.

In addition, if the lvalue is of volatile-qualified class type, a volatile copy constructor is required to initialize the resulting rvalue temporary.

If the expression is a non-void prvalue (after any lvalue-to-rvalue conversion that might have taken place), temporary materialization occurs.

Compilers may issue warnings when an expression other than cast to void discards a value declared [[nodiscard]].

(since C++17)

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1054 C++98 assigning a value to a volatile variable might
result in an unnecessary read due to the lvalue-to-
rvalue conversion applied to the assignment result
introduce discarded-value expressions
and exclude this case from the list
of cases that require the conversion
CWG 1383 C++98 the list of expressions where lvalue-to-rvalue
conversion is applied to discarded-value
expressions also covered overloaded operators
only cover operators
with built-in meaning
CWG 1576 C++11 lvalue-to-rvalue conversions were not applied
to discarded-value volatile xvalue expressions
apply the conversion
in this case
CWG 2249 C++98 identifiers to be declared in declarators were not id-expressions they are
CWG 2431 C++11 the invocations of the destructors of temporaries that
are bound to references were not full-expressions
they are

See also