A | B | C | D | E | F | G | H | CH | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
This is a list of operators in the C and C++ programming languages. All the operators (except typeof) listed exist in C++; the 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 C-family languages such as C#, D, Java, Perl, and PHP with the same precedence, associativity, and semantics.
Table
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. Some of the operators have alternative spellings using digraphs and trigraphs or operator synonyms.
Arithmetic operators
All arithmetic operators exist in C and C++ and can be overloaded in C++.
Operator name | Syntax | C++ prototype examples | ||
---|---|---|---|---|
As member of K | Outside class definitions | |||
Addition | a + b
|
R K::operator +(S b);
|
R operator +(K a, S b);
| |
Subtraction | a - b
|
R K::operator -(S b);
|
R operator -(K a, S b);
| |
Unary plus (integer promotion) | +a
|
R K::operator +();
|
R operator +(K a);
| |
Unary minus (additive inverse) | -a
|
R K::operator -();
|
R operator -(K a);
| |
Multiplication | a * b
|
R K::operator *(S b);
|
R operator *(K a, S b);
| |
Division | a / b
|
R K::operator /(S b);
|
R operator /(K a, S b);
| |
Modulo (integer remainder)[a] | a % b
|
R K::operator %(S b);
|
R operator %(K a, S b);
| |
Increment | Prefix | ++a
|
R& K::operator ++();
|
R& operator ++(K& a);
|
Postfix | a++
|
R K::operator ++(int);
|
R operator ++(K& a, int);
| |
Note: C++ uses the unnamed dummy-parameter int to differentiate between prefix and postfix increment operators.
| ||||
Decrement | Prefix | --a
|
R& K::operator --();
|
R& operator --(K& a);
|
Postfix | a--
|
R K::operator --(int);
|
R operator --(K& a, int);
| |
Note: C++ uses the unnamed dummy-parameter int to differentiate between prefix and postfix decrement operators.
|
Comparison operators/relational operators
All comparison operators can be overloaded in C++. Since C++20, the inequality operator is automatically generated if operator==
is defined and all four relational operators are automatically generated if operator<=>
is defined.[1]
Operator name | Syntax | Included in C |
Prototype examples | ||
---|---|---|---|---|---|
As member of K | Outside class definitions | ||||
Equal to | a == b
|
Yes | bool K::operator ==(S const& b) const;
|
bool operator ==(K const& a, S const& b);
| |
Not equal to | a != b |
Yes | bool K::operator !=(S const& b) const;
|
bool operator !=(K const& a, S const& b);
| |
Greater than | a > b |
Yes | bool K::operator >(S const& b) const;
|
bool operator >(K const& a, S const& b);
| |
Less than | a < b |
Yes | bool K::operator <(S const& b) const;
|
bool operator <(K const& a, S const& b);
| |
Greater than or equal to | a >= b |
Yes | bool K::operator >=(S const& b) const;
|
bool operator >=(K const& a, S const& b);
| |
Less than or equal to | a <= b |
Yes | bool K::operator <=(S const& b) const;
|
bool operator <=(K const& a, S const& b);
| |
Three-way comparison[b] | a <=> b |
No | auto K::operator <=>(const S &b);
|
auto operator <=>(const K &a, const S &b);
| |
The operator has a total of 3 possible return types: std::weak_ordering , std::strong_ordering and std::partial_ordering to which they all are convertible to.
|
Logical operators
All logical operators exist in C and C++ and can be overloaded in C++, albeit the overloading of the logical AND and logical OR is discouraged, because as overloaded operators they behave as ordinary function calls, which means that both of their operands are evaluated, so they lose their well-used and expected short-circuit evaluation property.[2]
Operator name | Syntax | C++ prototype examples | ||
---|---|---|---|---|
As member of K | Outside class definitions | |||
Logical negation (NOT) | !a
|
bool K::operator !();
|
bool operator !(K a);
| |
Logical AND | a && b
|
bool K::operator &&(S b);
|
bool operator &&(K a, S b);
| |
Logical OR | a || b
|
bool K::operator ||(S b);
|
bool operator ||(K a, S b);
|
Bitwise operators
All bitwise operators exist in C and C++ and can be overloaded in C++.
Operator name | Syntax | Prototype examples | ||
---|---|---|---|---|
As member of K | Outside class definitions | |||
Bitwise NOT | ~a |
R K::operator ~();
|
R operator ~(K a);
| |
Bitwise AND | a & b
|
R K::operator &(S b);
|
R operator &(K a, S b);
| |
Bitwise OR | a | b
|
R K::operator |(S b);
|
R operator |(K a, S b);
| |
Bitwise XOR | a ^ b
|
R K::operator ^(S b);
|
R operator ^(K a, S b);
| |
Bitwise left shift[c] | a << b
|
R K::operator <<(S b);
|
R operator <<(K a, S b);
| |
Bitwise right shift[c][d] | a >> b
|
R K::operator >>(S b);
|
R operator >>(K a, S b);
|
Assignment operators
All assignment expressions exist in C and C++ and can be overloaded in C++.
For the given operators the semantic of the built-in combined assignment expression a ⊚= b
is equivalent to a = a ⊚ b
, except that a
is evaluated only once.
Operator name | Syntax | C++ prototype examples | |
---|---|---|---|
As member of K | Outside class definitions | ||
Direct assignment | a = b
|
R& K::operator =(S b);
|
— |
Addition assignment | a += b
|
R& K::operator +=(S b);
|
R& operator +=(K& a, S b);
|
Subtraction assignment | a -= b
|
R& K::operator -=(S b);
|
R& operator -=(K& a, S b);
|
Multiplication assignment | a *= b
|
R& K::operator *=(S b);
|
R& operator *=(K& a, S b);
|
Division assignment | a /= b
|
R& K::operator /=(S b);
|
R& operator /=(K& a, S b);
|
Modulo assignment | a %= b
|
R& K::operator %=(S b);
|
R& operator %=(K& a, S b);
|
Bitwise AND assignment | a &= b
|
R& K::operator &=(S b);
|
R& operator &=(K& a, S b);
|
Bitwise OR assignment | a |= b
|
R& K::operator |=(S b);
|
R& operator |=(K& a, S b);
|
Bitwise XOR assignment | a ^= b
|
R& K::operator ^=(S b);
|
R& operator ^=(K& a, S b);
|
Bitwise left shift assignment | a <<= b
|
R& K::operator <<=(S b);
|
R& operator <<=(K& a, S b);
|
Bitwise right shift assignment[d] | a >>= b
|
R& K::operator >>=(S b);
|
R& operator >>=(K& a, S b);
|
Member and pointer operators
Operator name | Syntax | Can overload in C++ | Included in C |
C++ prototype examples | ||
---|---|---|---|---|---|---|
As member of K | Outside class definitions | |||||
Subscript | a
|
Yes | Yes | R& K::operator (S b); R& K::operator (S b, ...); // since C++23 |
— | |
Indirection ("object pointed to by a") | *a |
Yes | Yes | R& K::operator *();
|
R& operator *(K a);
| |
Address-of ("address of a") | &a |
Yes[e] | Yes | R* K::operator &();
|
R* operator &(K a);
| |
Structure dereference ("member b of object pointed to by a") | a->b |
Yes | Yes | R* K::operator ->(); [f] |
— | |
Structure reference ("member b of object a") | a.b |
No | Yes | — | ||
Member selected by pointer-to-member b of object pointed to by a[g] | a->*b |
Yes | No | R& K::operator ->*(S b);
|
R& operator ->*(K a, S b);
| |
Member of object a selected by pointer-to-member b | a.*b |
No | No | — |
Other operators
Operator name | Syntax | Can overload in C++ | Included in C |
Prototype examples | ||
---|---|---|---|---|---|---|
As member of K | Outside class definitions | |||||
Function call See Function object. |
a(a1, a2)
|
Yes | Yes | R K::operator ()(S a, T b, ...);
|
— | |
Comma | a, b |
Yes | Yes | R K::operator ,(S b);
|
R operator ,(K a, S b);
| |
Ternary conditional | a ? b : c |
No | Yes | — | ||
Scope resolution | a::b |
No | No | — | ||
User-defined literals[h] since C++11 |
"a"_b |
Yes | No | — | R operator "" _b(T a)
| |
Sizeof | sizeof a [i]sizeof (R) |
No | Yes | — | ||
Size of parameter pack since C++11 |
sizeof...(Args) |
No | No | — | ||
Alignof since C++11 |
alignof(R) or _Alignof(R) [j] |
No | Yes | — | ||
Typeof since C23 |
typeof (a) typeof (R) typeof_unqual (a) typeof_unqual (R) |
— | Only[k] | — | ||
Decltype since C++11 |
decltype (a) decltype (R) |
No | No | — | ||
Type identification | typeid(a) typeid(R) |
No | No | — | ||
Conversion (C-style cast) | (R)a |
Yes | Yes | K::operator R(); [4]
|
— | |
Conversion | R(a) R{a} since C++11auto(a) since C++23auto{a} since C++23 |
No | No | Note: behaves like const_cast/static_cast/reinterpret_cast. In the last two cases the auto specifier is replaced with the type of the invented variable x declared with auto x(a); (which is never interpreted as a function declaration) or auto x{a}; , respectively. [5]
| ||
static_cast conversion | static_cast<R>(a) |
Yes | No | K::operator R(); explicit K::operator R(); since C++11
|
— | |
Note: for user-defined conversions, the return type implicitly and necessarily matches the operator name unless the type is inferred (e.g. operator auto() , operator decltype(auto)() etc.).
| ||||||
dynamic cast conversion | dynamic_cast<R>(a) |
No | No | — | ||
const_cast conversion | const_cast<R>(a) |
No | No | — | ||
reinterpret_cast conversion | reinterpret_cast<R>(a) |
No | No | — | ||
Allocate storage | new R [l] |
Yes | No | void* K::operator new(size_t x);
|
void* operator new(size_t x);
|