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 extension, 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 in a call, 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 13.3. [Example:
---end example]double abs(double); int abs(int); abs(1); // call abs(int); abs(1.0); // call abs(double);
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. [Note: this restriction applies to explicit declarations in a scope, and between such declarations and declarations made through a using-declaration (7.3.3). It does not apply to sets of functions fabricated as a result of name lookup (e.g., because of using-directives) or overload resolution (e.g., for operator functions). ]
2 Certain function declarations cannot be overloaded:
---end example]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 [Note: as specified in 8.3.5, function declarations that have equivalent parameter declarations declare the same function and therefore cannot be overloaded:
---end example] Enumerations, on the other hand, are distinct types and can be used to distinguish overloaded function declarations. [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)
---end example]enum E { a }; void f(int i) { /* ... */ } void f(E i) { /* ... */ }
---end example]int f(char*); int f(char[]); // same as f(char*); int f(char[7]); // same as f(char*); int f(char[9]); // same as f(char*); int g(char(*)[10]); int g(char[5][10]); // same as g(char(*)[10]); int g(char[7][10]); // same as g(char(*)[10]); int g(char(*)[20]); // different from g(char(*)[10]);
]void h(int()); void h(int (*)()); // redeclaration of h(int()) void h(int x()) { } // definition of h(int()) void h(int (*x)()) { } // ill-formed: redefinition of h(int())
---end example] 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 overloaded function declarations.112) In particular, for any type T, ``pointer to T,'' ``pointer to const T,'' and ``pointer to volatile T'' are considered distinct parameter types, as are ``reference to T,'' ``reference to const T,'' and ``reference to volatile T.''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)
---end example] ---end note]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); // 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()? }
112) When a parameter type includes a function type, such as in the case of a parameter type that is a pointer to function, the const and volatile type-specifiers at the outermost level of the parameter type specifications for the inner function type are also ignored. [back to text]
1 Two function declarations of the same name refer to the same function if they are in the same scope and have equivalent parameter declarations (13.1). 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. [Example:
Here D::f(char*) hides B::f(int) rather than overloading it.class B { public: int f(int); }; class D : public B { public: int f(char*); };
---end example]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 }
2 A locally declared function is not in the same scope as a function in a containing scope. [Example:
---end example]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 () { extern void callee(int, int); { extern void callee(int); // hides callee(int, int) callee(88, 99); // error: only callee(int) in scope } }
3 Different versions of an overloaded member function can be given different access rules. [Example:
---end 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]; } // ... };
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, how well (for nonstatic member functions) the object matches the implied object parameter, and certain other properties of the candidate function. [Note: the function selected by overload resolution is not guaranteed to be appropriate for the context. Other restrictions, 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 seven distinct contexts within the language:
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:
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. When overload resolution succeeds, and the best viable function is not accessible (clause 11) in the context in which it is used, the program is ill-formed.
1 The subclauses of 13.3.1 describe the set of candidate functions and the argument list submitted to overload resolution in each of the seven contexts in which overload resolution is used. The source transformations and constructions defined in these subclauses are only for the purpose of describing the overload resolution process. An implementation 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 overload 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 argument.
4 For non-static member functions, the type of the implicit object parameter is ``reference to cv X'' where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [Example: for a const member function of class X, the extra parameter is assumed to have type ``reference to const X''. ] For conversion functions, the function is considered to be a member of the class of the implicit object argument for the purpose of defining the type of the implicit object parameter. For nonconversion functions introduced by a using-declaration into a derived class, the function is considered to be a member of the derived class for the purpose of defining the type of the implicit object parameter. For static member functions, the implicit object parameter is considered to match any object (since if the function is selected, the object is discarded). [Note: no actual type is established for the implicit object parameter of a static member function, and no attempt will be made to determine a conversion sequence for that parameter (13.3.3). ]
5 During overload resolution, the implied object argument is indistinguishable from other arguments. The implicit object parameter, however, retains its identity since conversions on the corresponding argument shall obey these additional rules:
6 Because only one user-defined conversion is allowed in an implicit conversion sequence, special rules apply when selecting the best user-defined conversion (13.3.3, 13.3.3.1). [Example:
---end example]class T { public: T(); // ... }; class C : T { public: C(int); // ... }; T a = 1; // ill-formed: T(C(1)) not tried
7 In each case where a candidate is a function template, candidate template functions are generated using template argument deduction (14.8.3, 14.8.2). Those candidates are then handled as candidate functions in the usual way.113) A given name can refer to one or more function templates and also to a set of overloaded non-template functions. In such a case, the candidate functions generated from each function template are combined with the set of non-template candidate functions.
113) The process of argument deduction fully determines the parameter types of the template functions, i.e., the parameters of template functions contain no template parameter types. Therefore the template functions can be treated as normal (non-template) functions for the remainder of overload resolution. [back to text]
1 Recall from 5.2.2, that a function call is a postfix-expression, possibly nested arbitrarily deep in parentheses, followed by an optional expression-list enclosed in parentheses:
Overload resolution is required if the postfix-expression is the name of a function, a function template (14.5.5), an object of class type, or a set of pointers-to-function.(...(opt postfix-expression )...)opt (expression-listopt)
2 13.3.1.1.1 describes how overload resolution is used in the first two of the above cases to determine the function to call. 13.3.1.1.2 describes how overload resolution is used in the third of the above cases to determine the function to call.
3 The fourth 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 functions114). 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 13.3.1.1.1. (The resolution of &F in other contexts is described in 13.4.)
114) If F names a non-static member function, &F is a pointer-to-member, which cannot be used with the function call syntax. [back to text]
1 Of interest in 13.3.1.1.1 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, perhaps nested arbitrarily deep in parentheses, has one of the following forms:
2 In 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 of generality, 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 class115). 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 (10.2). If a member function is found, that function and its overloaded declarations constitute the set of candidate functions. 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 (13.3.1).
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 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 refers to the class of that member function, or a derived class thereof, then the function call is transformed into a normalized 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 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 argument117). The call is ill-formed, however, if overload resolution selects one of the non-static member functions of T in this case.
115) Note that cv-qualifiers on the type of objects are significant in overload resolution for both lvalue and class rvalue objects. [back to text]116) Because of the usual name hiding rules, these will be introduced by declarations or by using-directives all found in the same block or all found at namespace scope. [back to text]
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 object parameter, the contrived object will not be the cause to select or reject a function. [back to text]
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().
2 In addition, for each conversion function declared in T of the form
where cv-qualifier is the same cv-qualification as, or a greater cv-qualification than, cv, and where conversion-type-id denotes the type ``pointer to function of (P1,...,Pn) returning R'', or the type ``reference to pointer to function of (P1,...,Pn) returning R'', or the type ``reference to function of (P1,...,Pn) returning R'', a surrogate call function with the unique name call-function and having the formoperator conversion-type-id () cv-qualifier;
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 not hidden within T by another intervening declaration118).R call-function (conversion-type-id F, P1 a1,...,Pn an) { return F (a1,...,an); }
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 arguments 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). [Note: 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 function, the implied object argument is compared against the first parameter of the surrogate 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. ] [Example:
---end 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 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. [back to text]
1 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 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 the second and third operands when they have class or enumeration type (5.16). ] [Example:
---end example]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 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 8 (where @ denotes one of the operators covered in the specified subclause).
Table 8---relationship between operator and function call notation |
_ ______________________________________________________________________ _ Subclause Expression As member function As non-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) ______________________________________________________________________ |
3 For 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 whose cv-unqualified version is T2, three sets of candidate functions, designated member candidates, non-member candidates and built-in candidates, are constructed as follows:
4 For the built-in assignment operators, conversions of the left operand are restricted as follows:
5 For all other operators, no such restrictions apply.
6 The 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:
---end 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) }
7 If 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 corresponding built-in operator and interpreted according to clause 5.
8 The 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 applied to the value returned, with the original second operand.120)
9 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 clause 5.
10 [Note: 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:
---end note]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+ }
119) If the set of candidate functions is empty, overload resolution is unsuccessful. [back to text]120) If the value returned by the operator-> function has class type, this may result in selecting and calling another operator-> function. The process repeats until an operator-> function returns a value of non-class type. [back to text]
1 When objects of class type are direct-initialized (8.5), overload resolution selects the constructor. The candidate functions are all the constructors of the class of the object being initialized. The argument list is the expression-list within the parentheses of the initializer.
1 Under the conditions specified in 8.5, as part of a copy-initialization of an object of class type, a userdefined conversion can be invoked to convert an initializer expression to the type of the object being initialized. Overload resolution is used to select the user-defined conversion to be invoked. Assuming that ``cv1 T'' is the type of the object being initialized, with T a class type, the candidate functions are selected as follows:
2 In both cases, the argument list has one argument, which is the initializer expression. [Note: this argument will be compared against the first parameter of the constructors and against the implicit object parameter of the conversion functions. ]
1 Under the conditions specified in 8.5, as part of an initialization of an object of nonclass type, a conversion function can be invoked to convert an initializer expression of class type to the type of the object being initialized. Overload resolution is used to select the conversion function to be invoked. Assuming that ``cv1 T'' is the type of the object being initialized, and ``cv S'' is the type of the initializer expression, with S a class type, the candidate functions are selected as follows:
2 The argument list has one argument, which is the initializer expression. [Note: this argument will be compared against the implicit object parameter of the conversion functions. ]
1 Under the conditions specified in 8.5.3, a reference can be bound directly to an lvalue that is the result of applying a conversion function to an initializer expression. Overload resolution is used to select the conversion function to be invoked. Assuming that ``cv1 T'' is the underlying type of the reference being initialized, and ``cv S'' is the type of the initializer expression, with S a class type, the candidate functions are selected as follows:
2 The argument list has one argument, which is the initializer expression. [Note: this argument will be compared against the implicit object parameter of the conversion functions. ]
1 From the set of candidate functions constructed for a given context (13.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences for the best fit (13.3.3). The selection of viable functions considers relationships between arguments and function 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.
3 Second, for F to be a viable function, there shall exist for each argument an implicit conversion sequence (13.3.3.1) that converts 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 13.3.3.1.4).
121) According to 8.3.6, parameters following the (m+1) st parameter must also have default arguments. [back to text]
1 Define ICSi(F) as follows:
---end 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-formed123).
3 [Example:
---end example]void Fcn(const int*, short); void Fcn(int*, int); int i; short s = 0; void f() { 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 }
122) If a function is a static member function, this definition means that the first argument, the implied object parameter, has no effect in the determination of whether the function is better or worse than any other function. [back to text]123) 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 another function F that W did not face might be at least as good as W, F cannot be the best function because at some point in the tournament F encountered 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. [back to text]
1 An implicit conversion sequence is a sequence of conversions used to convert an argument in a function call to the type of the corresponding parameter of the function being called. The sequence of conversions is an implicit conversion as defined in clause 4, which means it is governed by the rules for initialization of an object or reference by a single expression (8.5, 8.5.3).
2 Implicit conversion sequences are concerned only with the type, cv-qualification, and lvalue-ness of the argument and how these are converted to match the corresponding properties of the parameter. Other properties, such as the lifetime, storage class, alignment, or accessibility 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 (13.3.1.4, 13.3.1.5), a well-formed implicit conversion sequence is one of the following forms:
4 In the context of an initialization by user-defined conversion (i.e., when considering the argument of a user-defined conversion function; see 13.3.1.4, 13.3.1.5), only standard conversion sequences and ellipsis conversion sequences are allowed.
5 For the case where the parameter type is a reference, see 13.3.3.1.4.
6 When the parameter type is not a reference, the implicit conversion sequence models a copy-initialization of the parameter from the argument expression. The implicit conversion sequence is the one required to convert the argument expression to an rvalue of the type of the parameter. [Note: when the parameter has a class type, this is a conceptual conversion defined for the purposes of clause 13; the actual initialization is defined in terms of constructors and is not a conversion. ] Any difference in top-level cv-qualification is subsumed by the initialization itself and does not constitute a conversion. [Example: a parameter of type A can be initialized from an argument of type const A. The implicit conversion sequence for that case is the identity sequence; it contains no ``conversion'' from const A to A. ] When the parameter has a class type and the argument expression has the same type, the implicit conversion sequence is an identity conversion. When the parameter has a class type and the argument expression has a derived class type, the implicit conversion sequence is a derived-to-base Conversion from the derived class to the base class. [Note: there is no such standard conversion; this derived-to-base Conversion exists only in the description of implicit conversion sequences. ] A derived-to-base Conversion has Conversion rank (13.3.3.1.1).
7 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.
8 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 (13.3.3.1.1).
9 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.
10 If several different sequences of conversions exist that each convert the argument to the parameter type, the implicit conversion sequence associated with the parameter is defined to be the unique conversion sequence designated the ambiguous conversion sequence. For the purpose of ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous conversion sequence is treated as a user-defined sequence that is indistinguishable from any other user-defined conversion sequence124). If a function that uses the ambiguous conversion sequence is selected as the best viable function, the call will be ill-formed because the conversion of one of the arguments in the call is ambiguous.
11 The three forms of implicit conversion sequences mentioned above are defined in the following subclauses.
124) The ambiguous conversion sequence is ranked with user-defined conversion sequences because multiple conversion sequences for an argument can exist only if they involve different user-defined conversions. The ambiguous conversion sequence is indistinguishable from any other user-defined conversion sequence because it represents at least two user-defined conversion sequences, each with a different user-defined conversion, and any other user-defined conversion sequence must be indistinguishable from at least one of them. This rule prevents a function from becoming non-viable because of an ambiguous conversion sequence for one of its parameters. Consider this example,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 resolve to that f(B) because the exact match with f(B) is better than any of the sequences required to match f(A). [back to text]class B; class A { A (B&); }; class B { operator A (); }; class C { C (B&); }; void f(A) { } void f(C) { } B b; f(b); // ambiguous because b -> C via constructor and // b --> A via constructor or conversion function.
1 Table 9 summarizes the conversions defined in clause 4 and partitions them into four disjoint categories: Lvalue Transformation, Qualification Adjustment, Promotion, and Conversion. [Note: these categories 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 [Note: As described in clause 4, a standard conversion sequence is either the Identity conversion by itself (that is, no conversion) or consists of one to three conversions from the other four categories. At most one conversion from each category is allowed in a single standard conversion sequence. If there are two or more conversions in the sequence, the conversions are applied in the canonical order: Lvalue Transformation, Promotion or Conversion, Qualification Adjustment. ---end note]
3 Each conversion in Table 9 also has an associated rank (Exact Match, Promotion, or Conversion). These are used to rank standard conversion sequences (13.3.3.2). 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 (13.3.3.1.4). If any of those has Conversion rank, the sequence has Conversion rank; otherwise, if any of those has Promotion rank, the sequence has Promotion rank; otherwise, the sequence has Exact Match rank.
Table 9---conversions |
_ ___________________________________________________________________________ _ Conversion Category Rank Subclause ___________________________________________________________________________ _ _ ___________________________________________________________________________ ___________________________________________________________________________ _ No conversions required Identity _ ___________ __________________________________________________ _ Lvalue-to-rvalue conversion _ 4.1 ___________________________ ___________ Lvalue Transformation Exact Match _ Array-to-pointer conversion _ 4.2 ___________________________ ___________ _ Function-to-pointer conversion _ 4.3 __________________________________________________ ___________ _ Qualification conversions Qualification Adjustment 4.4 ___________________________________________________________________________ _ Integral promotions Promotion Promotion _ 4.5 ___________________________ ___________ _ Floating point promotion 4.6 ___________________________________________________________________________ _ Integral conversions _ 4.7 ___________________________ ___________ _ Floating point conversions _ 4.8 ___________________________ ___________ _ Floating-integral conversions Conversion Conversion _ 4.9 ___________________________ ___________ _ Pointer conversions _ 4.10 ___________________________ ___________ _ Pointer to member conversions _ 4.11 ___________________________ ___________ _ Boolean conversions 4.12 ___________________________________________________________________________ |
1 A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user-defined conversion (12.3) followed by a second standard conversion sequence. If the user-defined conversion is specified by a constructor (12.3.1), the initial standard conversion sequence converts the source type to the type required by the argument of the constructor. If the user-defined conversion is specified by a conversion function (12.3.2), the initial standard conversion sequence converts the source type to the implicit object parameter of the conversion 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 userdefined conversion sequence (see 13.3.3 and 13.3.3.1).
3 If the user-defined conversion is specified by a template conversion function, the second standard conversion sequence must have exact match rank.
4 A conversion of an expression of class type to the same class type is given Exact Match rank, and a conversion of an expression of class type to a base class of that type is given Conversion rank, in spite of the fact that a copy constructor (i.e., a user-defined conversion function) is called for those cases.
1 An ellipsis conversion sequence occurs when an argument in a function call is matched with the ellipsis parameter specification of the function called.
1 When a parameter of reference type binds directly (8.5.3) to an argument expression, the implicit conversion sequence is the identity conversion, unless the argument expression has a type that is a derived class of the parameter type, in which case the implicit conversion sequence is a derived-to-base Conversion (13.3.3.1). [Example:
---end example] If the parameter binds directly to the result of applying a conversion function to the argument expression, the implicit conversion sequence is a user-defined conversion sequence (13.3.3.1.2), with the second standard conversion sequence either an identity conversion or, if the conversion function returns an entity of a type that is a derived class of the parameter type, a derived-to-base Conversion.struct A {}; struct B : public A {} b; int f(A&); int f(B&); int i = f(b); // Calls f(B&), an exact match, rather than // f(A&), a conversion
2 When a parameter of reference type is not bound directly to an argument expression, the conversion sequence is the one required to convert the argument expression to the underlying type of the reference according to 13.3.3.1. Conceptually, this conversion sequence corresponds to copy-initializing a temporary of the underlying type with the argument expression. Any difference in top-level cv-qualification is subsumed by the initialization itself and does not constitute a conversion.
3 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 13.3.1). [Note: this means, for example, that a candidate function cannot be a viable function if it has a non-const reference parameter (other than the implicit object parameter) and the corresponding argument is a temporary or would require one to be created to initialize the reference (see 8.5.3). ]
4 Other restrictions on binding a reference to a particular argument do not affect the formation of a standard conversion sequence, however. [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 parameter. 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 (8.5.3). ]
5 The binding of a reference to an expression that is reference-compatible with added qualification influences the rank of a standard conversion; see 13.3.3.2 and 8.5.3.
1 13.3.3.2 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 conversion 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 13.3.3.1)
3 Two implicit conversion sequences of the same form are indistinguishable conversion sequences unless one of the following rules apply:
---end example] or, if not that,int f(const int *); int f(int *); int i; int j = f(&i); // Calls f(int *)
---end example]int f(const int &); int f(int &); int g(const int &); int g(int); int i; int j = f(i); // Calls f(int &) int k = g(i); // ambiguous class X { public: void f() const; void f(); }; void g(const X& a, X b) { a.f(); // Calls X::f() const b.f(); // Calls X::f() }
---end example]struct A { operator short(); } a; int f(int); int f(float); int i = f(a); // Calls f(int), because short --> int is // better than short --> float.
4 Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion, which is a better conversion than a Conversion. Two conversion sequences with the same rank are indistinguishable unless one of the following rules applies:
---end example]struct A {}; struct B : public A {}; struct C : public B {}; C *pc; int f(A *); int f(B *); int i = f(pc); // Calls f(B *)
1 A use of an overloaded function name without arguments is resolved in certain contexts to a function, a pointer to function or a pointer to member function for a specific function from the overload set. A function template name is considered to name a set of overloaded functions in such contexts. The function selected is the one whose type matches the target type required in the context. The target can be
2 If the name is a function template, template argument deduction is done (14.8.2.2), and if the argument deduction succeeds, the deduced template arguments are used to generate a single template function, which is added to the set of overloaded functions considered.
3 Non-member functions and static member functions match targets of type ``pointer-to-function'' or ``reference-to-function.'' Nonstatic member functions match targets of type ``pointer-to-member-function;'' the function type of the pointer to member is used to select the member function from the set of overloaded member functions. If a nonstatic member function is selected, the reference to the overloaded function name is required to have the form of a pointer to member as described in 5.3.1.
4 If more than one function is selected, any template functions in the set are eliminated if the set also contains a non-template function, and any given template function is eliminated if the set contains a second template function that is more specialized than the first according to the partial ordering rules of 14.5.5.2. After such eliminations, if any, there shall remain exactly one selected function.
5 [Example:
The initialization of pfe is ill-formed because no f() with type int(...) has been defined, and not because of any ambiguity. For another example,int f(double); int f(int); int (*pfd)(double) = &f; // selects f(double) int (*pfi)(int) = &f; // selects f(int) int (*pfe)(...) = &f; // error: type mismatch int (&rfi)(int) = f; // selects f(int) int (&rfd)(double) = f; // selects f(double) void g() { (int (*)(int))&f; // cast expression as selector }
---end 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
6 [Note: 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). ]
7 [Note: there are no standard conversions (clause 4) of one pointer-to-function type into another. In particular, even if B is a public base of D, we have
---end note]D* f(); B* (*p1)() = &f; // error void g(D*); void (*p2)(B*) = &g; // error
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.
2 Both the unary and binary forms of
can be overloaded.+ - * &
3 The following operators cannot be overloaded:
nor can the preprocessing symbols # and ## (clause 16).. .* :: ?:
4 Operator functions are usually not called directly; instead they are invoked to evaluate the operators they implement (13.5.1 - 13.5.7). They can be explicitly called, however, using the operator-function-id as the name of the function in the function call syntax (5.2.2). [Example:
---end 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 3.7.3. The attributes and restrictions found in the rest of this subclause do not apply to them unless explicitly stated in 3.7.3.
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 class and enumeration types by defining operator functions that implement these operators. Operator functions are inherited in the same manner as other base class functions.
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 (8.3.6), except where explicitly stated below. Operator functions cannot have more or fewer parameters than the number required for the corresponding operator, as described in the rest of this subclause.
9 Operators not mentioned explicitly in subclauses 13.5.3 through 13.5.7 act as ordinary unary and binary operators obeying the rules of 13.5.1 or 13.5.2.
1 A prefix unary operator shall be implemented by a non-static member function (9.3) 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 13.3.1.2 determine which, if any, interpretation is used. See 13.5.7 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. [Note: consequently, a unary operator can hide a binary operator from an enclosing scope, and vice versa. ]
1 A binary operator shall be implemented either by a non-static member function (9.3) 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 13.3.1.2 determines which, if any, interpretation is used.
1 An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a class if not declared by the user (12.8), a base class assignment operator is always hidden by the copy assignment operator of the derived class.
2 Any assignment operator, even the copy assignment operator, can be virtual. [Note: for a derived class D with a base class B for which a virtual copy assignment has been declared, the copy assignment operator in D does not override B's virtual copy assignment operator. [Example:
---end example] ---end note]struct B { virtual int operator= (int); virtual B& operator= (const B&); }; struct D : B { virtual int operator= (int); virtual D& operator= (const B&); }; D dobj1; D dobj2; B* bptr = &dobj1; void f() { bptr->operator=(99); // calls D::operator=(int) *bptr = 99; // ditto bptr->operator=(dobj2); // calls D::operator=(const B&) *bptr = dobj2; // ditto dobj1 = dobj2; // calls implicitly-declared // D::operator=(const D&) }
1 operator() shall be a non-static member function with an arbitrary number of parameters. It can have default arguments. It implements the function call syntax
where the postfix-expression evaluates to a class object and the possibly empty expression-list matches the parameter list of an operator() member function of the class. Thus, a call x(arg1,...) is interpreted as x.operator()(arg1,...) 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 mechanism (13.3.3).postfix-expression ( expression-listopt )
1 operator[] shall be a non-static member function with exactly one parameter. It implements the subscripting syntax
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 resolution mechanism (13.3.3).postfix-expression [ expression ]
1 operator-> shall be a non-static member function taking no parameters. It implements class member access using ->
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 mechanism (13.3).postfix-expression -> id-expression
1 The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters, or a non-member function with one parameter of class or enumeration type, it defines the prefix increment operator ++ for objects of that type. 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 of which shall be of type int), it defines the postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator, the int argument will have value zero.125) [Example:
---end example]class X { public: X& operator++(); // prefix ++a X operator++(int); // postfix a++ }; class Y { }; Y& operator++(Y&); // prefix ++b 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 analogously.
125) Calling operator++ explicitly, as in expressions like a.operator++(2), has no special properties: The argument to operator++ is 2. [back to text]
1 The candidate operator functions that represent the built-in operators defined in clause 5 are specified in this subclause. These candidate functions participate in the operator overload resolution process as described in 13.3.1.2 and are used for no other purpose. [Note: because built-in operators take only operands with non-class type, and operator overload resolution occurs only when an operand expression originally has class or enumeration type, operator overload resolution can resolve to a built-in operator only when an operand has a class type that has a user-defined conversion to a non-class type appropriate for the operator, or when an operand has an enumeration type that can be converted to a type appropriate for the operator. Also note that some of the candidate operator functions given in this subclause are more permissive than the built-in operators themselves. As described in 13.3.1.2, after a built-in operator is selected by overload resolution the expression is subject to the requirements for the built-in operator given in clause 5, and therefore to any additional semantic constraints given there. If there is a user-written candidate with the same name and parameter types as a built-in candidate operator function, the built-in operator function is hidden and is not included in the set of candidate functions. ]
2 In this subclause, the term promoted integral type is used to refer to those integral types which are preserved by integral promotion (including e.g. int and long but excluding e.g. char). Similarly, the term promoted arithmetic type refers to promoted integral types plus floating types. [Note: in all cases where a promoted integral type or promoted arithmetic type is required, an operand of enumeration type will be acceptable by way of the integral promotions. ]
3 For every pair (T, VQ), where T is an arithmetic type, and VQ is either volatile or empty, there exist candidate operator functions of the form
VQ T& operator++(VQ T&); T operator++(VQ T&, int);
4 For every pair (T, VQ), where T is an arithmetic type other than bool, and VQ is either volatile or empty, there exist candidate operator functions of the form
VQ T& operator--(VQ T&); T operator--(VQ T&, int);
5 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object type, and VQ is either volatile or empty, there exist candidate operator functions of the form
T*VQ& operator++(T*VQ&); T*VQ& operator--(T*VQ&); T* operator++(T*VQ&, int); T* operator--(T*VQ&, int);
6 For every cv-qualified or cv-unqualified object type T, there exist candidate operator functions of the form
T& operator*(T*);
7 For every function type T, there exist candidate operator functions of the form
T& operator*(T*);
8 For every type T, there exist candidate operator functions of the form
T* operator+(T*);
9 For every promoted arithmetic type T, there exist candidate operator functions of the form
T operator+(T); T operator-(T);
10 For every promoted integral type T, there exist candidate operator functions of the form
T operator~(T);
11 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1 is the same type as C2 or is a derived class of C2, T is an object type or a function type, and CV1 and CV2 are cv-qualifier-seqs, there exist candidate operator functions of the form
where CV12 is the union of CV1 and CV2.CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
12 For every pair of promoted arithmetic types L and R, there exist candidate operator functions of the form
where LR is the result of the usual arithmetic conversions between types L and R.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);
13 For every cv-qualified or cv-unqualified object type T there exist candidate operator functions of the form
T* operator+(T*, ptrdiff_t); T& operator[](T*, ptrdiff_t); T* operator-(T*, ptrdiff_t); T* operator+(ptrdiff_t, T*); T& operator[](ptrdiff_t, T*);
14 For every T, where T is a pointer to object type, there exist candidate operator functions of the form
ptrdiff_t operator-(T, T);
15 For every pointer or enumeration type T, there exist candidate operator functions of the form
bool operator<(T, T); bool operator>(T, T); bool operator<=(T, T); bool operator>=(T, T); bool operator==(T, T); bool operator!=(T, T);
16 For every pointer to member type T, there exist candidate operator functions of the form
bool operator==(T, T); bool operator!=(T, T);
17 For every pair of promoted integral types L and R, there exist candidate operator functions of the form
where LR is the result of the usual arithmetic conversions between types L and R.LR operator%(L, R); LR operator&(L, R); LR operator^(L, R); LR operator|(L, R); L operator<<(L, R); L operator>>(L, R);
18 For 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 candidate operator functions of the form
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);
19 For every pair (T, VQ), where T is any type and VQ is either volatile or empty, there exist candidate operator functions of the form
T*VQ& operator=(T*VQ&, T*);
20 For every pair (T, VQ), where T is an enumeration or pointer to member type and VQ is either volatile or empty, there exist candidate operator functions of the form
VQ T& operator=(VQ T&, T);
21 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object type and VQ is either volatile or empty, there exist candidate operator functions of the form
T*VQ& operator+=(T*VQ&, ptrdiff_t); T*VQ& operator-=(T*VQ&, ptrdiff_t);
22 For 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 candidate operator functions of the form
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);
23 There also exist candidate operator functions of the form
bool operator!(bool); bool operator&&(bool, bool); bool operator||(bool, bool);
24 For every pair of promoted arithmetic types L and R, there exist candidate operator functions of the form
where LR is the result of the usual arithmetic conversions between types L and R. [Note: as with all these descriptions of candidate functions, this declaration serves only to describe the built-in operator for purposes of overload resolution. The operator ``?'' cannot be overloaded. ]LR operator?(bool, L, R);
25 For every type T, where T is a pointer or pointer-to-member type, there exist candidate operator functions of the form
T operator?(bool, T, T);