C++ reference
operators
This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the fourth column “Included in C”, states whether an operator is also present in C. Note that C does not support operator overloading.
When not overloaded, for the operators &&
, ||
, and ,
(the comma operator), there is a sequence point after the evaluation of the first operand.
C++ also contains the type conversion operators const_cast
, static_cast
, dynamic_cast
, and reinterpret_cast
. The formatting of these operators means that their precedence level is unimportant.
Most of the operators available in C and C++ are also available in other languages such as C#, D, Java, Perl, and PHP with the same precedence, associativity, and semantics.
For the purposes of these tables, a
, b
, and c
represent valid values (literals, values from variables, or return value), object names, or lvalues, as appropriate. R
, S
and T
stand for any type(s), and K
for a class type or enumerated type.
Type | Name | Syntax | Can overload in C++ | Included in C | C++ prototype example: as member of K | C++ prototype example: outside class definitions |
---|---|---|---|---|---|---|
arithmetic | Assignment | a = b |
Yes | Yes | R& K::operator =(S b); |
N/A |
arithmetic | Addition | a + b |
Yes | Yes | R K::operator +(S b); |
R operator +(K a, S b); |
arithmetic | Subtraction | a - b |
Yes | Yes | R K::operator -(S b); |
R operator -(K a, S b); |
arithmetic | Unary plus (integer promotion) | +a |
Yes | Yes | R K::operator +(); |
R operator +(K a); |
arithmetic | Unary minus (additive inverse) | -a |
Yes | Yes | R K::operator -(); |
R operator -(K a); |
arithmetic | Multiplication | a * b |
Yes | Yes | R K::operator *(S b); |
R operator *(K a, S b); |
arithmetic | Division | a / b |
Yes | Yes | R K::operator /(S b); |
R operator /(K a, S b); |
arithmetic | Modulo (integer remainder)4 | a % b |
Yes | Yes | R K::operator %(S b); |
R operator %(K a, S b); |
arithmetic | Increment prefix | ++a |
Yes | Yes | R& K::operator ++(); |
R& operator ++(K& a); |
arithmetic | Increment postfix | a++ |
Yes | Yes | R K::operator ++(int); 5 |
R operator ++(K& a, int); 5 |
arithmetic | Decrement Prefix | --a |
Yes | Yes | R& K::operator --(); |
R& operator --(K& a); |
arithmetic | Decrement Postfix | a-- |
Yes | Yes | R K::operator --(int); 6 |
R operator --(K& a, int); 6 |
bitwise | Bitwise NOT | ~a compl a 7 |
Yes | Yes | R K::operator ~(); |
R operator ~(K a); |
bitwise | Bitwise AND | a & b a bitand b 7 |
Yes | Yes | R K::operator &(S b); |
R operator &(K a, S b); |
bitwise | Bitwise OR | a | b a bitor b 7 |
Yes | Yes | R K::operator |(S b); |
R operator |(K a, S b); |
bitwise | Bitwise XOR | a ^ b a xor b 7 |
Yes | Yes | R K::operator ^(S b); |
R operator ^(K a, S b); |
bitwise | Bitwise left shift 8 | a << b |
Yes | Yes | R K::operator <<(S b); |
R operator <<(K a, S b); |
bitwise | Bitwise right shift 8 9 | a >> b |
Yes | Yes | R K::operator >>(S b); |
R operator >>(K a, S b); |
logical | Logical negation (NOT) | !a not a 7 |
Yes | Yes | bool K::operator !(); |
bool operator !(K a); |
logical | Logical AND | a && b a and b 7 |
Yes | Yes | bool K::operator &&(S b); |
bool operator &&(K a, S b); |
logical | Logical OR | a || b a or b 7 |
Yes | Yes | bool K::operator ||(S b); |
bool operator ||(K a, S b); |
member and pointer | Subscript | a[b] |
Yes | Yes | R& K::operator [](S b); |
N/A |
member and pointer | Indirection (“object pointed to by a”) | *a |
Yes | Yes | R& K::operator *(); |
R& operator *(K a); |
member and pointer | Address-of (“address of a”) | &a |
Yes | Yes | R* K::operator &(); |
R* operator &(K a); |
member and pointer | Structure dereference (“member b of object pointed to by a”) | a->b |
Yes | Yes | R* K::operator ->(); 10 |
N/A |
member and pointer | Structure reference (“member b of object a”) | a.b |
No | Yes | N/A | N/A |
member and pointer | Member selected by pointer-to-member b of object pointed to by a3 | a->*b |
Yes | No | R& K::operator ->*(S b); |
R& operator ->*(K a, S b); |
member and pointer | Member of object a selected by pointer-to-member b | a.*b |
No | No | N/A | N/A |
other | Function call (see Function object) | a(a1, a2) |
Yes | Yes | R K::operator ()(S a, T b, ...); |
N/A |
other | Comma | a, b |
Yes | Yes | R K::operator ,(S b); |
R operator ,(K a, S b); |
other | Ternary conditional | a ? b : c |
No | Yes | N/A | N/A |
other | Scope resolution | a::b |
No | No | N/A | N/A |
other | User-defined literals11 (since C++11) | "a"_b |
Yes | No | N/A | R operator "" _b(T a) |
other | Size-of | sizeof (a) 12sizeof (type) |
No | Yes | N/A | N/A |
other | Size of parameter pack (since C++11) | sizeof...(Args) |
No | No | N/A | N/A |
other | Align-of (since C++11) | alignof (type) or _Alignof (type) 13 |
No | Yes | N/A | N/A |
other | Type identification | typeid (a) typeid (type) |
No | No | N/A | N/A |
other | Conversion (C-style cast) | (type) a |
No | Yes | N/A | N/A |
other | Conversion 14 | type(a) |
No | No | N/A | N/A |
other | static_cast conversion15 | static_cast<type>(a) |
Yes | No | K::operator R(); |
explicit K::operator R(); (since C++11) |
other | dynamic cast conversion | dynamic_cast<type>(a) |
No | No | N/A | N/A |
other | const_cast conversion | const_cast<type>(a) |
No | No | N/A | N/A |
other | reinterpret_cast conversion | reinterpret_cast<type>(a) |
No | No | N/A | N/A |
other | Allocate storage | new type |
Yes | No | void* K::operator new(size_t x); |
void* operator new(size_t x); |
other | Allocate storage (array) | new type[n] |
Yes | No | void* K::operator new[](size_t a); |
void* operator new[](size_t a); |
other | Deallocate storage | delete a |
Yes | No | void K::operator delete(void *a); |
void operator delete(void *a); |
other | Deallocate storage (array) | delete[] a |
Yes | No | void K::operator delete[](void *a); |
void operator delete[](void *a); |
other | Exception check (since C++11) | noexcept(a) |
No | No | N/A | N/A |
relational | Equal to | a == b |
Yes | Yes | bool K::operator ==(S const& b); |
bool operator ==(K const& a, S const& b); |
relational | Not equal to | a != b a not_eq b 7 |
Yes | Yes | bool K::operator !=(S const& b); bool K::operator !=(S const& b) const; |
bool operator !=(K const& a, S const& b); |
relational | Greater than | a > b |
Yes | Yes | bool K::operator >(S const& b) const; |
bool operator >(K const& a, S const& b); |
relational | Less than | a < b |
Yes | Yes | bool K::operator <(S const& b)const; |
bool operator <(K const& a, S const& b); |
relational | Greater than or equal to | a >= b |
Yes | Yes | bool K::operator >=(S const& b) const; |
bool operator >=(K const& a, S const& b); |
relational | Less than or equal to | a <= b |
Yes | Yes | bool K::operator <=(S const& b); |
bool operator <=(K const& a, S const& b); |
augmented assignment operators
In C, C++, and C#, the assignment operator is =
, which is augmented as follows:
Operator` | Description |
---|---|
+= |
Addition |
-= |
Subtraction |
*= |
Multiplication |
/= |
Division |
%= |
Modulus |
<<= |
Left bit shift |
>>= |
Right bit shift |
&= |
Bitwise AND |
^= |
Bitwise exclusive OR |
|= |
Bitwise inclusive OR |
licensing
Some rights reserved: CC BY-SA 3.0. Includes significant content from Augmented assignment§C descendants and Operators in C and C++ on Wikipedia, with changes made.
-
Explicit type conversion in C++ ↩
-
Meyers, Scott (Oct 1999), "Implementing operator->* for Smart Pointers" (PDF), Dr.Dobbs, Aristeia. ↩
-
The modulus operator works just with integer operands, for floating point numbers a library function must be used instead (like
fmod
). ↩ -
C++ uses the unnamed dummy-parameter
int
to differentiate between prefix and postfix increment operators. ↩↩ -
C++ uses the unnamed dummy-parameter
int
to differentiate between prefix and postfix decrement operators. ↩↩ -
In the context of iostreams, writers often will refer to
<<
and>>
as the “put-to” or “stream insertion” and “get-from” or “stream extraction” operators, respectively. ↩↩ -
According to the C99 standard, the right shift of a negative number is implementation defined. Most implementations, e.g., the GCC,1 use an arithmetic shift (i.e., sign extension), but a logical shift is possible. ↩
-
The return type of
operator->()
must be a type for which the->
operation can be applied, such as a pointer type. Ifx
is of typeC
whereC
overloadsoperator->()
,x->y
gets expanded tox.operator->()->y
. ↩ -
About C++11 User-defined literals ↩
-
The parentheses are not necessary when taking the size of a value, only when taking the size of a type. However, they are usually used regardless. ↩
-
C++ defines
alignof
operator, whereas C defines_Alignof
. Both operators have the same semantics. ↩ -
For prototype examples, behaves like
const_cast/static_cast/reinterpret_cast
2 ↩ -
For prototype examples, for user-defined conversions, the return type implicitly and necessarily matches the operator name. ↩