C++ Operator Precedence
作者:
R同学
,
2022-01-19 20:44:28
,
所有人可见
,
阅读 264
Precedence Operator Description Associativity
-----------------------------------------------------------------------------------------------------------
1 :: Scope resolution Left-to-right
----------------------------------------------------------------------------------------------
a++ a-- Suffix/postfix increment and decrement
type() type{} Functional cast
2 a() Function call
a[] Subscript
. -> Member access
-----------------------------------------------------------------------------------------------------------
++a --a Prefix increment and decrement Right-to-left
+a -a Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) C-style cast
3 *a Indirection (dereference)
&a Address-of
sizeof Size-of[note 1]
co_await await-expression (C++20)
new new[] Dynamic memory allocation
delete delete[] Dynamic memory deallocation
-----------------------------------------------------------------------------------------------------------
4 .* ->* Pointer-to-member Left-to-right
----------------------------------------------------------------------------------------------
5 a*b a/b a%b Multiplication, division, and remainder
----------------------------------------------------------------------------------------------
6 a+b a-b Addition and subtraction
----------------------------------------------------------------------------------------------
7 << >> Bitwise left shift and right shift
----------------------------------------------------------------------------------------------
8 <=> Three-way comparison operator (since C++20)
----------------------------------------------------------------------------------------------
9 < <= > >= For relational operators < and ≤ and > and ≥ respectively
----------------------------------------------------------------------------------------------
10 == != For equality operators = and ≠ respectively
----------------------------------------------------------------------------------------------
11 a&b Bitwise AND
----------------------------------------------------------------------------------------------
12 ^ Bitwise XOR (exclusive or)
----------------------------------------------------------------------------------------------
13 | Bitwise OR (inclusive or)
----------------------------------------------------------------------------------------------
14 && Logical AND
----------------------------------------------------------------------------------------------
15 || Logical OR
-----------------------------------------------------------------------------------------------------------
a?b:c Ternary conditional[note 2] Right-to-left
throw throw operator
co_yield yield-expression (C++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
-----------------------------------------------------------------------------------------------------------
17 , Comma Left-to-right
NOTES:
- The operand of sizeof can’t be a C-style type cast: the expression
sizeof (int) * p
is unambiguously interpreted as (sizeof(int)) * p
, but not sizeof((int)*p)
.
- The expression in the middle of the conditional operator (between ? and :) is parsed as if parenthesized: its precedence relative to ?: is ignored.