We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
ar1222, 814 PM r+ Operator Precedence -eppreterence.com
C++ Operator Precedence
‘The following table lists the precedence and associativity of C++ operators. Operators are listed top to bottom, in
descending precedence,
Precedence Operator Description ‘Associativity
2 Scope resolution Left-to-right >
an as Suffixipostfix increment and decrement
type() type(} Functional cast
2 ao Function call
all Subscript
> Member access
Ha Prefix increment and decrement Right-to-left =
faa Unary plus and minus
a Logical NOT and bitwise NOT
(type) style cast
a Indirection (dereference)
a sa Address-of
sizeof Size-ofnate 1)
co_await await-expression (c++20)
new new(] Dynamic memory allocation
delete delete!) Dynamic memory deallocation
4 od Pointer-to-member Left-to-right >
5 _atb a/b ab Multiplication, division, and remainder
6 at ab Addition and subtraction
7c Bitwise left shift and right shift
Cod ‘Three-way comparison operator (since C20)
9 For relational operators < and = and > and = respectively
10 For equality operators = and # respectively
1 Bitwise AND
2 Bitwise XOR (exclusive or)
13 Bitwise OR (inclusive or)
14 Logical AND
1s Logical OR
arbic ‘Ternary conditionailnote 2] Right-to-left«
throw throw operator
co_yield yleld-expression (¢++20)
16 Direct assignment (provided by default for C++ classes)
‘Compound assignment by sum and difference
Compound assignment by product, quotient, and remainder:
Compound assignment by bitwise left shift and right shift
‘Compound assignment by bitwise AND, XOR, and OR
7, Comma Left-to-right >
1.1 The operand of sizeof can't be a Cstyle type cast: the expression sizeof (int
(sizeoflint)) * p), but not sizeof ((int)=p)
2.1 The expression in the middle of the consitional operator (between 7 and :) is parsed as if parenthesized: its precedence
relative to 72 is ignores,
* is unambiguously interpreted as
When parsing an expression, an operator which is sted on some row of the table above with a precedence will be
bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it with a
lower precedence. For example, the expressions std::cout << a & b and “pi+ are parsed as
(std::cout << a) & b and *(pr+) ,andnotas std::cout << (a & b) or (*p)++
Operators that have the same precedence are bound to their arguments in the direction of their associativity, For
‘example, the expression a = b= c isparsedas a= (b= c) ,andnotas (a = b) = c because of right-to-left
associativity of assignment, but a +b - ¢ isparsed (a +b) - ¢ andnot a+ (b - c) because of left-to-right
associativity of addition and subtraction
nce 12
hitpsien.cppreference.comwlepplanguageloperator_precsar1222, 814 PM r+ Operator Precedence -eppreterence.com
‘Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators
always associate right-to-left (delete ++*p is delete(++(*p)) ) and unary postfix operators always associate left-
to-right ( a[1](2]++ is ((a[1]) (2])++ ). Note that the associativity is meaningful for member access operators,
‘even though they are grouped with unary postfix operators: a.b++ is parsed (a.b)++ and not a. (b++)
Operator precedence is unaffected by operator overloading. For example, std::cout << a? b : ¢;) parses as
(std::cout << a) 7 b : c; because the precedence of arithmetic left shift is higher than the canditional operator.
Notes
Precedence and associativity are compile-time concepts and are independent from order of evaluation, which is a
runtime concept
‘The standard itself doesn't specify precedence levels. They are derived from the grammar.
const_cast, static_cast, dynamic_cast, reinterpret_cast, typeid, sizeof..., noexcept and alignof are not
included since they are never ambiguous.
Some of the operators have alternate spellings (e.9., and for &&, or for ||, not) for, etc.)
Inc, the ternary conditional operator has higher precedence than assignment operators. Therefore, the expression
e = a