Стандарт C++ 11 (1119564), страница 82
Текст из файла (страница 82)
The source transformationsand constructions defined in these subclauses are only for the purpose of describing the overload resolutionprocess. An implementation is not required to use such transformations and constructions.2The set of candidate functions can contain both member and non-member functions to be resolved againstthe same argument list.
So that argument and parameter lists are comparable within this heterogeneousset, a member function is considered to have an extra parameter, called the implicit object parameter, whichrepresents the object for which the member function has been called. For the purposes of overload resolution,both static and non-static member functions have an implicit object parameter, but constructors do not.3Similarly, when appropriate, the context can construct an argument list that contains an implied objectargument to denote the object to be operated on.
Since arguments and parameters are associated by§ 13.3.1© ISO/IEC 2011 – All rights reserved293ISO/IEC 14882:2011(E)position within their respective lists, the convention is that the implicit object parameter, if present, isalways the first parameter and the implied object argument, if present, is always the first argument.4For non-static member functions, the type of the implicit object parameter is— “lvalue reference to cv X” for functions declared without a ref-qualifier or with the & ref-qualifier— “rvalue reference to cv X” for functions declared with the && ref-qualifierwhere X is the class of which the function is a member and cv is the cv-qualification on the member functiondeclaration.
[ Example: for a const member function of class X, the extra parameter is assumed to havetype “reference to const X”. — end example ] For conversion functions, the function is considered to bea member of the class of the implied object argument for the purpose of defining the type of the implicitobject parameter. For non-conversion functions introduced by a using-declaration into a derived class, thefunction is considered to be a member of the derived class for the purpose of defining the type of the implicitobject parameter.
For static member functions, the implicit object parameter is considered to match anyobject (since if the function is selected, the object is discarded). [ Note: No actual type is established forthe implicit object parameter of a static member function, and no attempt will be made to determine aconversion sequence for that parameter (13.3.3). — end note ]5During overload resolution, the implied object argument is indistinguishable from other arguments. Theimplicit object parameter, however, retains its identity since conversions on the corresponding argumentshall obey these additional rules:— no temporary object can be introduced to hold the argument for the implicit object parameter; and— no user-defined conversions can be applied to achieve a type match with it.For non-static member functions declared without a ref-qualifier, an additional rule applies:— even if the implicit object parameter is not const-qualified, an rvalue can be bound to the parameter aslong as in all other respects the argument can be converted to the type of the implicit object parameter.[ Note: The fact that such an argument is an rvalue does not affect the ranking of implicit conversionsequences (13.3.3.2).
— end note ]6Because other than in list-initialization only one user-defined conversion is allowed in an implicit conversionsequence, special rules apply when selecting the best user-defined conversion (13.3.3, 13.3.3.1). [ Example:class T {public:T();};class C : T {public:C(int);};T a = 1;// ill-formed: T(C(1)) not tried— end example ]7In each case where a candidate is a function template, candidate function template specializations are generated using template argument deduction (14.8.3, 14.8.2).
Those candidates are then handled as candidatefunctions in the usual way.125 A given name can refer to one or more function templates and also to a set125) The process of argument deduction fully determines the parameter types of the function template specializations, i.e.,the parameters of function template specializations contain no template parameter types. Therefore the function templatespecializations can be treated as normal (non-template) functions for the remainder of overload resolution.§ 13.3.1294© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)of overloaded non-template functions.
In such a case, the candidate functions generated from each functiontemplate are combined with the set of non-template candidate functions.13.3.1.11Function call syntax[over.match.call]In a function call (5.2.2)postfix-expression ( expression-listopt )if the postfix-expression denotes a set of overloaded functions and/or function templates, overload resolutionis applied as specified in 13.3.1.1.1. If the postfix-expression denotes an object of class type, overloadresolution is applied as specified in 13.3.1.1.2.2If the postfix-expression denotes the address of a set of overloaded functions and/or function templates,overload resolution is applied using that set as described above.
If the function selected by overload resolutionis a non-static member function, the program is ill-formed. [ Note: The resolution of the address of anoverload set in other contexts is described in 13.4. — end note ]13.3.1.1.11Call to named function[over.call.func]Of interest in 13.3.1.1.1 are only those function calls in which the postfix-expression ultimately contains aname that denotes one or more functions that might be called.
Such a postfix-expression, perhaps nestedarbitrarily deep in parentheses, has one of the following forms:postfix-expression:postfix-expression . id-expressionpostfix-expression -> id-expressionprimary-expressionThese represent two syntactic subcategories of function calls: qualified function calls and unqualified functioncalls.2In qualified function calls, the name to be resolved is an id-expression and is preceded by an -> or .
operator.Since the construct A->B is generally equivalent to (*A).B, the rest of Clause 13 assumes, without loss ofgenerality, that all member function calls have been normalized to the form that uses an object and the. operator. Furthermore, Clause 13 assumes that the postfix-expression that is the left operand of the .operator has type “cv T” where T denotes a class126 . Under this assumption, the id-expression in the callis looked up as a member function of T following the rules for looking up names in classes (10.2).
Thefunction declarations found by that lookup constitute the set of candidate functions. The argument listis the expression-list in the call augmented by the addition of the left operand of the . operator in thenormalized 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 general formof a primary-expression. The name is looked up in the context of the function call following the normal rulesfor name lookup in function calls (3.4).
The function declarations found by that lookup constitute the set ofcandidate functions. Because of the rules for name lookup, the set of candidate functions consists (1) entirelyof non-member functions or (2) entirely of member functions of some class T. In case (1), the argument listis the same as the expression-list in the call. In case (2), the argument list is the expression-list in the callaugmented by the addition of an implied object argument as in a qualified function call.
If the keywordthis (9.3.2) is in scope and refers to class T, or a derived class of T, then the implied object argument is(*this). If the keyword this is not in scope or refers to another class, then a contrived object of type126) Note that cv-qualifiers on the type of objects are significant in overload resolution for both glvalue and class prvalueobjects.§ 13.3.1.1.1© ISO/IEC 2011 – All rights reserved295ISO/IEC 14882:2011(E)T becomes the implied object argument127 . If the argument list is augmented by a contrived object andoverload resolution selects one of the non-static member functions of T, the call is ill-formed.13.3.1.1.2Call 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 theset of candidate functions includes at least the function call operators of T.
The function call operators of Tare obtained by ordinary lookup of the name operator() in the context of (E).operator().2In addition, for each non-explicit conversion function declared in T of the formoperator conversion-type-id ( ) attribute-specifier-seqopt cv-qualifier ;where cv-qualifier is the same cv-qualification as, or a greater cv-qualification than, cv, and where conversiontype-id denotes the type “pointer to function of (P1,...,Pn) returning R”, or the type “reference to pointer tofunction of (P1,...,Pn) returning R”, or the type “reference to function of (P1,...,Pn) returning R”, a surrogatecall 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 candidatefunctions for each non-explicit conversion function declared in a base class of T provided the function is nothidden within T by another intervening declaration128 .3If such a surrogate call function is selected by overload resolution, the corresponding conversion function willbe called to convert E to the appropriate function pointer or reference, and the function will then be invokedwith the arguments of the call.
If the conversion function cannot be called (e.g., because of an ambiguity),the program is ill-formed.4The argument list submitted to overload resolution consists of the argument expressions present in thefunction call syntax preceded by the implied object argument (E). [ Note: When comparing the call againstthe function call operators, the implied object argument is compared against the implicit object parameterof the 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 parametersince it converts the implied object argument to the appropriate function pointer or reference required bythat first parameter. — end note ] [ Example:int 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 function127) An implied object argument must be contrived to correspond to the implicit object parameter attributed to memberfunctions during overload resolution.