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.
Common operators | ||||||
---|---|---|---|---|---|---|
assignment |
increment decrement | arithmetic | logical | comparison |
member access | other |
|
|
|
|
|
| function call |
a(...) |
||||||
comma | ||||||
a, b |
||||||
conditional | ||||||
a ? b : c |
||||||
Special operators | ||||||
|
const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversion sizeof
alignof
typeid
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:
this
2
or "Hello, world"
) n
or cout
), std::string::npos
), and (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 are the tokens of a C++ program that represent constant values embedded in the source code.
| (since C++11) |
| (since C++20) |
| (since C++11) |
| (since C++20) |
true
and false
| (since C++11) |
A constituent expression is defined as follows:
int num1 = 0; num1 += 1; // Case 1: the constituent expression of `num += 1` is `num += 1` int arr2[2] = {2, 22} // Case 2: the constituent expressions // of `{2, 22}` are `2` and `22` // Case 3: the constituent expressions of ` = {2, 22}` // are the constituent expressions of `{2, 22}` // (i.e. also `2` and `22`) The immediate subexpressions of an expression
A subexpression of an 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++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) |
An expression is potentially evaluated unless.
| (until C++11) | ||
The following operands are unevaluated operands , they are not evaluated:
An expression is potentially evaluated unless.
| (since C++11) |
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 glvalue and has one of the following forms (built-in meaning required, possibly parenthesized):
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 | (since C++17) |
Expression-equivalenceA number of expressions
| (since C++20) |
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 |
C documentation for Expressions |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/expressions