Стандарт C++ 98 (1119566), страница 60
Текст из файла (страница 60)
operator in the normalized member function call as the implied object argument (13.3.1).3In unqualified function calls, the name is not qualified by an -> or . operator and has the more generalform of a primary-expression. The name is looked up in the context of the function call following the normal rules for name lookup in function calls (3.4.2).
If the name resolves to a non-member function declaration, that function and its overloaded declarations constitute the set of candidate functions116). The argument list is the same as the expression-list in the call. If the name resolves to a nonstatic member function,then the function call is actually a member function call. If the keyword this (9.3.2) is in scope and refersto the class of that member function, or a derived class thereof, then the function call is transformed into anormalized qualified function call using (*this) as the postfix-expression to the left of the . operator.The candidate functions and argument list are as described for qualified function calls above.
If the keyword this is not in scope or refers to another class, then name resolution found a static member of someclass T. In this case, all overloaded declarations of the function name in T become candidate functions anda contrived object of type T becomes the implied object argument117). The call is ill-formed, however, ifoverload resolution selects one of the non-static member functions of T in this case.13.3.1.1.2 Call to object of class type[over.call.object]1If the primary-expression E in the function call syntax evaluates to a class object of type “cv T”, then the setof candidate functions includes at least the function call operators of T.
The function call operators of T areobtained by ordinary lookup of the name operator() in the context of (E).operator().2In addition, for each conversion function declared in T of the formoperator conversion-type-id () cv-qualifier;where cv-qualifier is the same cv-qualification as, or a greater cv-qualification than, cv, and whereconversion-type-id denotes the type “pointer to function of (P1,...,Pn) returning R”, or the type “referenceto pointer to function of (P1,...,Pn) returning R”, or the type “reference to function of (P1,...,Pn) returningR”, a surrogate call function with the unique name call-function and having the formR call-function (conversion-type-id F, P1 a1,...,Pn an) { return F (a1,...,an); }is also considered as a candidate function.
Similarly, surrogate call functions are added to the set of candidate functions for each conversion function declared in an accessible base class provided the function is nothidden within T by another intervening declaration118).3If such a surrogate call function is selected by overload resolution, its body, as defined above, will be executed to convert E to the appropriate function and then to invoke that function with the arguments of thecall.4The argument list submitted to overload resolution consists of the argument expressions present in the function call syntax preceded by the implied object argument (E).
[Note: when comparing the call against thefunction call operators, the implied object argument is compared against the implicit object parameter ofthe function call operator. When comparing the call against a surrogate call function, the implied objectargument is compared against the first parameter of the surrogate call function.
The conversion functionfrom which the surrogate call function was derived will be used in the conversion sequence for that parameter since it converts the implied object argument to the appropriate function pointer or reference required bythat first parameter. ] [Example:__________________116) Because of the usual name hiding rules, these will be introduced by declarations or by using-directives all found in the same blockor all found at namespace scope.117) An implied object argument must be contrived to correspond to the implicit object parameter attributed to member functions during overload resolution.
It is not used in the call to the selected function. Since the member functions all have the same implicit objectparameter, the contrived object will not be the cause to select or reject a function.118) Note that this construction can yield candidate call functions that cannot be differentiated one from the other by overload resolution because they have identical declarations or differ only in their return type. The call will be ambiguous if overload resolution cannot select a match to the call that is uniquely better than such undifferentiable functions.215ISO/IEC 14882:1998(E)© ISO/IEC13.3.1.1.2 Call to object of class type13 Overloadingint f1(int);int f2(float);typedef int (*fp1)(int);typedef int (*fp2)(float);struct A {operator fp1() { return f1; }operator fp2() { return f2; }} a;int i = a(1);// Calls f1 via pointer returned from// conversion function—end example]13.3.1.2 Operators in expressions1[over.match.oper]If no operand of an operator in an expression has a type that is a class or an enumeration, the operator isassumed to be a built-in operator and interpreted according to clause 5.
[Note: because ., .*, and :: cannot be overloaded, these operators are always built-in operators interpreted according to clause 5. ?: cannot be overloaded, but the rules in this subclause are used to determine the conversions to be applied to thesecond and third operands when they have class or enumeration type (5.16). ] [Example:class String {public:String (const String&);String (char*);operator char* ();};String operator + (const String&, const String&);void f(void){char* p= "one" + "two";int I = 1 + 1;// ill-formed because neither// operand has user defined type// Always evaluates to 2 even if// user defined types exist which// would perform the operation.}—end example]2If either operand has a type that is a class or an enumeration, a user-defined operator function might bedeclared that implements this operator or a user-defined conversion can be necessary to convert the operandto a type that is appropriate for a built-in operator.
In this case, overload resolution is used to determinewhich operator function or built-in operator is to be invoked to implement the operator. Therefore, theoperator notation is first transformed to the equivalent function-call notation as summarized in Table 8(where @ denotes one of the operators covered in the specified subclause).Table 8—relationship between operator and function call notation_______________________________________________________________________________________________________________________________________________ As non-member function Subclause Expression As member function______________________________________________________________________ 13.5.1 @a (a).operator@ () operator@ (a) 13.5.2 a@b (a).operator@ (b) operator@ (a, b) 13.5.3 a=b (a).operator= (b) 13.5.5 a[b] (a).operator[](b) 13.5.6 a-> (a).operator-> () 13.5.7 a@_______________________________________________________________________ (a).operator@ (0) operator@ (a, 0)216© ISO/IEC13 Overloading3ISO/IEC 14882:1998(E)13.3.1.2 Operators in expressionsFor a unary operator @ with an operand of a type whose cv-unqualified version is T1, and for a binary operator @ with a left operand of a type whose cv-unqualified version is T1 and a right operand of a type whosecv-unqualified version is T2, three sets of candidate functions, designated member candidates, non-membercandidates and built-in candidates, are constructed as follows:— If T1 is a class type, the set of member candidates is the result of the qualified lookup ofT1::operator@ (13.3.1.1.1); otherwise, the set of member candidates is empty.— The set of non-member candidates is the result of the unqualified lookup of operator@ in the contextof the expression according to the usual rules for name lookup in unqualified function calls (3.4.2)except that all member functions are ignored.
However, if no operand has a class type, only those nonmember functions in the lookup set that have a first parameter of type T1 or “reference to (possibly cvqualified) T1”, when T1 is an enumeration type, or (if there is a right operand) a second parameter oftype T2 or “reference to (possibly cv-qualified) T2”, when T2 is an enumeration type, are candidatefunctions.— For the operator ,, the unary operator &, or the operator ->, the built-in candidates set is empty. For allother operators, the built-in candidates include all of the candidate operator functions defined in 13.6that, compared to the given operator,— have the same operator name, and— accept the same number of operands, and— accept operand types to which the given operand or operands can be converted according to13.3.3.1, and— do not have the same parameter type list as any non-template non-member candidate.4For the built-in assignment operators, conversions of the left operand are restricted as follows:— no temporaries are introduced to hold the left operand, and— no user-defined conversions are applied to the left operand to achieve a type match with the left-mostparameter of a built-in candidate.5For all other operators, no such restrictions apply.6The set of candidate functions for overload resolution is the union of the member candidates, the nonmember candidates, and the built-in candidates.
The argument list contains all of the operands of the operator. The best function from the set of candidate functions is selected according to 13.3.2 and 13.3.3.119)[Example:struct A {operator int();};A operator+(const A&, const A&);void m() {A a, b;a + b;// operator+(a,b) chosen over int(a) + int(b)}—end example]7If a built-in candidate is selected by overload resolution, the operands are converted to the types of the corresponding parameters of the selected operation function. Then the operator is treated as the correspondingbuilt-in operator and interpreted according to clause 5.8The second operand of operator -> is ignored in selecting an operator-> function, and is not an argument when the operator-> function is called.
When operator-> returns, the operator -> is appliedto the value returned, with the original second operand.120)__________________119) If the set of candidate functions is empty, overload resolution is unsuccessful.120) If the value returned by the operator-> function has class type, this may result in selecting and calling another operator->217ISO/IEC 14882:1998(E)© ISO/IEC13.3.1.2 Operators in expressions13 Overloading9If 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 clause 5.10[Note: the lookup rules for operators in expressions are different than the lookup rules for operator functionnames 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;}// ERROR – global operator hidden by member// OK – calls global operator+—end note]13.3.1.3 Initialization by constructor1When objects of class type are direct-initialized (8.5), overload resolution selects the constructor.