C++ operator precedence: Difference between revisions

From Thought dump
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
| →
| →
|-
|-
| 2
| <code>type()</code> <code>type{}</code>
| <code>type()</code> <code>type{}</code>
| Functional cast
| Functional cast
| →
| →
|-
|-
| 2
| <code>a()</code>
| <code>a()</code>
| Function call
| Function call
| →
| →
|-
|-
| 2
| <code>a[]</code>
| <code>a[]</code>
| Subscript
| Subscript
| →
| →
|-
|-
| 2
| <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
| ←
| ←
|-
|-
| 3
| <code>+a</code> <code>-a</code>
| <code>+a</code> <code>-a</code>
| Unary plus and minus
| Unary plus and minus
| ←
| ←
|-
|-
| 3
| <code>!</code> <code>~</code>
| <code>!</code> <code>~</code>
| Logical NOT and bitwise NOT
| Logical NOT and bitwise NOT
| ←
| ←
|-
|-
| 3
| <code>(type)</code>
| <code>(type)</code>
| C-style cast
| C-style cast
| ←
| ←
|-
|-
| 3
| <code>*a</code>
| <code>*a</code>
| Indirection (dereference)
| Indirection (dereference)
| ←
| ←
|-
|-
| 3
| <code>&amp;a</code>
| <code>&amp;a</code>
| Address-of
| Address-of
| ←
| ←
|-
|-
| 3
| <code>sizeof</code>
| <code>sizeof</code>
| Size-of
| Size-of
| ←
| ←
|-
|-
| 3
| <code>co_await</code>
| <code>co_await</code>
| await-expression
| await-expression
| ←
| ←
|-
|-
| 3
| <code>new</code> <code>new[]</code>
| <code>new</code> <code>new[]</code>
| Dynamic memory allocation
| Dynamic memory allocation
| ←
| ←
|-
|-
| 3
| <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
| ←
| ←
|-
|-
| 16
| <code>throw</code>
| <code>throw</code>
| throw operator
| throw operator
| ←
| ←
|-
|-
| 16
| <code>co_yield</code>
| <code>co_yield</code>
| yield-expression
| yield-expression
| ←
| ←
|-
|-
| 16
| <code>=</code>
| <code>=</code>
| Direct assignment (provided by default for C++ classes)
| Direct assignment (provided by default for C++ classes)
| ←
| ←
|-
|-
| 16
| <code>+=</code> <code>-=</code>
| <code>+=</code> <code>-=</code>
| Compound assignment by sum and difference
| Compound assignment by sum and difference
| ←
| ←
|-
|-
| 16
| <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
| ←
| ←
|-
|-
| 16
| <code>&lt;&lt;=</code> <code>>>=</code>
| <code>&lt;&lt;=</code> <code>>>=</code>
| Compound assignment by bitwise left shift and right shift
| Compound assignment by bitwise left shift and right shift
| ←
| ←
|-
|-
| 16
| <code>&amp;=</code> <code>^=</code> <code>|=</code>
| <code>&amp;=</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

External links