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

101 lines
4.7 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.binary]
# 12 Overloading [[over]](./#over)
## 12.4 Overloaded operators [[over.oper]](over.oper#over.binary)
### 12.4.3 Binary operators [over.binary]
#### [12.4.3.1](#general) General [[over.binary.general]](over.binary.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3530)
A [*binary operator function*](#def:operator_function,binary "12.4.3.1General[over.binary.general]") is a function named operator@ for a binary operator @ that is either
a non-static member function ([[class.mfct]](class.mfct "11.4.2Member functions")) with one non-object parameter or
a non-member function with two parameters[.](#general-1.sentence-1)
For an expression x @ y with subexpressions x and y,
the operator function is selected by overload resolution ([[over.match.oper]](over.match.oper "12.2.2.3Operators in expressions"))[.](#general-1.sentence-2)
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 )
[2](#general-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3550)
An [*equality operator function*](#def:operator_function,equality "12.4.3.1General[over.binary.general]") is an operator function
for an equality operator ([[expr.eq]](expr.eq "7.6.10Equality operators"))[.](#general-2.sentence-1)
A [*relational operator function*](#def:operator_function,relational "12.4.3.1General[over.binary.general]") is an operator function
for a relational operator ([[expr.rel]](expr.rel "7.6.9Relational operators"))[.](#general-2.sentence-2)
A [*three-way comparison operator function*](#def:operator_function,three-way_comparison "12.4.3.1General[over.binary.general]") is an operator function
for the three-way comparison operator ([[expr.spaceship]](expr.spaceship "7.6.8Three-way comparison operator"))[.](#general-2.sentence-3)
A [*comparison operator function*](#def:operator_function,comparison "12.4.3.1General[over.binary.general]") is
an equality operator function,
a relational operator function, or
a three-way comparison operator function[.](#general-2.sentence-4)
#### [12.4.3.2](#over.assign) Simple assignment [[over.assign]](over.assign)
[1](#over.assign-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3566)
A [*simple assignment operator function*](#def:operator_function,simple_assignment "12.4.3.2Simple assignment[over.assign]") is a binary operator function named operator=[.](#over.assign-1.sentence-1)
A simple assignment operator function shall be a non-static member function[.](#over.assign-1.sentence-2)
[*Note [1](#over.assign-note-1)*:
Because only standard conversion sequences are considered when converting
to the left operand of an assignment operation ([[over.best.ics]](over.best.ics "12.2.4.2Implicit conversion sequences")),
an expression x = y with a subexpression x of class type
is always interpreted as x.operator=(y)[.](#over.assign-1.sentence-3)
— *end note*]
[2](#over.assign-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3577)
[*Note [2](#over.assign-note-2)*:
Since a copy assignment operator is implicitly declared for a class
if not declared by the user ([[class.copy.assign]](class.copy.assign "11.4.6Copy/move assignment operator")),
a base class assignment operator function is always hidden by
the copy assignment operator function of the derived class[.](#over.assign-2.sentence-1)
— *end note*]
[3](#over.assign-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3585)
[*Note [3](#over.assign-note-3)*:
Any assignment operator function, even the copy and move assignment operators,
can be virtual[.](#over.assign-3.sentence-1)
For a derived class D with a base class B for 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[.](#over.assign-3.sentence-2)
[*Example [1](#over.assign-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); // calls D::operator=(int)*bptr = 99; // ditto bptr->operator=(dobj2); // calls D::operator=(const B&)*bptr = dobj2; // ditto dobj1 = dobj2; // calls implicitly-declared D::operator=(const D&)} — *end example*]
— *end note*]