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).
Common operators | ||||||
---|---|---|---|---|---|---|
assignment | increment decrement | arithmetic | logical | comparison | member access | other |
|
|
|
|
|
|
|
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:
2
or "Hello, world"
)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.
char
, char16_t
, char32_t
, or wchar_t
float
, double
, or long double
const char[]
, const char16_t[]
, const char32_t[]
, or const wchar_t[]
bool
, that is true
and false
The operands of the four 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 unevaluated operands are considered to be full expressions even though they are syntactically operands in a larger expression (for example, this means that | (since C++14) |
The requires-expressions(concepts TS) are also unevaluated 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 argument of the comma operator, or the argument 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, however, is applied, but only if the expression is a volatile-qualified glvalue and has one of the following forms (possibly parenthesized).
In addition, if the expression is of class type, a volatile copy-constructor is required to initialize the resulting rvalue temporary. | (until C++17) |
If the expression is a prvalue (after any lvalue-to-rvalue conversion that might have taken place), temporary materialization occurs. If the original glvalue is of volatile-qualified class type, a volatile copy-constructor is required to initialize the resulting rvalue temporary. Compilers may issue warnings when an expression other than cast to | (since C++17) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/language/expressions