General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
| ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
General | |||||||||||||||||||||||||||||||||||||||||||||||||||
Literals | |||||||||||||||||||||||||||||||||||||||||||||||||||
Operators | |||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||
Conversions | |||||||||||||||||||||||||||||||||||||||||||||||||||
Customizes the C++ operators for operands of user-defined types.
Contents |
Operator functions arefunctions with special function names:
operator op | (1) | ||||||||
operator new operator new [] | (2) | ||||||||
operator delete operator delete [] | (3) | ||||||||
operator co_await | (4) | (since C++20) | |||||||
op | - | any of the following operators:+-*/%^&|~!=<>+=-=*=/=%=^=&=|=<<>>>>=<<===!=<=>=<=>(since C++20)&&||++--,->*->()[] |
The behaviors of non-punctuation operators are described in their own respective pages. Unless otherwise specified, the remaining description in this page does not apply to these functions.
When an operator appears in anexpression, and at least one of its operands has aclass type or anenumeration type, thenoverload resolution is used to determine the user-defined function to be called among all the functions whose signatures match the following:
Expression | As member function | As non-member function | Example |
---|---|---|---|
@a | (a).operator@ ( ) | operator@ (a) | !std::cin callsstd::cin.operator!() |
a@b | (a).operator@ (b) | operator@ (a, b) | std::cout<<42 callsstd::cout.operator<<(42) |
a=b | (a).operator= (b) | cannot be non-member | Givenstd::string s;,s="abc"; callss.operator=("abc") |
a(b...) | (a).operator()(b...) | cannot be non-member | Givenstd::random_device r;,auto n= r(); callsr.operator()() |
a[b...] | (a).operator[](b...) | cannot be non-member | Givenstd::map<int,int> m;,m[1]=2; callsm.operator[](1) |
a-> | (a).operator->( ) | cannot be non-member | Givenstd::unique_ptr<S> p;,p->bar() callsp.operator->() |
a@ | (a).operator@ (0) | operator@ (a, 0) | Givenstd::vector<int>::iterator i;,i++ callsi.operator++(0) |
In this table, |
In addition, for comparison operators==,!=,<,>,<=,>=,<=>, overload resolution also considers therewritten candidatesoperator== oroperator<=>. | (since C++20) |
Overloaded operators (but not the built-in operators) can be called using function notation:
std::string str="Hello, ";str.operator+=("world");// same as str += "world";operator<<(operator<<(std::cout, str),'\n');// same as std::cout << str << '\n';// (since C++17) except for sequencing
Static overloaded operatorsOverloaded operators that are member functions can be declaredstatic. However, this is only allowed foroperator() andoperator[]. Such operators can be called using function notation. However, when these operators appear in expressions, they still require an object of class type. struct SwapThem{template<typename T>staticvoid operator()(T& lhs, T& rhs){ std::ranges::swap(lhs, rhs);} template<typename T>staticvoid operator[](T& lhs, T& rhs){ std::ranges::swap(lhs, rhs);}};inlineconstexpr SwapThem swap_them{}; void foo(){int a=1, b=2; swap_them(a, b);// OK swap_them[a, b];// OK SwapThem{}(a, b);// OK SwapThem{}[a, b];// OK SwapThem::operator()(a, b);// OK SwapThem::operator[](a, b);// OK SwapThem(a, b);// error, invalid construction SwapThem[a, b];// error} | (since C++23) |
::
(scope resolution),.
(member access),.*
(member access through pointer to member), and?:
(ternary conditional) cannot be overloaded.**
,<>
, or&|
cannot be created.->
must either return a raw pointer, or return an object (by reference or by value) for which operator->
is in turn overloaded.&&
and||
lose short-circuit evaluation.
| (until C++17) |
Besides the restrictions above, the language puts no other constraints on what the overloaded operators do, or on the return type (it does not participate in overload resolution), but in general, overloaded operators are expected to behave as similar as possible to the built-in operators:operator+ is expected to add, rather than multiply its arguments,operator= is expected to assign, etc. The related operators are expected to behave similarly (operator+ andoperator+= do the same addition-like operation). The return types are limited by the expressions in which the operator is expected to be used: for example, assignment operators return by reference to make it possible to writea= b= c= d, because the built-in operators allow that.
Commonly overloaded operators have the following typical, canonical forms:[1]
The assignment operatoroperator= has special properties: seecopy assignment andmove assignment for details.
The canonical copy-assignment operator is expected tobe safe on self-assignment, and to return the lhs by reference:
// copy assignmentT& operator=(const T& other){// Guard self assignmentif(this==&other)return*this; // assume *this manages a reusable resource, such as a heap-allocated buffer mArrayif(size!= other.size)// resource in *this cannot be reused{ temp= newint[other.size];// allocate resource, if throws, do nothing delete[] mArray;// release resource in *this mArray= temp; size= other.size;} std::copy(other.mArray, other.mArray+ other.size, mArray);return*this;}
The canonical move assignment is expected toleave the moved-from object in valid state (that is, a state with class invariants intact), and eitherdo nothing or at least leave the object in a valid state on self-assignment, and return the lhs by reference to non-const, and be noexcept: // move assignmentT& operator=(T&& other)noexcept{// Guard self assignmentif(this==&other)return*this;// delete[]/size=0 would also be ok delete[] mArray;// release resource in *this mArray=std::exchange(other.mArray, nullptr);// leave other in valid state size=std::exchange(other.size,0);return*this;} | (since C++11) |
In those situations where copy assignment cannot benefit from resource reuse (it does not manage a heap-allocated array and does not have a (possibly transitive) member that does, such as a memberstd::vector orstd::string), there is a popular convenient shorthand: the copy-and-swap assignment operator, which takes its parameter by value (thus working as both copy- and move-assignment depending on the value category of the argument), swaps with the parameter, and lets the destructor clean it up.
// copy assignment (copy-and-swap idiom)T& T::operator=(T other)noexcept// call copy or move constructor to construct other{std::swap(size, other.size);// exchange resources between *this and otherstd::swap(mArray, other.mArray);return*this;}// destructor of other is called to release the resources formerly managed by *this
This form automatically providesstrong exception guarantee, but prohibits resource reuse.
The overloads ofoperator>>
andoperator<<
that take astd::istream& orstd::ostream& as the left hand argument are known as insertion and extraction operators. Since they take the user-defined type as the right argument (b
ina @ b
), they must be implemented as non-members.
std::ostream& operator<<(std::ostream& os,const T& obj){// write obj to streamreturn os;} std::istream& operator>>(std::istream& is, T& obj){// read obj from streamif(/* T could not be constructed */) is.setstate(std::ios::failbit);return is;}
These operators are sometimes implemented asfriend functions.
When a user-defined class overloads the function call operatoroperator(), it becomes aFunctionObject type.
An object of such a type can be used in a function call expression:
// An object of this type represents a linear function of one variable a * x + b.struct Linear{double a, b; double operator()(double x)const{return a* x+ b;}}; int main(){ Linear f{2,1};// Represents function 2x + 1. Linear g{-1,0};// Represents function -x.// f and g are objects that can be used like a function. double f_0= f(0);double f_1= f(1); double g_0= g(0);}
Many standard libraryalgorithms acceptFunctionObjects to customize behavior. There are no particularly notable canonical forms ofoperator(), but to illustrate the usage:
#include <algorithm>#include <iostream>#include <vector> struct Sum{int sum=0;void operator()(int n){ sum+= n;}}; int main(){std::vector<int> v={1,2,3,4,5}; Sum s=std::for_each(v.begin(), v.end(), Sum());std::cout<<"The sum is "<< s.sum<<'\n';}
Output:
The sum is 15
When the postfix increment or decrement operator appears in an expression, the corresponding user-defined function (operator++ oroperator--) is called with an integer argument0. Typically, it is declared asT operator++(int) orT operator--(int), where the argument is ignored. The postfix increment and decrement operators are usually implemented in terms of the prefix versions:
struct X{// prefix increment X& operator++(){// actual increment takes place herereturn*this;// return new value by reference} // postfix increment X operator++(int){ X old=*this;// copy old value operator++();// prefix incrementreturn old;// return old value} // prefix decrement X& operator--(){// actual decrement takes place herereturn*this;// return new value by reference} // postfix decrement X operator--(int){ X old=*this;// copy old value operator--();// prefix decrementreturn old;// return old value}};
Although the canonical implementations of the prefix increment and decrement operators return by reference, as with any operator overload, the return type is user-defined; for example the overloads of these operators forstd::atomic return by value.
Binary operators are typically implemented as non-members to maintain symmetry (for example, when adding a complex number and an integer, ifoperator+ is a member function of the complex type, then onlycomplex+ integer would compile, and notinteger+ complex). Since for every binary arithmetic operator there exists a corresponding compound assignment operator, canonical forms of binary operators are implemented in terms of their compound assignments:
class X{public: X& operator+=(const X& rhs)// compound assignment (does not need to be a member,{// but often is, to modify the private members)/* addition of rhs to *this takes place here */return*this;// return the result by reference} // friends defined inside class body are inline and are hidden from non-ADL lookupfriend X operator+(X lhs,// passing lhs by value helps optimize chained a+b+cconst X& rhs)// otherwise, both parameters may be const references{ lhs+= rhs;// reuse compound assignmentreturn lhs;// return the result by value (uses move constructor)}};
Standard library algorithms such asstd::sort and containers such asstd::set expectoperator< to be defined, by default, for the user-provided types, and expect it to implement strict weak ordering (thus satisfying theCompare requirements). An idiomatic way to implement strict weak ordering for a structure is to use lexicographical comparison provided bystd::tie:
struct Record{std::string name;unsignedint floor;double weight; friendbool operator<(const Record& l,const Record& r){returnstd::tie(l.name, l.floor, l.weight)<std::tie(r.name, r.floor, r.weight);// keep the same order}};
Typically, onceoperator< is provided, the other relational operators are implemented in terms ofoperator<.
inlinebool operator<(const X& lhs,const X& rhs){/* do actual comparison */}inlinebool operator>(const X& lhs,const X& rhs){return rhs< lhs;}inlinebool operator<=(const X& lhs,const X& rhs){return!(lhs> rhs);}inlinebool operator>=(const X& lhs,const X& rhs){return!(lhs< rhs);}
Likewise, the inequality operator is typically implemented in terms ofoperator==:
inlinebool operator==(const X& lhs,const X& rhs){/* do actual comparison */}inlinebool operator!=(const X& lhs,const X& rhs){return!(lhs== rhs);}
When three-way comparison (such asstd::memcmp orstd::string::compare) is provided, all six two-way comparison operators may be expressed through that:
inlinebool operator==(const X& lhs,const X& rhs){return cmp(lhs,rhs)==0;}inlinebool operator!=(const X& lhs,const X& rhs){return cmp(lhs,rhs)!=0;}inlinebool operator<(const X& lhs,const X& rhs){return cmp(lhs,rhs)<0;}inlinebool operator>(const X& lhs,const X& rhs){return cmp(lhs,rhs)>0;}inlinebool operator<=(const X& lhs,const X& rhs){return cmp(lhs,rhs)<=0;}inlinebool operator>=(const X& lhs,const X& rhs){return cmp(lhs,rhs)>=0;}
User-defined classes that provide array-like access that allows both reading and writing typically define two overloads foroperator[]: const and non-const variants:
struct T{ value_t& operator[](std::size_t idx){return mVector[idx];}const value_t& operator[](std::size_t idx)const{return mVector[idx];}};
Alternatively, they can be expressed as a single member function template using anexplicit object parameter: struct T{ decltype(auto) operator[](thisauto& self,std::size_t idx){return self.mVector[idx];}}; | (since C++23) |
If the value type is known to be a scalar type, the const variant should return by value.
Where direct access to the elements of the container is not wanted or not possible or distinguishing between lvaluec[i]= v; and rvaluev= c[i]; usage,operator[] may return a proxy. See for examplestd::bitset::operator[].
operator[] can only take one subscript. In order to provide multidimensional array access semantics, e.g. to implement a 3D array accessa[i][j][k]= x;,operator[] has to return a reference to a 2D plane, which has to have its ownoperator[] which returns a reference to a 1D row, which has to haveoperator[] which returns a reference to the element. To avoid this complexity, some libraries opt for overloadingoperator() instead, so that 3D access expressions have the Fortran-like syntaxa(i, j, k)= x;. | (until C++23) |
operator[] can take any number of subscripts. For example, anoperator[] of a 3D array class declared asT& operator[](std::size_t x,std::size_t y,std::size_t z); can directly access the elements. Run this code #include <array>#include <cassert>#include <iostream> template<typename T,std::size_t Z,std::size_t Y,std::size_t X>struct Array3d{std::array<T, X* Y* Z> m{}; constexpr T& operator[](std::size_t z,std::size_t y,std::size_t x)// C++23{assert(x< X and y< Y and z< Z);return m[z* Y* X+ y* X+ x];}}; int main(){ Array3d<int,4,3,2> v; v[3,2,1]=42;std::cout<<"v[3, 2, 1] = "<< v[3,2,1]<<'\n';} Output: v[3, 2, 1] = 42 | (since C++23) |
User-defined classes and enumerations that implement the requirements ofBitmaskType are required to overload the bitwise arithmetic operatorsoperator&,operator|,operator^,operator~,operator&=,operator|=, andoperator^=, and may optionally overload the shift operatorsoperator<<operator>>,operator>>=, andoperator<<=. The canonical implementations usually follow the pattern for binary arithmetic operators described above.
The operatoroperator! is commonly overloaded by the user-defined classes that are intended to be used in boolean contexts. Such classes also provide a user-defined conversion function to boolean type (seestd::basic_ios for the standard library example), and the expected behavior ofoperator! is to return the value opposite ofoperatorbool. | (until C++11) |
Since the built-in operator! performscontextual conversion tobool, user-defined classes that are intended to be used in boolean contexts could provide onlyoperatorbool and need not overloadoperator!. | (since C++11) |
The following operators are rarely overloaded:
CComPtrBase
. An example of this operator's use in EDSL can be found inboost.spirit.Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_static_call_operator | 202207L | (C++23) | staticoperator() |
__cpp_multidimensional_subscript | 202211L | (C++23) | staticoperator[] |
#include <iostream> class Fraction{// or C++17's std::gcdconstexprint gcd(int a,int b){return b==0? a: gcd(b, a% b);} int n, d;public:constexpr Fraction(int n,int d=1): n(n/ gcd(n, d)), d(d/ gcd(n, d)){} constexprint num()const{return n;}constexprint den()const{return d;} constexpr Fraction& operator*=(const Fraction& rhs){int new_n= n* rhs.n/ gcd(n* rhs.n, d* rhs.d); d= d* rhs.d/ gcd(n* rhs.n, d* rhs.d); n= new_n;return*this;}}; std::ostream& operator<<(std::ostream& out,const Fraction& f){return out<< f.num()<<'/'<< f.den();} constexprbool operator==(const Fraction& lhs,const Fraction& rhs){return lhs.num()== rhs.num()&& lhs.den()== rhs.den();} constexprbool operator!=(const Fraction& lhs,const Fraction& rhs){return!(lhs== rhs);} constexpr Fraction operator*(Fraction lhs,const Fraction& rhs){return lhs*= rhs;} int main(){constexpr Fraction f1{3,8}, f2{1,2}, f3{10,2};std::cout<< f1<<" * "<< f2<<" = "<< f1* f2<<'\n'<< f2<<" * "<< f3<<" = "<< f2* f3<<'\n'<<2<<" * "<< f1<<" = "<<2* f1<<'\n'; static_assert(f3== f2*10);}
Output:
3/8 * 1/2 = 3/161/2 * 5/1 = 5/22 * 3/8 = 3/4
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1481 | C++98 | the non-member prefix increment operator could only have a parameter of class type, enumeration type, or a reference type to such types | no type requirement |
CWG 2931 | C++23 | explicit object member operator functions could only have no parameter of class type, enumeration type, or a reference type to such types | prohibited |
Common operators | ||||||
---|---|---|---|---|---|---|
assignment | increment decrement | arithmetic | logical | comparison | member access | other |
a= b | ++a | +a | !a | a== b | a[...] | function call a(...) |
comma a, b | ||||||
conditional a? b: c | ||||||
Special operators | ||||||
static_cast converts one type to another related type |
|