Namespaces
Variants
Views
Actions

Expressions

From cppreference.com
< cpp‎ | language

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

Contents

[edit] General

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

[edit] Operators

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a = rvalue
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->b
a.b
a->*b
a.*b

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

Special operators

static_cast converts one type to another compatible type
dynamic_cast converts virtual base class to derived class
const_cast converts type to compatible type with different cv qualifiers
reinterpret_cast converts type to incompatible type
new allocates memory
delete deallocates memory
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)

[edit] Conversions

[edit] Memory allocation

[edit] Other


[edit] 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:

1) Literals (e.g. 2 or "Hello, world")
2) Suitably declared unqualified identifiers (e.g. n or cout)
3) Suitably declared qualified identifiers (e.g. std::string::npos)

Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator.

[edit] Literals

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

[edit] Unevaluated expressions

The operands of the four operators typeid, sizeof, noexcept, and decltype are expressions that are not evaluated, 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.