Other than 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+ and operator+= 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 write a=b=c=d, because the built-in operators allow that.
Commonly overloaded operators have the following typical, canonical forms:[1]

The assignment operator (operator=) has special properties: see copy assignment and move_assignment for details. To summarize, the canonical "universal assignment operator" implementation is

T& T::operator=(T arg){// copy/move constructor is called to construct arg
swap(arg);// resources exchanged between *this and argreturn*this;}// destructor is called to release the resources formerly held by *this

When there are resources that can be reused in assignment, for example, if the class owns a heap-allocated array, then copy-assignment between arrays of the same size can avoid allocation and deallocation:

The overloads of operator>> and operator<< that take a std::istream& or std::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 in a@b), they must be implemented as non-members.

When a user-defined class overloads the function call operator, operator(), it becomes a FunctionObject type. Many standard algorithms, from std::sort to std::accumulate accept objects of such types to customize behavior. There are no particularly notable canonical forms of operator(), but to illustrate the usage

When the postfix increment and decrement appear in an expression, the corresponding user-defined function (operator++ or operator--) is called with an integer argument 0. Typically, it is implemented as T operator++(int), where the argument is ignored. The postfix increment and decrement operator is usually implemented in terms of the prefix version:

Although canonical form of pre-increment/pre-decrement returns a reference, as with any operator overload, the return type is user-defined; for example the overloads of these operators for std::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, if operator+ is a member function of the complex type, then only complex+integer would compile, and not integer+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 first arg by value helps optimize chained a+b+cconst X& rhs)// alternatively, both parameters may be const references.{return lhs += rhs;// reuse compound assignment and return the result by value}};

Standard algorithms such as std::sort and containers such as std::set expect operator< to be defined, by default, for the user-provided types. Typically, operator< is provided and the other relational operators are implemented in terms of operator<.

If the value type is known to be a built-in 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 l-value c[i]= v; and r-value v = c[i]; usage, operator[] may return a proxy. see for example std::bitset::operator[].

To provide multidimensional array access semantics, e.g. to implement a 3D array access a[i][j][k]= x;, operator[] has to return a reference to a 2D plane, which has to have its own operator[] which returns a reference to a 1D row, which has to have operator[] which returns a reference to the element. To avoid this complexity, some libraries opt for overloading operator() instead, so that 3D access expressions have the Fortran-like syntax a(i,j,k)= x;