What is the precedence and associativity of operators?

439 Views Asked by At

What is the precedence and associativity of operators in C++?

Who defines operator precedence and associativity, and how does it relate to order of evaluation? explains how those properties emerge from the grammar. However, I am simply interested in a list of all the rules.


This question is supposed to be a community FAQ which can be referenced instead of the cppreference article.

1

There are 1 best solutions below

0
Jan Schultke On

C++ Operator Precedence

Operator/Expression Grammatical Rule Description Associativity
and Arity
0, id, this, []{},
(expression),
(x, ...),
requires () {}
primary-expression Literals, identifiers, fold expressions, requires expressions, etc. These don't really have precedence, but are the building block out of which other expressions are built. None
:: qualified-id Scope resolution. :: is also not an operator strictly speaking, but a way of combining multiple identifiers into a single name. Left-to-right
Binary
x++ x--
type() type{}
x() x[]
., ->
static_cast
typeid, ...
postfix-expression Postfix expressions. Casts like static_cast are also classified as a postfix expression, although usually people don't consider it an operator and there is usually no confusion as to its precedence. Left-to-right
Various
++x, --x
+x -x
!x ~x
(type) x
*x &x
sizeof
co_await
new
delete
unary-expression Prefix expressions. This also includes C-style casts, co_await, new, and delete, which not everyone expects to be an operator.

Note that the operand to sizeof cannot be a C-style cast, e.g. sizeof (int)x is parsed as sizeof(int) followed by x.
Right-to-left
Various
.* ->* pm-expression Pointer-to-member operators. Left-to-right
Binary
* / % multiplicative-expression Multiplicative operators.
+ - additive-expression Additive operators.
<< >> shift-expression Bitwise shift operators.
<=> compare-expression Three-way comparison operator.
< >
<= >=
relational-expression Relational comparison operators. Note that a < b < c means (a < b) < c and is likely a mistake.
== != equality-expression Equality comparison operators. Note that a == b == c means (a == b) == c and is likely a mistake.
& and-expression Bitwise AND operator.
^ exclusive-or-expression Bitwise XOR operator.
| inclusive-or-expression Bitwise OR operator.
&& logical-and-expression Logical AND operator.
|| logical-or-expression Logical OR operator.
a?b:c conditional-expression Conditional operator, colloquially called "ternary operator". Note that b is parsed as if it was parenthesized, so its precedence doesn't matter. Right-to-left
Ternary
= += ...
co_yield
throw
assignment-expression Regular assignment and compound assignment. Also yield expression and throw expressions. Right-to-left
Various
, expression Comma operator Left-to-right
Binary

Note that this list equally applies to operator precedence in C. The C++ rules are a superset of the C rules, meaning that they are the same, just with more operators.

Common Examples

Expression Parenthesized Explanation
x + y * z x + (y * z) * has higher precedence
x + y + z (x + y) + z + is left-associative
x = y = z x = (y = z) = is right-associative
x || y && z x || (y && z) && has higher precedence
(this makes disjunctive normal forms
easy to write)
x || y || z (x || y) || z || is left-associative
x = y ? a : b x = (y ? a : b) ?: has higher precedence,
and is right-associative
*x++ *(x++) postfix expressions have higher precedence
*++x *(++x) prefix expressions are right-associative
x | y << z x | (y << z) << has higher precedence
cout << x ? y : z (cout << x) ? y : z << has higher precedence
x << y + z x << (y + z) + has higher precedence

See also on other sites