C++ operator precedence: Difference between revisions
Jump to navigation
Jump to search
Created page with "{| class="wikitable" |- ! Precedence ! Operator(s) ! Description |- | 1 | <code>::</code> | Scope resolution | → |- | 2 | <code>a++</code> <code>a--</code> | Suffix/postfix increment and decrement | → |- | 2 | <code>type()</code> <code>type{}</code> | Functional cast | → |- | 2 | <code>a()</code> | Function call | → |- | 2 | <code>a[]</code> | Subscript | → |- | 2 | <code>.</code> <code>-></code> | Member access | → |- | 3 | <code>++a</code> <code>--a</code>..." |
rowspans |
||
(4 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
! Operator(s) | ! Operator(s) | ||
! Description | ! Description | ||
! <abbr title="Associativity">Ass.</abbr> | |||
|- | |- | ||
| 1 | | 1 | ||
Line 10: | Line 11: | ||
| → | | → | ||
|- | |- | ||
| 2 | | rowspan=5 | 2 | ||
| <code>a++</code> <code>a--</code> | | <code>a++</code> <code>a--</code> | ||
| Suffix/postfix increment and decrement | | Suffix/postfix increment and decrement | ||
| → | | → | ||
|- | |- | ||
| <code>type()</code> <code>type{}</code> | | <code>type()</code> <code>type{}</code> | ||
| Functional cast | | Functional cast | ||
| → | | → | ||
|- | |- | ||
| <code>a()</code> | | <code>a()</code> | ||
| Function call | | Function call | ||
| → | | → | ||
|- | |- | ||
| <code>a[]</code> | | <code>a[]</code> | ||
| Subscript | | Subscript | ||
| → | | → | ||
|- | |- | ||
| <code>.</code> <code>-></code> | | <code>.</code> <code>-></code> | ||
| Member access | | Member access | ||
| → | | → | ||
|- | |- | ||
| 3 | |rowspan=10| 3 | ||
| <code>++a</code> <code>--a</code> | | <code>++a</code> <code>--a</code> | ||
| Prefix increment and decrement | | Prefix increment and decrement | ||
| ← | | ← | ||
|- | |- | ||
| <code>+a</code> <code>-a</code> | | <code>+a</code> <code>-a</code> | ||
| Unary plus and minus | | Unary plus and minus | ||
| ← | | ← | ||
|- | |- | ||
| <code>!</code> <code>~</code> | | <code>!</code> <code>~</code> | ||
| Logical NOT and bitwise NOT | | Logical NOT and bitwise NOT | ||
| ← | | ← | ||
|- | |- | ||
| <code>(type)</code> | | <code>(type)</code> | ||
| C-style cast | | C-style cast | ||
| ← | | ← | ||
|- | |- | ||
| <code>*a</code> | | <code>*a</code> | ||
| Indirection (dereference) | | Indirection (dereference) | ||
| ← | | ← | ||
|- | |- | ||
| <code>&a</code> | | <code>&a</code> | ||
| Address-of | | Address-of | ||
| ← | | ← | ||
|- | |- | ||
| <code>sizeof</code> | | <code>sizeof</code> | ||
| Size-of | | Size-of | ||
| ← | | ← | ||
|- | |- | ||
| <code>co_await</code> | | <code>co_await</code> | ||
| await-expression | | await-expression | ||
| ← | | ← | ||
|- | |- | ||
| <code>new</code> <code>new[]</code> | | <code>new</code> <code>new[]</code> | ||
| Dynamic memory allocation | | Dynamic memory allocation | ||
| ← | | ← | ||
|- | |- | ||
| <code>delete</code> <code>delete[]</code></code> | | <code>delete</code> <code>delete[]</code></code> | ||
| Dynamic memory deallocation | | Dynamic memory deallocation | ||
Line 145: | Line 133: | ||
| → | | → | ||
|- | |- | ||
| 16 | |rowspan=8| 16 | ||
| <code>… ? … : …</code> | | <code>… ? … : …</code> | ||
| Ternary conditional | | Ternary conditional | ||
| ← | | ← | ||
|- | |- | ||
| <code>throw</code> | | <code>throw</code> | ||
| throw operator | | throw operator | ||
| ← | | ← | ||
|- | |- | ||
| <code>co_yield</code> | | <code>co_yield</code> | ||
| yield-expression | | yield-expression | ||
| ← | | ← | ||
|- | |- | ||
| <code>=</code> | | <code>=</code> | ||
| Direct assignment (provided by default for C++ classes) | | Direct assignment (provided by default for C++ classes) | ||
| ← | | ← | ||
|- | |- | ||
| <code>+=</code> <code>-=</code> | | <code>+=</code> <code>-=</code> | ||
| Compound assignment by sum and difference | | Compound assignment by sum and difference | ||
| ← | | ← | ||
|- | |- | ||
| <code>*=</code> <code>/=</code> <code>%=</code> | | <code>*=</code> <code>/=</code> <code>%=</code> | ||
| Compound assignment by product, quotient, and remainder | | Compound assignment by product, quotient, and remainder | ||
| ← | | ← | ||
|- | |- | ||
| <code><<=</code> <code>>>=</code> | | <code><<=</code> <code>>>=</code> | ||
| Compound assignment by bitwise left shift and right shift | | Compound assignment by bitwise left shift and right shift | ||
| ← | | ← | ||
|- | |- | ||
| <code>&=</code> <code>^=</code> <code>|=</code> | | <code>&=</code> <code>^=</code> <code>|=</code> | ||
| Compound assignment by bitwise AND, XOR, and OR | | Compound assignment by bitwise AND, XOR, and OR | ||
Line 190: | Line 171: | ||
| → | | → | ||
|} | |} | ||
== External links == | |||
* https://en.cppreference.com/w/cpp/language/operator_precedence | |||
{{Page lang|en}} | |||
[[Category:Notes]] | |||
[[Category:C++]] | |||
[[Category:Programming languages]] | |||
[[Category:Computer-related cheatsheets]] |
Latest revision as of 20:42, 15 April 2025
Precedence | Operator(s) | Description | Ass. |
---|---|---|---|
1 | ::
|
Scope resolution | → |
2 | a++ a--
|
Suffix/postfix increment and decrement | → |
type() type{}
|
Functional cast | → | |
a()
|
Function call | → | |
a[]
|
Subscript | → | |
. ->
|
Member access | → | |
3 | ++a --a
|
Prefix increment and decrement | ← |
+a -a
|
Unary plus and minus | ← | |
! ~
|
Logical NOT and bitwise NOT | ← | |
(type)
|
C-style cast | ← | |
*a
|
Indirection (dereference) | ← | |
&a
|
Address-of | ← | |
sizeof
|
Size-of | ← | |
co_await
|
await-expression | ← | |
new new[]
|
Dynamic memory allocation | ← | |
delete delete[]
|
Dynamic memory deallocation | ← | |
4 | .* ->*
|
Pointer-to-member | → |
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 | → |
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 | → |
16 | … ? … : …
|
Ternary conditional | ← |
throw
|
throw operator | ← | |
co_yield
|
yield-expression | ← | |
=
|
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 | → |