cpp/language/expressions

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

Expression evaluation may produce a result (e.g., evaluation of produces the result ) and may generate side-effects (e.g. evaluation of  prints the character  on the standard output).

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

General

 * (lvalue, rvalue) classify expressions by their values
 * of arguments and subexpressions specify the order in which intermediate results are obtained

Operators

 * defines the order in which operators are bound to their arguments
 * are alternative spellings for some operators
 * makes it possible to specify the behavior of the operators with user-defined classes.

Conversions

 * implicit conversions from one type to another
 * conversion using C-style cast notation and function-style notation
 * makes it possible to specify conversion from user-defined classes
 * conversion using C-style cast notation and function-style notation
 * makes it possible to specify conversion from user-defined classes
 * conversion using C-style cast notation and function-style notation
 * makes it possible to specify conversion from user-defined classes

Memory allocation

 * allocates memory dynamically
 * deallocates memory dynamically

Other

 * s can be evaluated at compile time and used in compile-time context (template arguments, array sizes, etc)

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

Primary expressions are any of the following:
 * literals (e.g. or )
 * id-expressions, including
 * suitably declared (e.g.  or ),
 * suitably declared (e.g. std), and
 * identifiers to be declared in
 * identifiers to be declared in

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.


 * s are decimal, octal, hexadecimal or binary numbers of integer type.
 * s are individual characters of type
 * or


 * are values of type, , or
 * s are sequences of characters of type
 * or


 * are values of type, that is and

Full-expressions
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.

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, the left-hand operand of the built-in comma operator, or the operand of a cast-expression that casts to the type.

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 glvalue 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.

{{rrev|since=c++20|

Expression-equivalence
A number of expressions, , ..., are expression-equivalent if
 * 1) they have the same effects;
 * 2) either they are all  or neither is;
 * 3) either they are all  or else neither is.

is expression-equivalent to if and only if  and  are expression-equivalent (which means  is also expression-equivalent to ). }}