If no operand of an operator in an expression has a type that is a class
or an enumeration, the operator is assumed to be a built-in operator
and interpreted according to [expr.compound].
Because
.,
.*,
and
::
cannot be overloaded,
these operators are always built-in operators interpreted according to
[expr.compound].
?:
cannot be overloaded, but the rules in this subclause are used to determine
the conversions to be applied to the second and third operands when they
have class or enumeration type ([expr.cond]).
β end note]
[Example 1: struct String {
String (const String&);
String (constchar*);
operatorconstchar*();
};
String operator+(const String&, const String&);
void f(){constchar* p="one"+"two"; // error: cannot add two pointers; overloaded operator+ not considered// because neither operand has class or enumeration typeint I =1+1; // always evaluates to 2 even if class or enumeration types exist// that would perform the operation.} β end example]
If either operand has a type that is a class or an enumeration, a
user-defined operator function can be declared that implements
this operator or a user-defined conversion can be necessary to
convert the operand to a type that is appropriate for a built-in
operator.
In this case, overload resolution is used to determine
which operator function or built-in operator is to be invoked to implement the
operator.
Therefore, the operator notation is first transformed
to the equivalent function-call notation as summarized in
Table 18
(where @ denotes one of the operators covered in the specified subclause).
However, the operands are sequenced in the order prescribed
for the built-in operator ([expr.compound]).
For a unary operator @
with an operand of type cv1T1,
and for a binary operator @
with a left operand of type cv1T1
and a right operand of type cv2T2,
four sets of candidate functions, designated
member candidates,
non-member candidates,
built-in candidates,
and
rewritten candidates,
are constructed as follows:
If T1 is a complete class type or a class currently being defined,
the set of member candidates is the result of a search for
operator@ in the scope of T1;
otherwise, the set of member candidates is empty.
For the operators =, [], or ->,
the set of non-member candidates is empty;
otherwise, it includes the result of unqualified lookup for
operator@
in the rewritten function call ([basic.lookup.unqual], [basic.lookup.argdep]),
ignoring all member functions.
However, if no operand has a class type, only those non-member
functions in the lookup set that have a first parameter of type
T1
or βreference to cvT1β,
when
T1
is an enumeration type,
or (if there is a right operand) a second parameter of type
T2
or βreference to cvT2β,
when
T2
is an enumeration type,
are candidate functions.
For the operator
,,
the unary operator
&,
or the operator
->,
the built-in candidates set is empty.
For all other operators, the built-in candidates include all
of the candidate operator functions defined in [over.built] that,
compared to the given operator,
For the
relational ([expr.rel]) and
three-way comparison ([expr.spaceship])
operators,
the rewritten candidates also include
a synthesized candidate,
with the order of the two parameters reversed,
for each non-rewritten candidate
for the expression
y <=> x.
For the != operator ([expr.eq]),
the rewritten candidates
include all non-rewritten candidates
for the expression x == y
that are rewrite targets with first operand x (see below).
For the equality operators,
the rewritten candidates also include a synthesized candidate,
with the order of the two parameters reversed,
for each non-rewritten candidate
for the expression y == x
that is a rewrite target with first operand y.
A candidate synthesized from a member candidate has its
object parameter as the second parameter, thus implicit conversions
are considered for the first, but not for the second, parameter.
A non-template function or function template F named operator==
is a rewrite target with first operand o
unless a search for the name operator!= in the scope S
from the instantiation context of the operator expression
finds a function or function template
that would correspond ([basic.scope.scope]) to F
if its name were operator==,
where S is the scope of the class type of o
if F is a class member, and
the namespace scope of which F is a member otherwise.
A function template specialization named operator== is a rewrite target
if its function template is a rewrite target.
[Example 2: struct A {};
template<typename T>booloperator==(A, T); // #1bool a1 =0== A(); // OK, calls reversed #1template<typename T>booloperator!=(A, T);
bool a2 =0== A(); // error, #1 is not a rewrite targetstruct B {booloperator==(const B&); // #2};
struct C : B {
C();
C(B);
booloperator!=(const B&); // #3};
bool c1 = B()== C(); // OK, calls #2; reversed #2 is not a candidate// because search for operator!= in C finds #3bool c2 = C()== B(); // error: ambiguous between #2 found when searching C and// reversed #2 found when searching Bstruct D {};
template<typename T>booloperator==(D, T); // #4inlinenamespace N {template<typename T>booloperator!=(D, T); // #5}bool d1 =0== D(); // OK, calls reversed #4; #5 does not forbid #4 as a rewrite target β end example]
The set of candidate functions for overload resolution
for some operator @
is the
union of
the member candidates,
the non-member candidates,
the built-in candidates,
and the rewritten candidates
for that operator @.
[Example 3: struct A {operatorint();
};
A operator+(const A&, const A&);
void m(){
A a, b;
a + b; // operator+(a, b) chosen over int(a) + int(b)} β end example]
If a rewritten operator<=> candidate
is selected by overload resolution
for an operator @,
x @ y
is interpreted as
0 @ (y <=> x)
if the selected candidate is a synthesized candidate
with reversed order of parameters,
or (x <=> y) @ 0 otherwise,
using the selected rewritten operator<=> candidate.
Rewritten candidates for the operator @
are not considered in the context of the resulting expression.
If a rewritten operator== candidate
is selected by overload resolution
for an operator @,
its return type shall be cvbool, and
x @ y is interpreted as:
If a built-in candidate is selected by overload resolution, the
operands of class type are converted to the types of the corresponding parameters
of the selected operation function, except that the second standard conversion
sequence of a user-defined conversion sequence is not applied.
Then the operator is treated as the corresponding
built-in operator and interpreted according to [expr.compound].
[Example 4: struct X {operatordouble();
};
struct Y {operatorint*();
};
int*a = Y()+100.0; // error: pointer arithmetic requires integral operandint*b = Y()+ X(); // error: pointer arithmetic requires integral operand β end example]
If the operator is the operator
,,
the unary operator
&,
or the operator
->,
and there are no viable functions, then the operator is
assumed to be the built-in operator and interpreted according to
[expr.compound].
The lookup rules for operators in expressions are different than
the lookup
rules for operator function names in a function call, as shown in the following
example:
struct A {};
voidoperator+(A, A);
struct B {voidoperator+(B);
void f ();
};
A a;
void B::f(){operator+(a,a); // error: global operator hidden by member
a + a; // OK, calls global operator+}