Here is a complete list of the C++ operators in order of decreasing precedence, and with their associativities. The number indicates the precedence and the letter the associativity. Higher numbers indicate higher precedence (tighter binding).
Those operators which cannot be overloaded are so noted; so are the operators whose overloading is somehow 'special'.
Operator | Precedence and associativity | Built-in meaning | Notes |
---|---|---|---|
:: | 20 Left | Class scope qualifier (when used as a binary operator) | Cannot be overloaded |
:: | 20 | Global scope qualifier (when used as a unary operator) | Cannot be overloaded |
() | 20 | Parentheses for grouping | Unary; cannot be overloaded |
[] | 19 Left | Subscripting | Binary; overloads as member functions only. |
() | 19 Left | Function call | Binary; RHS is the list of arguments. Overloads as member functions only. |
() | 19 Left | Type conversion in functional form ('value builder') | Binary; LHS is the type; RHS is the list of arguments. Constructors provide overloads; other member overloads are allowed. |
. | 19 Left | Member selection from object | Cannot be overloaded. (There are proposals for overloading operator. but they probably won't be accepted.) |
-> | 19 Left | Member selection from pointed-at object. | Overloadable as member function only. |
++ | 19 | Postfix increment (postincrement). | Unary; overload takes an 'unnecessary' argument. |
-- | 19 | Postfix decrement (postdecrement). | Unary; overload takes an 'unnecessary' argument. |
new | 18 | New (memory allocator) | Unary or binary; nonassociative. First argument is type; second is list of argument expressions. When overloaded as a member, represented by a static member function. |
delete | 18 | Delete (memory deallocator) | Unary; When overloaded as a member, represented by a static member function. Type is always void. |
sizeof | 17 | Size of type or object | Unary; returns size_t. Cannot be overloaded. |
++ | 17 | Pre-increment | Unary |
-- | 17 | Pre-decrement | Unary |
~ | 17 | Bitwise complement | Unary |
! | 17 | Logical NOT | Unary |
- | 17 | Negative | Unary |
+ | 17 | Unary, + for symmetry with -. Built-in operator requires operand of arithmetic type; returns value and type of its operand | |
* | 17 | Indirection (Unary) | Built-in operator requires pointer type. Complement of & |
& | 17 | Address-of (Unary) | Overloads hide the built-in operator for class, struct, or union types. Members can get pointer as this. If overloaded on an enum, the built-in operator is permanently hidden. |
() | 16 | Type conversion by cast (prefix) | Binary. First operand is type; second is expression to be converted. Can be overloaded when the equivalent functional form conversion can be. |
.* | 15 Left | Selection of member from object using pointer-to-member on the RHS | Cannot be overloaded. Should a proposal for overloading . be accepted, overloads of .* will probably be accepted as well. |
->* | 15 Left | Member selection from pointed-to object using pointer-to-member on the RHS. | Overloadable as a member function only. |
* | 14 Left | Multiplication (Binary) | |
/ | 14 Left | Division | |
% | 14 Left | Remainder | |
+ | 13 Left | Addition (Binary) | |
- | 13 Left | Subtraction (Binary) | |
<< | 12 Left | Left shift | LHS shifted left bitwise by RHS. Undefined if RHS is negative. (The stream I/O system overloads this operator heavily.) |
>> | 12 Left | Right shift | LHS shifted right bitwise by RHS. Undefined if RHS is negative. Shift is arithmetic for signed types, logical for unsigned. (The stream I/O system overloads this operator heavily.) |
> | 11 Left | Greater-than | |
< | 11 Left | Less-than | |
>= | 11 Left | Greater-than or equal to | |
<= | 11 Left | Less-than or equal to | |
== | 10 Left | Equality | Compares the operands; returns 1 if they are equal, zero otherwise. |
!= | 10 Left | Inequality | Compares the operands; returns zero if they are equal, 1 otherwise. |
& | 9 Left | Bitwise AND (Binary) | Built-in requires integral types; returns bit-by-bit AND of operands. |
^ | 8 Left | Bitwise XOR | Built-in requires integral types; returns bit-by-bit exclusive-or of operands. |
| | 7 Left | Bitwise OR | Built-in requires integral types; returns bit-by-bit inclusive-or of operands. |
&& | 6 Left | Logical (short-circuit) AND | Short-circuit properties do not apply to overloads. |
|| | 5 Left | Logical (short-circuit) OR | Short-circuit properites do not apply to overloads. |
? : | 4 Left | Conditional Expression (Ternary) | Cannot be overloaded. C++ permits a conditional to return an lvalue (C does not) and the grammar permits an assignment in both the 'then' and 'else' expressions (C's grammar does not). |
throw | 3 Right | Throw an exception | Unary or nonary. Cannot be overloaded. Optional expression provides the object to be thrown. Right associative in the grammar, but returns void, so the associativity cannot actually be used. |
= | 2 Right | Assignment | Can be overloaded as a member only. The built-in assignment operators return a non-const lvalue (in C assignment returns a const rvalue). |
/= | 2 Right | Compound assignment:division | Can be overloaded only as a member. |
%= | 2 Right | Compound assignment:remainder | Can be overloaded only as a member. |
*= | 2 Right | Compound assignment:multiplication | Can be overloaded only as a member. |
+= | 2 Right | Compound assignment:addition | Can be overloaded only as a member. |
-= | 2 Right | Compound assignment:subtraction | Can be overloaded only as a member. |
<<= | 2 Right | Compound assignment:left shift | Can be overloaded only as a member. |
>>= | 2 Right | Compound assignment:right shift | Can be overloaded only as a member. |
&= | 2 Right | Compound assignment:bitwise AND | Can be overloaded only as a member. |
|= | 2 Right | Compound assignment:bitwise OR | Can be overloaded only as a member. |
^= | 2 Right | Compound assignment:bitwise XOR | Can be overloaded only as a member. |
, | 1 Left | Comma or voiding operator. | When overloaded, the strict left-to-right evaluation property is lost. |