[over.match.oper] (original) (raw)

12 Overloading [over]

12.2 Overload resolution [over.match]

12.2.2 Candidate functions and argument lists [over.match.funcs]

12.2.2.3 Operators in expressions [over.match.oper]

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

[Note 1:

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 (const char*);operator const char* ();}; String operator + (const String&, const String&);void f() { const char* p= "one" + "two"; int I = 1 + 1; } — _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 cv1 T1, and for a binary operator @with a left operand of type cv1 T1and a right operand of type cv2 T2, four sets of candidate functions, designatedmember candidates,non-member candidates,built-in candidates, andrewritten candidates, are constructed as follows:

A non-template function or function template F named operator==is a rewrite target with first operand ounless a search for the name operator!= in the scope Sfrom the instantiation context of the operator expression finds a function or function template that would correspond ([basic.scope.scope]) to Fif its name were operator==, where S is the scope of the class type of oif 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> bool operator==(A, T); bool a1 = 0 == A(); template<typename T> bool operator!=(A, T);bool a2 = 0 == A(); struct B { bool operator==(const B&); };struct C : B { C(); C(B);bool operator!=(const B&); };bool c1 = B() == C(); bool c2 = C() == B(); struct D {};template<typename T> bool operator==(D, T); inline namespace N { template<typename T> bool operator!=(D, T); } bool d1 = 0 == D(); — _end example_]

For the first parameter of the built-in assignment operators, only standard conversion sequences ([over.ics.scs]) are considered.

For all other operators, no such restrictions apply.

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

The argument list contains all of the operands of the operator.

[Example 3: struct A { operator int();}; A operator+(const A&, const A&);void m() { A a, b; a + b; } — _end example_]

If a rewritten operator<=> candidate is selected by overload resolution for an operator @,x @ yis interpreted as0 @ (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 cv bool, andx @ y is interpreted as:

in each case using the selected rewritten operator== candidate.

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 { operator double();};struct Y { operator int*();};int *a = Y() + 100.0; int *b = Y() + X(); — _end example_]

The second operand of operator->is ignored in selecting anoperator->function, and is not an argument when theoperator->function is called.

Whenoperator->returns, the operator->is applied to the value returned, with the original second operand.106

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

[Note 3:

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 { };void operator + (A, A);struct B { void operator + (B);void f ();}; A a;void B::f() { operator+ (a,a); a + a; }

— _end note_]