______________________________________________________________________ 13 Overloading [over] ______________________________________________________________________ 1 When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. By exten sion, two declarations in the same scope that declare the same name but with different types are called overloaded declarations. Only function declarations can be overloaded; object and type declarations cannot be overloaded. 2 When an overloaded function name is used, which overloaded function declaration is being referenced is determined by comparing the types of the arguments at the point of use with the types of the parameters in the overloaded declarations that are visible at the point of use. This function selection process is called overload resolution and is defined in _over.match_. For example, double abs(double); int abs(int); abs(1); // call abs(int); abs(1.0); // call abs(double); 13.1 Overloadable declarations [over.load] 1 Not all function declarations can be overloaded. Those that cannot be overloaded are specified here. A program is ill-formed if it contains two such non-overloadable declarations in the same scope. 2 Certain function declarations that cannot be distinguished by overload resolution cannot be overloaded: --Since for any type T, a parameter of type T and a parameter of type ``reference to T accept the same set of initializer values, function declarations with parameter types differing only in this respect cannot be overloaded. +------- BEGIN BOX 1 -------+ This restriction is hard to check across translation units. Moreover, ambiguities can be detected just fine at call time. Perhaps we should remove it. +------- END BOX 1 -------+ For example, int f(int i) { // ... } int f(int& r) // error: function types // not sufficiently different { // ... } It is, however, possible to distinguish between reference to const T, reference to volatile T, and plain reference to T so function declarations that differ only in this respect can be overloaded. Similarly, it is possible to distinguish between pointer to const T, pointer to volatile T, and plain reference to T so function declara tions that differ only in this respect can be overloaded. --Function declarations that differ only in the return type cannot be overloaded. --Member function declarations with the same name and the same parame ter types cannot be overloaded if any of them is a static member function declaration (_class.static_). The types of the implicit object parameters constructed for the member functions for the pur pose of overload resolution (_over.match.funcs_) are not considered when comparing parameter types for enforcement of this rule. In contrast, if there is no static member function declaration among a set of member function declarations with the same name and the same parameter types, then these member function declarations can be overloaded if they differ in the type of their implicit object parameter. The following example illustrates this distinction: class X { static void f(); void f(); // ill-formed void f() const; // ill-formed void f() const volatile; // ill-formed void g(); void g() const; // Ok: no static g void g() const volatile; // Ok: no static g }; 3 Function declarations that have equivalent parameter declarations declare the same function and therefore cannot be overloaded: --Parameter declarations that differ only in the use of equivalent typedef types are equivalent. A typedef is not a separate type, but only a synonym for another type (_dcl.typedef_). For example, typedef int Int; void f(int i); void f(Int i); // OK: redeclaration of f(int) void f(int i) { /* ... */ } void f(Int i) { /* ... */ } // error: redefinition of f(int) Enumerations, on the other hand, are distinct types and can be used to distinguish overloaded function declarations. For example, enum E { a }; void f(int i) { /* ... */ } void f(E i) { /* ... */ } --Parameter declarations that differ only in a pointer * versus an array [] are equivalent. That is, the array declaration is adjusted to become a pointer declaration (_dcl.fct_). Note that only the second and subsequent array dimensions are significant in parameter types (_dcl.array_). f(char*); f(char[]); // same as f(char*); f(char[7]); // same as f(char*); f(char[9]); // same as f(char*); g(char(*)[10]); g(char[5][10]); // same as g(char(*)[10]); g(char[7][10]); // same as g(char(*)[10]); g(char(*)[20]); // different from g(char(*)[10]); --Parameter declarations that differ only in the presence or absence of const and/or volatile are equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called. For example, typedef const int cInt; int f (int); int f (const int); // redeclaration of f (int); int f (int) { ... } // definition of f (int) int f (cInt) { ... } // error: redefinition of f (int) Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish over loaded function declarations. In particular, for any type T, pointer to T, pointer to const T, and pointer to volatile T are con sidered distinct parameter types, as are reference to T, reference to const T, and reference to volatile T. --Two parameter declarations that differ only in their default ini tialization are equivalent. Consider the following example void f (int i, int j); void f (int i, int j = 99); // Ok: redeclaration of f (int, int) void f (int i = 88, int j = 99); // Ok: redeclaration of f (int, int) void f (); // Ok: overloaded declaration of f void prog () { f (1, 2); // Ok: call f (int, int) f (1); // Ok: call f (int, int) f (); // Error: f (int, int) or f ()? } 13.1.1 Declaration matching [over.dcl] 1 Two function declarations of the same name refer to the same function if they are in the same scope and have equivalent parameter declara tions (_over.load_). A function member of a derived class is not in the same scope as a function member of the same name in a base class. For example, class B { public: int f(int); }; class D : public B { public: int f(char*); }; Here D::f(char*) hides B::f(int) rather than overloading it. void h(D* pd) { pd->f(1); // error: // D::f(char*) hides B::f(int) pd->B::f(1); // ok pd->f("Ben"); // ok, calls D::f } A locally declared function is not in the same scope as a function in a containing scope. int f(char*); void g() { extern f(int); f("asdf"); // error: f(int) hides f(char*) // so there is no f(char*) in this scope } void caller () { void callee (int, int); { void callee (int); // hides callee (int, int) callee (88, 99); // error: only callee (int) in scope } ) 2 Different versions of an overloaded member function can be given dif ferent access rules. For example, class buffer { private: char* p; int size; protected: buffer(int s, char* store) { size = s; p = store; } // ... public: buffer(int s) { p = new char[size = s]; } // ... }; 13.2 Overload resolution [over.match] 1 Overload resolution is a mechanism for selecting the best function to call given a list of expressions that are to be the arguments of the call and a set of candidate functions that can be called based on the context of the call. The selection criteria for the best function are the number of arguments, how well the arguments match the types of the parameters of the candidate function, and certain other properties of the candidate function. The function selected by overload resolution is not guaranteed to be appropriate for the context. Other restric tions, such as the accessibility of the function, can make its use in the calling context ill-formed. 2 Overload resolution selects the function to call in five distinct con texts within the language: --Invocation of a function named in the function call syntax (_expr.call_) --Invocation of a function call operator, a pointer-to-function con version function, or a reference-to-function conversion function of a class object named in the function call syntax (_over.match.call_) --Invocation of the operator referenced in an expression (_expr_) --Invocation of a constructor during initialization of a class object via a parenthesized expression list (_class.expl.init_) --Invocation of a user-defined conversion during initialization from an expression (_dcl.init_, _dcl.init.ref_) 3 Each of these contexts defines the set of candidate functions and the list of arguments in its own unique way. But, once the candidate functions and argument lists have been identified, the selection of the best function is the same in all cases: --First, a subset of the candidate functions--those that have the proper number of arguments and meet certain other conditions---is selected to form a set of viable functions. --Then the best viable function is selected based on the implicit con version sequences (_over.best.ics_) needed to match each argument to the corresponding parameter of each viable function. 4 If a best viable function exists and is unique, overload resolution succeeds and produces it as the result. Otherwise overload resolution fails and the invocation is ill-formed. 13.2.1 Candidate functions and argument lists [over.match.funcs] 1 The following subclauses describe the set of candidate functions and the argument list submitted to overload resolution in each of the five contexts in which overload resolution is used. The source transforma tions and constructions defined in these subclauses are only for the purpose of describing the overload resolution process. An implementa tion is not required to use such transformations and constructions. 2 The set of candidate functions can contain both member and non-member functions to be resolved against the same argument list. So that argument and parameter lists are comparable within this heterogeneous set, a member function is considered to have an extra parameter, called the implicit object parameter, which represents the object for which the member function has been called. For the purposes of over load resolution, both static and non-static member functions have an implicit object parameter, but constructors do not. 3 Similarly, when appropriate, the context can construct an argument list that contains an implied object argument to denote the object to be operated on. Since arguments and parameters are associated by position within their respective lists, the convention is that the implicit object parameter, if present, is always the first parameter and the implied object argument, if present, is always the first argu ment. 4 For non-static member functions, the type of the implicit object parameter is reference to cv X where X is the class that defines the member function and cv is the cv-qualification on the member function declaration. For example, for a const member function of class X, the extra parameter is assumed to have type reference to const X. For static member functions, the implicit object parameter is considered to match any object (since if the function is selected, the object is discarded). 5 During overload resolution, the implied object argument is indistin guishable from other arguments. The implicit object parameter, how ever, retains its identity since conversions on the corresponding argument shall obey these additional rules: --no temporary object can be introduced to hold the argument for the implicit object parameter --no user-defined conversions can be applied to achieve a type match with it --even if the implicit object parameter is not const-qualified, an rvalue temporary can be bound to the parameter as long as in all other respects the temporary can be converted to the type of the implicit object parameter. 13.2.1.1 Function call syntax [over.match.call] 1 Recall from _expr.call_, that a function call is a postfix-expression, possibly nested arbitrarily deep in parentheses, followed by an optional expression-list enclosed in parentheses: (...(opt postfix-expression )...)opt (expression-listopt) Overload resolution is required if the postfix-expression yields the name of a function, an object of class type, or a set of pointers-to- function. 2 Subclauses _over.call.func_ and _over.call.object_, respectively, describe how overload resolution is used in the first two cases to determine the function to call. 3 The third case arises from a postfix-expression of the form &F, where F names a set of overloaded functions. In the context of a function call, the set of functions named by F shall contain only non-member functions and static member functions1). And in this context using &F behaves the same as using the name F by itself. Thus, (&F)(expression-listopt) is simply (F)(expression-listopt), which is discussed in _over.call.func_. (The resolution of &F in other con texts is described in _over.over_.) 13.2.1.1.1 Call to named function [over.call.func] 1 Of interest in this subclause are only those function calls in which the postfix-expression ultimately contains a name that denotes one or more functions that might be called. Such a postfix-expression, per haps nested arbitrarily deep in parentheses, has one of the following forms: postfix-expression: postfix-expression . id-expression postfix-expression -> id-expression primary-expression These represent two syntactic subcategories of function calls: quali fied function calls and unqualified function calls. 2 In qualified function calls, the name to be resolved is an id- expression and is preceded by an -> or . operator. Since the con struct A->B is generally equivalent to (*A).B, the rest of this clause assumes, without loss of generality, that all member function calls have been normalized to the form that uses an object and the . opera tor. Furthermore, this clause assumes that the postfix-expression that is the left operand of the . operator has type cv T where T denotes a class2). Under this assumption, the id-expression in the call is looked up as a member function of T following the rules for looking up names in classes (_class.derived_). If a member function _________________________ 1) If F names a non-static member function, &F is a pointer-to-member, which cannot be used with the function call syntax. 2) Note that cv-qualifiers on the type of objects are significant in overload resolution for both lvalue and rvalue objects. is found, that function and its overloaded declarations constitute the set of candidate functions. Because of the usual name hiding rules, these will all be declared in T or they will all be declared in the same base class of T. The argument list is the expression-list in the call augmented by the addition of the left operand of the . operator in the normalized member function call as the implied object argument. 3 In unqualified function calls, the name is not qualified by an -> or . operator and has the more general form of a primary-expression. The name is looked up in the context of the function call following the normal rules for name lookup. If the name resolves to a non-member function declaration, that function and its overloaded declarations constitute the set of candidate functions. Because of the usual name hiding rules, these will all be declared in the same block or names pace. The argument list is the same as the expression-list in the call. If the name resolves to a member function, then the function call is actually a member function call. If the keyword this is in scope and refers to the class of that member function, then the func tion call is transformed into a normalized qualified function call using (*this) as the postfix-expression to the left of the . opera tor. 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 some class T. In this case, all overloaded declarations of the function name in T become candidate functions and a contrived object of type T becomes the implied object argument3). The call is ill- formed, however, if overload resolution selects one of the non-static member functions of T in this case. 13.2.1.1.2 Call to object of class type [over.call.object] 1 If the primary-expression E in the function call syntax evaluates to a class object of type cv T, then the set of candidate functions includes at least the function call operators of T. The function call operators of T are obtained by ordinary lookup of the name operator() in the context of (E).operator(). Because of the usual name hiding rules, these will all be declared in T or they will all be declared in the same base class of T. 2 In addition, for each conversion function declared in T of the form operator conversion-type-id () cv-qualifier; where conversion-type-id denotes the type pointer to function with parameters of type P1,...,Pn and returning R or type reference to function with parameters of type P1,...,Pn and returning R, a surro gate call function with the unique name call-function and having the _________________________ 3) An implied object argument must be contrived to correspond to the implicit object parameter attributed to member functions during over load resolution. It is not used in the call to the selected function. Since the member functions all have the same implicit object parame ter, the contrived object will not be the cause to select or reject a function. form R 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 conver sion function declared in an accessible base class provided the func tion is not hidden within T by another intervening declaration4). 3 If 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 argu ments of the call. 4 The argument list submitted to overload resolution consists of the argument expressions present in the function call syntax preceded by the implied object argument (E). When comparing the call against the function call operators, the implied object argument is compared against the implicit object parameter of the function call operator. When comparing the call against a surrogate call funtion, the implied object argument is compared against the first parameter of the surro gate call function. The conversion function from 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 by that first parameter. 13.2.1.2 Operators in expressions [over.match.oper] 1 If no operand of the operator has a type that is a class or an enumer ation, the operator is assumed to be a built-in operator and inter preted according to clause _expr_. For example, _________________________ 4) Note that this construction can yield candidate call functions that cannot be differentiated one from the other by overload resolution be cause 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. class String { public: String (const String&); String (char*); operator char* (); }; String operator + (const String&, const String&); void f(void) { char* p= "one" + "two"; // ill-formed because neither // operand has user defined type int I = 1 + 1; // Always evaluates to 2 even if // user defined types exist which // would perform the operation. } 2 If either operand has a type that is a class or an enumeration, a user-defined operator function might 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 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 1 (where @ denotes one of the opera tors covered in the specified subclause). Table 1--relationship between operator and function call notation +--------------+------------+--------------------+------------------------+ |Subclause | Expression | As member function | As non-member function | +--------------+------------+--------------------+------------------------+ |_over.unary_ | @a | (a).operator@ () | operator@ (a) | |_over.binary_ | a@b | (a).operator@ (b) | operator@ (a, b) | |_over.ass_ | a=b | (a).operator= (b) | | |_over.sub_ | a[b] | (a).operator[](b) | | |_over.ref_ | a-> | (a).operator-> () | | |_over.inc_ | a@ | (a).operator@ (0) | operator@ (a, 0) | +--------------+------------+--------------------+------------------------+ 3 Three sets of candidate functions are constructed as follows: --If the first operand of the operator is an object or reference to an object of class X, the operator could be implemented by a member operator function of X. The expression is transformed to a quali fied function call per column 3 of Table 1 and a set of candidate functions is constructed for the transformed call according to the rules in _over.call.func_. This set is designated the member candi dates . --If the operator is either a unary or binary operator (_over.unary_, _over.binary_, or _over.inc_), the operator could be implemented by a non-member operator function. The expression is transformed to an unqualified function call per column 4 of Table 1. The operator name is looked up in the context of the expression following the usual rules for name lookup except that all member functions are ignored. Thus, if the operator name resolves to any declaration, it will be to a non-member function declaration. That function and its overloaded declarations constitute the set of candidate functions designated the non-member candidates. Because of the name hiding rules, these will all be declared in the same block or namespace5). +------- BEGIN BOX 2 -------+ A motion is expected in Valley Forge that would eliminate all name hiding when resolving non-member operator names so that the non-member candidates would include all operators of the same name with a decla ration in any enclosing block or namespace. +------- END BOX 2 -------+ --In any case, a set of candidate functions, called the built-in can didates, is constructed. For the binary operator , or the unary operator &, the built-in candidates set is empty. For all other operators, the built-in candidates include all of the built-in oper ators defined in _over.built_ that, 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 to _over.best.ics_. 4 For the built-in assignment operators, conversions of the left operand are restricted as follows: _________________________ 5) Note that the look up rules for operators in expressions are dif ferent 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); // ERROR - global operator hidden by member a + a; // OK - calls global operator+ } --no temporaries are introduced to hold the left operand --no user-defined conversions are applied to achieve a type match with it 5 For all other operators, no such restrictions apply. 6 If a built-in candidate is selected by overload resolution, any class operands are first converted to the appropriate type for the operator. Then the operator is treated as the corresponding built-in operator and interpreted according to clause _expr_. The set of candidate functions for overload resolution is the union of the member candi dates, the non-member candidates, and the built-in candidates. The argument list contains all of the operands of the operator. 7 If the operator is the binary operator ,or the unary operator & and overload resolution is unsuccessful, then the operator is assumed to be the built-in operator and interpreted according to clause _expr_. 13.2.1.3 Initialization by user-defined [over.match.user] conversions 1 Under the conditions specified in _dcl.init_ and _dcl.init.ref_, a user-defined conversion can be invoked to convert the assignment- expression of an initializer-clause to the type of the object being initialized (which might be a temporary in the reference case). Over load resolution is used to select the user-defined conversion to be invoked. Assuming that cv1 T is the type of the object being initial ized, the candidate functions are selected as follows: --When T is a class type, the constructors of T are candidate func tions --When the type of the assignment-expression is a class type cv S, the conversion functions of S and its base classes are considered. Those that are not hidden within S and yield type cv2 T or a type that can be converted to type cv2 T, for any cv2 that is the same cv-qualification as, or lesser cv-qualification than, cv1, via a standard conversion sequence (_over.ics.scs_) are candidate func tions 2 In both cases, the argument list has one argument, which is the assignment-expression of the initializer-clause. This argument will be compared against the first parameter of the constructors and against the implicit object parameter of the conversion functions. 3 Because only one user-defined conversion is allowed in an implicit conversion sequence, special rules apply when selecting the best user- defined conversion (_over.match.best_, _over.best.ics_). 13.2.1.4 Initialization by constructor [over.match.ctor] 1 When objects of classes with constructors are initialized with a parenthesized expression-list (_class.expl.init_), overload resolution selects the constructor. The candidate functions are all the con structors of the class of the object being initialized. The argument list is the expression-list within the parentheses of the initializer. 13.2.2 Viable functions [over.match.viable] 1 From the set of candidate functions constructed for a given context (_over.match.funcs_), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences for the best fit (_over.match.best_). The selection of viable functions considers relationships between arguments and func tion parameters other than the ranking of conversion sequences. 2 First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list. --If there are m arguments in the list, all candidate functions having exactly m parameters are viable. --A candidate function having fewer than m parameters is viable only if it has an ellipsis in its parameter list (_dcl.fct_). For the purposes of overload resolution, its parameter list is extended to the right with ellipses so that there are exactly m parameters. --A candidate function having more than m parameters is viable only if the (m+1)-st parameter has a default initializer (_dcl.fct.default_). For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters. 3 Second, for F to be a viable function, there shall exist for each argument an implicit conversion sequence (_over.best.ics_) that con verts that argument to the corresponding parameter of F. If the parameter has reference type, the implicit conversion sequence includes the operation of binding the reference, and the fact that a reference to non-const cannot be bound to an rvalue can affect the viability of the function (see _over.ics.ref_). 13.2.3 Best Viable Function [over.match.best] 1 Let ICSi(F) denote the implicit conversion sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F. Subclause _over.best.ics_ defines the implicit conversion sequences and subclause _over.ics.rank_ defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another. Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then --for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that, --F1 is a non-template function and F2 is a template function, or, if not that, --the context is an initialization by user-defined conversion (see _dcl.init_ and _over.match.user_) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type. For example, struct A { A(); operator int(); operator double(); } a; int i = a; // a.operator int() followed by no conversion is better // than a.operator double() followed by a conversion // to int float x = a; // ambiguous: both possibilities require conversions, // and neither is better than the other 2 If there is exactly one viable function that is a better function than all other viable functions, then it is the one selected by overload resolution; otherwise the call is ill-formed6). 3 Examples: _________________________ 6) The algorithm for selecting the best viable function is linear in the number of viable functions. Run a simple tournament to find a function W that is not worse than any opponent it faced. Although an other function F that W did not face might be better than W, F cannot be the best function because at some point in the tournament F encoun tered another function G such that F was not better than G. Hence, W is either the best function or there is no best function. So, make a second pass over the viable functions to verify that W is better than all other functions. void Fcn(const int*, short); void Fcn(int*, int); int i; short s = 0; Fcn(&i, s); // is ambiguous because // &i -> int* is better than &i -> const int* // but s -> short is also better than s -> int Fcn(&i, 1L); // calls Fcn(int*, int), because // &i -> int* is better than &i -> const int* // and 1L -> short and 1L -> int are indistinguishable Fcn(&i,'c'); // calls Fcn(int*, int), because // &i -> int* is better than &i -> const int* // and 'c' -> int is better than 'c' -> short 13.2.3.1 Implicit conversion sequences [over.best.ics] 1 An implicit conversion sequence is a sequence of conversions used to convert an argument in a function call to the type of the correspond ing parameter of the function being called. The sequence of conver sions is governed by the rules for initialization of an object or ref erence by a single expression (_dcl.init_ and _dcl.init.ref_). 2 Implicit conversion sequences are concerned only with the type, cv- qualification, and lvalue-ness of the argument and how these are con verted to match the corresponding properties of the parameter. Other properties, such as the lifetime, storage class, alignment, or acces sibility of the argument and whether or not the argument is a bit- field are ignored. So, although an implicit conversion sequence can be defined for a given argument-parameter pair, the conversion from the argument to the parameter might still be ill-formed in the final analysis. 3 Except in the context of an initialization by user-defined conversion (_over.match.user_), a well-formed implicit conversion sequence is one of the following forms: --a standard conversion sequence (_over.ics.scs_), --a user-defined conversion sequence (_over.ics.user_), or --an ellipsis conversion sequence (_over.ics.ellipsis_). 4 In the context of an initialization by user-defined conversion (i.e., when considering the argument of a user-defined conversion function; see _over.match.user_), only standard conversion sequences and ellip sis conversion sequences are allowed. 5 When initializing a reference, the operation of binding the reference to an object or temporary occurs after any conversion. The binding operation is not a conversion, but it is considered to be part of a standard conversion sequence, and it can affect the rank of the con version sequence. See _over.ics.ref_. 6 In all contexts, when converting to the implicit object parameter or when converting to the left operand of an assignment operation only standard conversion sequences that create no temporary object for the result are allowed. 7 If no conversions are required to match an argument to a parameter type, the implicit conversion sequence is the standard conversion sequence consisting of the identity conversion (_over.ics.scs_). 8 If no sequence of conversions can be found to convert an argument to a parameter type or the conversion is otherwise ill-formed, an implicit conversion sequence cannot be formed. 9 If several different sequences of conversions exist that each convert the argument to the parameter type, the implicit conversion sequence is a sequence among these that is not worse than all the rest accord ing to _over.ics.rank_7). If that conversion sequence in not better than all the rest and a function that uses such an implicit conversion sequence is selected as the best viable function, then the call will be ill-formed because the conversion of one of the arguments in the call is ambiguous. 10The three forms of implicit conversion sequences mentioned above are defined in the following subclauses. _________________________ 7) This rule prevents a function from becoming non-viable because of an ambiguous conversion sequence for one of its parameters. Consider this example, class B; class A { A (B&); }; class B { operator A (); }; class C { C (B&); }; f(A) { } f(C) { } B b; f(b); // ambiguous since b -> C via constructor and // b -> A via constructor or conversion function. If it were not for this rule, f(A) would be eliminated as a viable function for the call f(b) causing overload resolution to select f(C) as the function to call even though it is not clearly the best choice. On the other hand, if an f(B) were to be declared then f(b) would re solved to that f(B) because the exact match with f(B) is better than any of the sequences required to match f(A). 13.2.3.1.1 Standard conversion sequences [over.ics.scs] 1 Table 2 summarizes the conversions defined in clause _conv_ and parti tions them into four disjoint categories: Lvalue Transformation, Qual ification Adjustment, Promotion, and Conversion. Note that these cat egories are orthogonal with respect to lvalue-ness, cv-qualification, and data representation: the Lvalue Transformations do not change the cv-qualification or data representation of the type; the Qualification Adjustments do not change the lvalue-ness or data representation of the type; and the Promotions and Conversions do not change the lvalue- ness or cv-qualification of the type. 2 A standard conversion sequence is either the Identity conversion by itself or consists of one to four conversions from the other four cat egories. At most one conversion from each category is allowed in a single standard conversion sequence. If there are two or more conver sions in the sequence, the conversions are applied in the canonical order: Lvalue Transformation, Promotion, Conversion, Qualification Adjustment. 3 Each conversion in Table 2 also has an associated rank (Exact Match, Promotion, or Conversion). These are used to rank standard conversion sequences (_over.ics.rank_). The rank of a conversion sequence is determined by considering the rank of each conversion in the sequence and the rank of any reference binding (_over.ics.ref_). If any of those has Conversion rank, the sequence has Conversion rank; other wise, if any of those has Promotion rank, the sequence has Promotion rank; otherwise, the sequence has Exact Match rank. Table 2--conversions +-------------------------------+--------------------------+-------------+-----------------+ |Conversion | Category | Rank | Subclause | +-------------------------------+--------------------------+-------------+-----------------+ +-------------------------------+--------------------------+-------------+-----------------+ |No conversions required | Identity | | | +-------------------------------+--------------------------+ +-----------------+ |Lvalue-to-rvalue conversion | | | _conv.lval_ | +-------------------------------+ | +-----------------+ |Array-to-pointer conversion | Lvalue Transformation | Exact Match | _conv.array_ | +-------------------------------+ | +-----------------+ |Function-to-pointer conversion | | | _conv.func_ | +-------------------------------+--------------------------+ +-----------------+ |Qualification conversions | Qualification Adjustment | | _conv.qual_ | +-------------------------------+--------------------------+-------------+-----------------+ |Integral promotions | | | _conv.prom_ | +-------------------------------+ Promotion | Promotion +-----------------+ |Floating point promotion | | | _conv.fpprom_ | +-------------------------------+--------------------------+-------------+-----------------+ |Integral conversions | | | _conv.integral_ | +-------------------------------+ | +-----------------+ |Floating point conversions | | | _conv.double_ | +-------------------------------+ | +-----------------+ |Floating-integral conversions | | | _conv.fpint_ | +-------------------------------+ | +-----------------+ |Pointer conversions | Conversion | Conversion | _conv.ptr_ | +-------------------------------+ | +-----------------+ |Pointer to member conversions | | | _conv.mem_ | +-------------------------------+ | +-----------------+ |Base class conversion | | | _conv.class_ | +-------------------------------+ | +-----------------+ |Boolean conversions | | | _conv.bool_ | +-------------------------------+--------------------------+-------------+-----------------+ 13.2.3.1.2 User-defined conversion sequences [over.ics.user] 1 A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user-defined conversion (_class.conv_) followed by a second standard conversion sequence. If the user-defined conversion is specified by a constructor (_class.conv.ctor_), the initial standard conversion sequence converts the source type to the type required by the argument of the construc tor. If the user-defined conversion is specified by a conversion function (_class.conv.fct_), the initial standard conversion sequence converts the source type to the implicit object parameter of the con version function. 2 The second standard conversion sequence converts the result of the user-defined conversion to the target type for the sequence. Since an implicit conversion sequence is an initialization, the special rules for initialization by user-defined conversion apply when selecting the best user-defined conversion for a user-defined conversion sequence (see _over.match.best_ and _over.best.ics_) 3 It should be noted that a conversion of an expression of class type to the same class type or to a base class of that type is a standard con version rather than a user-defined conversion in spite of the fact that a copy constructor (i.e., a user-defined conversion function) is called. 13.2.3.1.3 Ellipsis conversion sequences [over.ics.ellipsis] 1 An ellipsis conversion sequence occurs when an argument in a function call is matched with the ellipsis parameter specification of the func tion called. 13.2.3.1.4 Reference binding [over.ics.ref] 1 The operation of binding a reference is not a conversion, but for the purposes of overload resolution it is considered to be part of a stan dard conversion sequence (specifically, it is the last step in such a sequence). 2 A standard conversion sequence cannot be formed if it requires binding a reference to non-const to an rvalue (except when binding an implicit object parameter; see the special rules for that case in _over.match.funcs_). This means, for example, that a candidate func tion cannot be a viable function if it has a non-const reference parameter (other than the implicit object parameter) and the corre sponding argument is a temporary or would require one to be created to initialize the reference (see _dcl.init.ref_). 3 Other restrictions on binding a reference to a particular argument do not affect the formation of a standard conversion sequence, however. For example, a function with a reference to int parameter can be a viable candidate even if the corresponding argument is an int bit- field. The formation of implicit conversion sequences treats the int bit-field as an int lvalue and finds an exact match with the parame ter. If the function is selected by overload resolution, the call will nonetheless be ill-formed because of the prohibition on binding a non-const reference to a bit-field (_dcl.init.ref_). 4 A reference binding in general has no effect on the rank of a standard conversion sequence, but there is one exception: the binding of a ref erence to a (possibly cv-qualified) class to an expression of a (pos sibly cv-qualified) class derived from that class gives the overall standard conversion sequence Conversion rank. 13.2.3.2 Ranking implicit conversion sequences [over.ics.rank] 1 This clause defines a partial ordering of implicit conversion sequences based on the relationships better conversion sequence and better conversion. If an implicit conversion sequence S1 is defined by these rules to be a better conversion sequence than S2, then it is also the case that S2 is a worse conversion sequence than S1. If con version sequence S1 is neither better than nor worse than conversion sequence S2, S1 and S2 are said to be indistinguishable conversion sequences. 2 When comparing the basic forms of implicit conversion sequences (as defined in _over.best.ics_) --A standard conversion sequence (_over.ics.scs_) is a better conver sion sequence than a user-defined conversion sequence or an ellipsis conversion sequence --A user-defined conversion sequence (_over.ics.user_) is a better conversion sequence than an ellipsis conversion sequence (_over.ics.ellipsis_) 3 Two implicit conversion sequences of the same form are indistinguish able conversion sequences unless one of the following rules apply: --Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if --S1 is a proper subsequence of S2, or, if not that, --the dominant conversion of S1 is better than the dominant conver sion of S2 (by the rules defined below), or, if not that, --S1 and S2 differ only in their qualification conversion and they yield types identical except for cv-qualifiers and S2 adds all the qualifiers that S1 adds (and in the same places) and S2 adds yet more cv-qualifiers than S1, or the similar case with reference binding (see the definition of reference-compatible with added qualification in _dcl.init.ref_). --User-defined conversion sequence U1 is a better conversion sequence than another user-defined conversion sequence U2 if they contain the same user-defined conversion operator or constructor and if the sec ond standard conversion sequence of U1 is better than the second standard conversion sequence of U2. 4 Standard conversions are ordered by their ranks: an Exact Match is a better conversion than a Promotion, which is a better conversion than a Conversion. Two conversions with the same rank are indistinguish able unless one of the following rules applies: --If class B is derived directly or indirectly from class A, conver sion of B* to A* is better than conversion of B* to void*. --If class B is derived directly or indirectly from class A and class C is derived directly or indirectly from B, --conversion of C* to B* is better than conversion of C* to A* --Binding of an expression of type C to a reference of type B& is better than binding an expression of type C to a reference of type A& --conversion of A::* to B::* is better than conversion of A::* to C::* 13.3 Address of overloaded function [over.over] 1 A use of a function name without arguments selects, among all func tions of that name that are in scope, the (only) function that exactly matches the target. The target can be --an object being initialized (_dcl.init_) --the left side of an assignment (_expr.ass_) --a parameter of a function (_expr.call_) --a parameter of a user-defined operator (_over.oper_) --the return value of a function, operator function, or conversion (_stmt.return_) --an explicit type conversion (_expr.type.conv_, _expr.cast_) 2 Non-member functions match targets of type pointer-to-function; member functions match targets of type pointer-to-member-function. 3 Note that if f() and g() are both overloaded functions, the cross product of possibilities must be considered to resolve f(&g), or the equivalent expression f(g). 4 For example, int f(double); int f(int); (int (*)(int))&f; // cast expression as selector int (*pfd)(double) = &f; // selects f(double) int (*pfi)(int) = &f; // selects f (int) int (*pfe)(...) = &f; // error: type mismatch The last initialization is ill-formed because no f() with type int(...) has been defined, and not because of any ambiguity. 5 Note also that there are no standard conversions (_conv_) of one pointer-to-function type or pointer-to-member-function into another (_conv.ptr_). In particular, even if B is a public base of D we have D* f(); B* (*p1)() = &f; // error void g(D*); void (*p2)(B*) = &g; // error 6 Note that if the target type is a pointer to member function, the function type of the pointer to member is used to select the member function from a set of overloaded member functions. For example: struct X { int f(int); static int f(long); }; int (X::*p1)(int) = &X::f; // OK int (*p2)(int) = &X::f; // error: mismatch int (*p3)(long) = &X::f; // OK int (X::*p4)(long) = &X::f; // error: mismatch int (X::*p5)(int) = &(X::f); // error: wrong syntax for // pointer to member int (*p6)(long) = &(X::f); // OK 13.4 Overloaded operators [over.oper] 1 A function declaration having one of the following operator-function- ids as its name declares an operator function. An operator function is said to implement the operator named in its operator-function-id. operator-function-id: operator operator operator: one of new delete new[] delete[] + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- , ->* -> () [] The last two operators are function call (_expr.call_) and subscript ing (_expr.sub_). 2 Both the unary and binary forms of + - * & can be overloaded. 3 The following operators cannot be overloaded: . .* :: ?: nor can the preprocessing symbols # and ## (_cpp_). 4 Operator functions are usually not called directly; instead they are invoked to evaluate the operators they implement (_over.unary_ - _over.inc_). They can be explicitly called, though. For example, complex z = a.operator+(b); // complex z = a+b; void* p = operator new(sizeof(int)*n); 5 The allocation and deallocation functions, operator new, operator new[], operator delete and operator delete[], are described completely in _class.free_. The attributes and restrictions found in the rest of this section do not apply to them unless explicitly stated in _class.free_. 6 An operator function shall either be a non-static member function or, be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators. The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for specific types by defining operator functions that implement these operators. Operator functions are inherited the same as other func tions, but because an instance of operator= is automatically con structed for each class (_class.copy_, _over.ass_), operator= is never inherited by a class from its bases. 7 The identities among certain predefined operators applied to basic types (for example, ++a == a+=1) need not hold for operator functions. Some predefined operators, such as +=, require an operand to be an lvalue when applied to basic types; this is not required by operator functions. 8 An operator function cannot have default arguments (_dcl.fct.default_). 9 Operators not mentioned explicitly below in _over.ass_ to _over.inc_ act as ordinary unary and binary operators obeying the rules of sec tion _over.unary_ or _over.binary_. 13.4.1 Unary operators [over.unary] 1 A prefix unary operator can be implemented by a non-static member function (_class.mfct_) with no parameters or a non-member function with one parameter. Thus, for any prefix unary operator @, @x can be interpreted as either x.operator@() or operator@(x). If both forms of the operator function have been declared, the rules in _over.match.oper_ determine which, if any, interpretation is used. See _over.inc_ for an explanation of the postfix unary operators ++ and --. 2 The unary and binary forms of the same operator are considered to have the same name. Consequently, a unary operator can hide a binary oper ator from an enclosing scope, and vice versa. 13.4.2 Binary operators [over.binary] 1 A binary operator can be implemented either by a non-static member function (_class.mfct_) with one parameter or by a non-member function with two parameters. Thus, for any binary operator @, x@y can be interpreted as either x.operator@(y) or operator@(x,y). If both forms of the operator function have been declared, the rules in _over.match.oper_ determines which, if any, interpretation is used. 13.4.3 Assignment [over.ass] 1 An overloaded assignment operator shall be a non-static member func tion with exactly one parameter. Because an instance of operator= is constructed for each class (_class.copy_), it is never inherited by a derived class. 2 A copy assignment operator operator= is a non-static member function of class X with exactly one parameter of type X& or const X&. _class.copy_ describes the copy assignment operator. 13.4.4 Function call [over.call] 1 operator() shall be a non-static member function. It implements the function call syntax postfix-expression ( expression-listopt ) where the postfix-expression evaluates to a class object and the pos sibly empty expression-list matches the parameter list of an opera tor() member function of the class. Thus, a call x(arg1,arg2,arg3) is interpreted as x.operator()(arg1,arg2,arg3) for a class object x of type T if T::operator()(T1, T2, T3) exists and if the operator is selected as the best match function by the overload resolution mecha nism (_over.match.best_). 13.4.5 Subscripting [over.sub] 1 operator[] shall be a non-static member function. It implements the subscripting syntax postfix-expression [ expression ] Thus, a subscripting expression x[y] is interpreted as x.operator[](y) for a class object x of type T if T::operator()(T1) exists and if the operator is selected as the best match function by the overload reso lution mechanism (_over.match.best_). 13.4.6 Class member access [over.ref] 1 operator-> shall be a non-static member function taking no parameters. It implements class member access using -> postfix-expression -> primary-expression An expression x->m is interpreted as (x.operator->())->m for a class object x of type T if T::operator->() exists and if the operator is selected as the best match function by the overload resolution mecha nism (_over.match_). It follows that operator-> must return either a pointer to a class that has a member m or an object of or a reference to a class for which operator-> is defined. 13.4.7 Increment and decrement [over.inc] 1 The prefix and postfix increment operators can be implemented by a function called operator++. If this function is a member function with no parameters, or a non-member function with one class parameter, it defines the prefix increment operator ++ for objects of that class. If the function is a member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second shall be of type int), it defines the postfix increment opera tor ++ for objects of that class. When the postfix increment is called, the int argument will have value zero. For example, class X { public: const X& operator++(); // prefix ++a const X& operator++(int); // postfix a++ }; class Y { public: }; const Y& operator++(Y&); // prefix ++b const Y& operator++(Y&, int); // postfix b++ void f(X a, Y b) { ++a; // a.operator++(); a++; // a.operator++(0); ++b; // operator++(b); b++; // operator++(b, 0); a.operator++(); // explicit call: like ++a; a.operator++(0); // explicit call: like a++; operator++(b); // explicit call: like ++b; operator++(b, 0); // explicit call: like b++; } 2 The prefix and postfix decrement operators -- are handled similarly. 13.5 Built-in operators [over.built] 1 The built-in operators (_expr_) participate in overload resolution (_over.match.oper_) as though declared as specified in this section. For operator, and unary operator&, a built-in operator is selected only if there are no user-defined operator candidates. For all other built-in operators, since they take only operands with non-class type, and operator overload resolution occurs only when an operand expres sion originally has class type, operator overload resolution can resolve to a built-in operator only when an operand has a class type which has a user-defined conversion to a non-class type appropriate for the operator. 2 In this section, the term promoted integral type is used to refer to those integral types which are preserved by integral promotion (including e.g. int but excluding e.g. char). Similarly, the term promoted arithmetic type refers to promoted integral types plus float ing types. 3 For every pair T, VQ), where T is an arithmetic type, and VQ is either volatile or empty, there exist VQ T& operator++(VQ T&); VQ T& operator--(VQ T&); T operator++(VQ T&, int); T operator--(VQ T&, int); 4 For every pair T, VQ), where T is a cv-qualified or unqualified com plete object type, and VQ is either volatile or empty, there exist T*VQ& operator++(T*VQ&); T*VQ& operator--(T*VQ&); T* operator++(T*VQ&, int); T* operator--(T*VQ&, int); 5 For every cv-qualified or unqualified complete object type T, there exists T& operator*(T*); 6 For every function type T, there exists T& operator*(T*); 7 For every type T, there exist T* operator&(T&); T* operator+(T*); 8 For every promoted arithmetic type T, there exist T operator+(T); T operator-(T); 9 For every promoted integral type T, there exists T operator~(T); 10For every quadruple C, T, CV1, CV2), where C is a class type, T is a complete object type or a function type, and CV1 and CV2 are cv- qualifier-seqs, there exists CV12 T& operator->*(CV1 C*, CV2 T C::*); where CV12 is the union of CV1 and CV2. 11For every pair of promoted arithmetic types L and R, there exist LR operator*(L, R); LR operator/(L, R); LR operator+(L, R); LR operator-(L, R); bool operator<(L, R); bool operator>(L, R); bool operator<=(L, R); bool operator>=(L, R); bool operator==(L, R); bool operator!=(L, R); where LR is the result of the usual arithmetic conversions between types L and R. 12For every pair of types T and I, where T is a cv-qualified or unquali fied complete object type and I is a promoted integral type, there exist T* operator+(T*, I); T& operator[](T*, I); T* operator-(T*, I); T* operator+(I, T*); T& operator[](I, T*); 13For every triple T, CV1, CV2), where T is a complete object type, and CV1 and CV2 are cv-qualifier-seqs, there exists ptrdiff_t operator-(CV1 T*, CV2 T*); 14For every triple T, CV1, CV2), where T is any type, and CV1 and CV2 are cv-qualifier-seqs, there exist bool operator<(CV1 T*, CV2 T*); bool operator>(CV1 T*, CV2 T*); bool operator<=(CV1 T*, CV2 T*); bool operator>=(CV1 T*, CV2 T*); bool operator==(CV1 T*, CV2 T*); bool operator!=(CV1 T*, CV2 T*); 15For every quadruple C, T, CV1, CV2), where C is a class type, T is any type, and CV1 and CV2 are cv-qualifier-seqs, there exist bool operator==(CV1 T C::*, CV2 T C::*); bool operator!=(CV1 T C::*, CV2 T C::*); 16For every pair of promoted integral types L and R, there exist LR operator%(L, R); LR operator&(L, R); LR operator^(L, R); LR operator|(L, R); L operator<<(L, R); L operator>>(L, R); where LR is the result of the usual arithmetic conversions between types L and R. 17For every triple L, VQ, R), where L is an arithmetic type, VQ is either volatile or empty, and R is a promoted arithmetic type, there exist VQ L& operator=(VQ L&, R); VQ L& operator*=(VQ L&, R); VQ L& operator/=(VQ L&, R); VQ L& operator+=(VQ L&, R); VQ L& operator-=(VQ L&, R); 18For every pair T, VQ), where T is any type and VQ is either volatile or empty, there exists T*VQ& operator=(T*VQ&, T*); 19For every triple T, VQ, I), where T is a cv-qualified or unqualified complete object type, VQ is either volatile or empty, and I is a pro moted integral type, there exist T*VQ& operator+=(T*VQ&, I); T*VQ& operator-=(T*VQ&, I); 20For every triple L, VQ, R), where L is an integral type, VQ is either volatile or empty, and R is a promoted integral type, there exist VQ L& operator%=(VQ L&, R); VQ L& operator<<=(VQ L&, R); VQ L& operator>>=(VQ L&, R); VQ L& operator&=(VQ L&, R); VQ L& operator^=(VQ L&, R); VQ L& operator|=(VQ L&, R); 21For every pair of types L and R, there exists R operator,(L, R); 22There also exist bool operator!(bool); bool operator&&(bool, bool); bool operator||(bool, bool);