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.
[edit] Generala = 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[...]
*a
&a
a->b
a.b
a->*b
a.*b
a(...)
commaa, b
conditionala ? b : c
Special operatorsstatic_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 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)
const_cast
conversionstatic_cast
conversiondynamic_cast
conversionreinterpret_cast
conversionsizeof
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
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.
[edit] LiteralsLiterals are the tokens of a C++ program that represent constant values embedded in the source code.
nullptr
is the pointer literal which specifies a null pointer valueA constituent expression is defined as follows:
=
are the constituent expressions of the initializer-clause.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 E are
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++11)
The following expressions are 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.
For an initializer, performing the initialization of the entity (including evaluating default member initializers of an aggregate)(since C++14) is also considered part of the full-expression.
[edit] Potentially-evaluated expressionsAn expression is potentially evaluated unless
sizeof
operator, ortypeid
operator and does not designate an lvalue of polymorphic class type.The following operands are unevaluated operands, they are not evaluated:
typeid
operator applies to, except glvalues of polymorphic class typessizeof
operatornoexcept
operatordecltype
specifierAn expression is potentially evaluated unless
Potentially-evaluated expressions are ODR-use.
[edit] Discarded-value expressionsA 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 [[nodiscard]]
.
A number of expressions e1, e2, ..., eN are expression-equivalent if all following conditions are satisfied:
e1 is expression-equivalent to e2 if and only if e1 and e2 are expression-equivalent (which means e2 is also expression-equivalent to e1).
(since C++20) [edit] Defect reportsThe 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 mightRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4