Files
2025-10-25 03:02:53 +03:00

168 lines
6.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[over.oper.general]
# 12 Overloading [[over]](./#over)
## 12.4 Overloaded operators [[over.oper]](over.oper#general)
### 12.4.1 General [over.oper.general]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3338)
A declaration
whose [*declarator-id*](dcl.decl.general#nt:declarator-id "9.3.1General[dcl.decl.general]") is an [*operator-function-id*](#nt:operator-function-id "12.4.1General[over.oper.general]") shall declare a function or function template or
an explicit instantiation or specialization of a function template[.](#1.sentence-1)
A function so declared is an [*operator function*](#def:function,operator "12.4.1General[over.oper.general]")[.](#1.sentence-2)
A function template so declared is
an [*operator function template*](#def:function,operator,template "12.4.1General[over.oper.general]")[.](#1.sentence-3)
A specialization of an operator function template is also an operator function[.](#1.sentence-4)
An operator function is said to[*implement*](#def:operator,implementation "12.4.1General[over.oper.general]") the operator named in its[*operator-function-id*](#nt:operator-function-id "12.4.1General[over.oper.general]")[.](#1.sentence-5)
[operator-function-id:](#nt:operator-function-id "12.4.1General[over.oper.general]")
operator [*operator*](#nt:operator "12.4.1General[over.oper.general]")
[operator:](#nt:operator "12.4.1General[over.oper.general]") one of
new delete new[] delete[] co_await ( ) [ ] -> ->*
~ ! + - * / % ^ &
| = += -= *= /= %= ^= &=
|= == != < > <= >= <=> &&
|| << >> <<= >>= ++ -- ,
[*Note [1](#note-1)*:
The operatorsnew[],delete[],(),
and[] are formed from more than one token[.](#1.sentence-6)
The latter two operators are [function call](expr.call "7.6.1.3Function call[expr.call]") and [subscripting](expr.sub "7.6.1.2Subscripting[expr.sub]")[.](#1.sentence-7)
— *end note*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3382)
Both the unary and binary forms of
+ - * &
can be overloaded[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3389)
[*Note [2](#note-2)*:
The following operators cannot be overloaded:
. .* :: ?:
nor can the preprocessing symbols# ([[cpp.stringize]](cpp.stringize "15.7.3The # operator"))
and## ([[cpp.concat]](cpp.concat "15.7.4The ## operator"))[.](#3.sentence-1)
— *end note*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3402)
Operator functions are usually not called directly; instead they are invoked
to evaluate the operators they implement ([[over.unary]](over.unary "12.4.2Unary operators") – [[over.inc]](over.inc "12.4.7Increment and decrement"))[.](#4.sentence-1)
They can be explicitly called, however, using the[*operator-function-id*](#nt:operator-function-id "12.4.1General[over.oper.general]") as the name of the function in the function call syntax ([[expr.call]](expr.call "7.6.1.3Function call"))[.](#4.sentence-2)
[*Example [1](#example-1)*: complex z = a.operator+(b); // complex z = a+b;void* p = operator new(sizeof(int)*n); — *end example*]
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3416)
The allocation and deallocation functions,operator new,operator new[],operator delete, andoperator delete[],
are described completely in [[basic.stc.dynamic]](basic.stc.dynamic "6.8.6.5Dynamic storage duration")[.](#5.sentence-1)
The attributes and restrictions
found in the rest of [[over.oper]](over.oper "12.4Overloaded operators") do not apply to them unless explicitly
stated in [[basic.stc.dynamic]](basic.stc.dynamic "6.8.6.5Dynamic storage duration")[.](#5.sentence-2)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3427)
The co_await operator is described completely in [[expr.await]](expr.await "7.6.2.4Await")[.](#6.sentence-1)
The attributes and restrictions
found in the rest of [[over.oper]](over.oper "12.4Overloaded operators") do not apply to it unless explicitly
stated in [[expr.await]](expr.await "7.6.2.4Await")[.](#6.sentence-2)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3433)
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[.](#7.sentence-1)
It is not possible to change the precedence, grouping, or number of operands
of operators[.](#7.sentence-2)
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[.](#7.sentence-3)
Likewise, the meaning of the operators (unary) & and , (comma)
can be changed for specific enumeration types[.](#7.sentence-4)
Operator functions are inherited in the same manner as other base class
functions[.](#7.sentence-5)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3452)
An operator function shall be a
prefix unary, binary, function call, subscripting, class member access, increment, or decrement
operator function[.](#8.sentence-1)
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3457)
[*Note [3](#note-3)*:
The identities among certain predefined operators applied to fundamental types
(for example,++a ≡a+=1)
need not hold for operator functions[.](#9.sentence-1)
Some predefined operators, such as+=,
require an operand to be an lvalue when applied to fundamental types;
this is not required by operator functions[.](#9.sentence-2)
— *end note*]
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3471)
An operator function cannot have [default arguments](dcl.fct.default "9.3.4.7Default arguments[dcl.fct.default]"),
except where explicitly stated below[.](#10.sentence-1)
Operator
functions cannot have more or fewer parameters than the
number required for the corresponding operator, as
described in the rest of [[over.oper]](over.oper "12.4Overloaded operators")[.](#10.sentence-2)
[11](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3480)
Operators not mentioned explicitly in subclauses [[over.assign]](over.assign "12.4.3.2Simple assignment") through [[over.inc]](over.inc "12.4.7Increment and decrement") act as ordinary unary and binary
operators obeying the rules of [[over.unary]](over.unary "12.4.2Unary operators") or [[over.binary]](over.binary "12.4.3Binary operators")[.](#11.sentence-1)