[over.oper] (original) (raw)

12 Overloading [over]

12.4 Overloaded operators [over.oper]

12.4.1 General [over.oper.general]

A declaration whose declarator-id is an operator-function-idshall declare a function or function template or an explicit instantiation or specialization of a function template.

A function so declared is an operator function.

A function template so declared is an operator function template.

A specialization of an operator function template is also an operator function.

An operator function is said toimplementthe operator named in itsoperator-function-id.

operator: one of
new delete new[] delete[] co_await ( ) [ ] -> ->*
~ ! + - * / % ^ &
| = += -= *= /= %= ^= &=
|= == != < > <= >= <=> &&
|| << >> <<= >>= ++ -- ,

[Note 1:

The operatorsnew[],delete[],(), and[]are formed from more than one token.

— _end note_]

Both the unary and binary forms of

can be overloaded.

Operator functions are usually not called directly; instead they are invoked to evaluate the operators they implement ([over.unary][over.inc]).

They can be explicitly called, however, using theoperator-function-idas the name of the function in the function call syntax ([expr.call]).

[Example 1: complex z = a.operator+(b); void* p = operator new(sizeof(int)*n); — _end example_]

The allocation and deallocation functions,operator new,operator new[],operator delete, andoperator delete[], are described completely in [basic.stc.dynamic].

The attributes and restrictions found in the rest of [over.oper] do not apply to them unless explicitly stated in [basic.stc.dynamic].

The co_await operator is described completely in [expr.await].

The attributes and restrictions found in the rest of [over.oper] do not apply to it unless explicitly stated in [expr.await].

An operator function shall either

It is not possible to change the precedence, grouping, or number of operands of operators.

The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for specific class types by defining operator functions that implement these operators.

Likewise, the meaning of the operators (unary) & and , (comma) can be changed for specific enumeration types.

Operator functions are inherited in the same manner as other base class functions.

An operator function shall be a prefix unary, binary, function call, subscripting, class member access, increment, or decrement operator function.

[Note 3:

The identities among certain predefined operators applied to fundamental types (for example,++a ≡ a+=1) need not hold for operator functions.

Some predefined operators, such as+=, require an operand to be an lvalue when applied to fundamental types; this is not required by operator functions.

— _end note_]

An operator function cannot have default arguments, except where explicitly stated below.

Operator functions cannot have more or fewer parameters than the number required for the corresponding operator, as described in the rest of [over.oper].

Operators not mentioned explicitly in subclauses [over.ass] through [over.inc]act as ordinary unary and binary operators obeying the rules of [over.unary] or [over.binary].

12.4.2 Unary operators [over.unary]

A prefix unary operator functionis a function named operator@for a prefix unary-operator @ ([expr.unary.op]) that is either a non-static member function ([class.mfct]) with no non-object parameters or a non-member function with one parameter.

If a member function is selected, the expression is interpreted as

Otherwise, if a non-member function is selected, the expression is interpreted as

[Note 2:

The unary and binary forms of the same operator have the same name.

Consequently, a unary operator can hide a binary operator from an enclosing scope, and vice versa.

— _end note_]

12.4.3 Binary operators [over.binary]

12.4.3.1 General [over.binary.general]

A binary operator functionis a function named operator@for a binary operator @ that is either a non-static member function ([class.mfct]) with one non-object parameter or a non-member function with two parameters.

For an expression x @ y with subexpressions x and y, the operator function is selected by overload resolution ([over.match.oper]).

If a member function is selected, the expression is interpreted as

x . operator @ ( y )

Otherwise, if a non-member function is selected, the expression is interpreted as

operator @ ( x , y )

An equality operator function is an operator function for an equality operator ([expr.eq]).

A relational operator function is an operator function for a relational operator ([expr.rel]).

A three-way comparison operator function is an operator function for the three-way comparison operator ([expr.spaceship]).

A comparison operator function is an equality operator function, a relational operator function, or a three-way comparison operator function.

12.4.3.2 Simple assignment [over.ass]

A simple assignment operator functionis a binary operator function named operator=.

A simple assignment operator function shall be a non-static member function.

[Note 1:

Because only standard conversion sequences are considered when converting to the left operand of an assignment operation ([over.best.ics]), an expression x = y with a subexpression x of class type is always interpreted as x.operator=(y).

— _end note_]

[Note 2:

Since a copy assignment operator is implicitly declared for a class if not declared by the user ([class.copy.assign]), a base class assignment operator function is always hidden by the copy assignment operator function of the derived class.

— _end note_]

[Note 3:

Any assignment operator function, even the copy and move assignment operators, can be virtual.

For a derived class D with a base class Bfor which a virtual copy/move assignment has been declared, the copy/move assignment operator in D does not overrideB's virtual copy/move assignment operator.

[Example 1: struct B { virtual int operator= (int);virtual B& operator= (const B&);};struct D : B { virtual int operator= (int);virtual D& operator= (const B&);}; D dobj1; D dobj2; B* bptr = &dobj1;void f() { bptr->operator=(99); *bptr = 99; bptr->operator=(dobj2); *bptr = dobj2; dobj1 = dobj2; } — _end example_]

— _end note_]

12.4.4 Function call [over.call]

A function call operator functionis a function named operator()that is a member function with an arbitrary number of parameters.

It may have default arguments.

If a surrogate call function is selected, let e be the result of invoking the corresponding conversion operator function on the postfix-expression;

the expression is interpreted as

Otherwise, the expression is interpreted as

12.4.5 Subscripting [over.sub]

A subscripting operator functionis a member function named operator[]with an arbitrary number of parameters.

It may have default arguments.

For an expression of the form

the operator function is selected by overload resolution ([over.match.oper]).

If a member function is selected, the expression is interpreted as

[Example 1: struct X { Z operator[](std::initializer_list<int>); Z operator[](auto...);}; X x; x[{1,2,3}] = 7; x[1,2,3] = 7; int a[10]; a[{1,2,3}] = 7; a[1,2,3] = 7; — _end example_]

12.4.6 Class member access [over.ref]

A class member access operator functionis a function named operator->that is a non-static member function taking no non-object parameters.

For an expression of the form

the operator function is selected by overload resolution ([over.match.oper]), and the expression is interpreted as

12.4.7 Increment and decrement [over.inc]

An increment operator functionis a function named operator++.

If this function is a non-static member function with no non-object parameters, or a non-member function with one parameter, it defines the prefix increment operator++for objects of that type.

If the function is a non-static member function with one non-object parameter (which shall be of typeint) or a non-member function with two parameters (the second of which shall be of typeint), it defines the postfix increment operator++for objects of that type.

When the postfix increment is called as a result of using the++operator, theintargument will have value zero.109

[Example 1: struct X { X& operator++(); X operator++(int); };struct Y { }; Y& operator++(Y&); Y operator++(Y&, int); void f(X a, Y b) { ++a; a++; ++b; b++; a.operator++(); a.operator++(0); operator++(b); operator++(b, 0); } — _end example_]

A decrement operator functionis a function named operator--and is handled analogously to an increment operator function.