[over.oper.general] (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 have at least one function parameter or implicit object parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

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].