Стандарт C++ 98 (1119566), страница 63
Текст из файла (страница 63)
[Example: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—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), withthe second standard conversion sequence either an identity conversion or, if the conversion function returnsan entity of a type that is a derived class of the parameter type, a derived-to-base Conversion.2When a parameter of reference type is not bound directly to an argument expression, the conversionsequence is the one required to convert the argument expression to the underlying type of the referenceaccording to 13.3.3.1. Conceptually, this conversion sequence corresponds to copy-initializing a temporaryof 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.3A standard conversion sequence cannot be formed if it requires binding a reference to non-const to anrvalue (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-constreference 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).
]4Other restrictions on binding a reference to a particular argument do not affect the formation of a standardconversion sequence, however. [Example: a function with a “reference to int” parameter can be a viablecandidate even if the corresponding argument is an int bit-field. The formation of implicit conversionsequences treats the int bit-field as an int lvalue and finds an exact match with the parameter. If thefunction 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).
]5The binding of a reference to an expression that is reference-compatible with added qualification influencesthe rank of a standard conversion; see 13.3.3.2 and 8.5.3.13.3.3.2 Ranking implicit conversion sequences[over.ics.rank]113.3.3.2 defines a partial ordering of implicit conversion sequences based on the relationships betterconversion sequence and better conversion.
If an implicit conversion sequence S1 is defined by these rulesto be a better conversion sequence than S2, then it is also the case that S2 is a worse conversion sequencethan S1. If conversion sequence S1 is neither better than nor worse than conversion sequence S2, S1 andS2 are said to be indistinguishable conversion sequences.2When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1)— a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence, and— a user-defined conversion sequence (13.3.3.1.2) is a better conversion sequence than an ellipsis conversion sequence (13.3.3.1.3).3Two implicit conversion sequences of the same form are indistinguishable conversion sequences unless oneof the following rules apply:— Standard conversion sequence S1 is a better conversion sequence than standard conversion sequenceS2 if224© ISO/IECISO/IEC 14882:1998(E)13 Overloading13.3.3.2 Ranking implicit conversion sequences— S1 is a proper subsequence of S2 (comparing the conversion sequences in the canonical formdefined by 13.3.3.1.1, excluding any Lvalue Transformation; the identity conversion sequence isconsidered to be a subsequence of any non-identity conversion sequence) or, if not that,— the rank of S1 is better than the rank of S2 (by the rules defined below), or, if not that,— S1 and S2 differ only in their qualification conversion and yield similar types T1 and T2 (4.4),respectively, and the cv-qualification signature of type T1 is a proper subset of the cv-qualificationsignature of type T2, [Example:intintintintf(const int *);f(int *);i;j = f(&i);// Calls f(int *)—end example] or, if not that,— S1 and S2 are reference bindings (8.5.3), and the types to which the references refer are the sametype except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers ismore cv-qualified than the type to which the reference initialized by S1 refers.
[Example:intintintintf(const int &);f(int &);g(const int &);g(int);int i;int j = f(i);int k = g(i);class X {public:void f() const;void f();};void g(const X& a, X b){a.f();b.f();}// Calls f(int &)// ambiguous// Calls X::f() const// Calls X::f()—end example]— 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 function or constructor and if thesecond standard conversion sequence of U1 is better than the second standard conversion sequence ofU2.
[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.—end example]4Standard 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 areindistinguishable unless one of the following rules applies:— A conversion that is not a conversion of a pointer, or pointer to member, to bool is better than anotherconversion that is such a conversion.225ISO/IEC 14882:1998(E)© ISO/IEC13.3.3.2 Ranking implicit conversion sequences13 Overloading— If class B is derived directly or indirectly from class A, conversion of B* to A* is better than conversionof B* to void*, and conversion of A* to void* 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 fromB,— conversion of C* to B* is better than conversion of C* to A*, [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 *)—end example]— binding of an expression of type C to a reference of type B& is better than binding an expression oftype C to a reference of type A&,— conversion of A::* to B::* is better than conversion of A::* to C::*,— conversion of C to B is better than conversion of C to A,— conversion of B* to A* is better than conversion of C* to A*,— binding of an expression of type B to a reference of type A& is better than binding an expression oftype C to a reference of type A&,— conversion of B::* to C::* is better than conversion of A::* to C::*, and— conversion of B to A is better than conversion of C to A.[Note: compared conversion sequences will have different source types only in the context of comparingthe second standard conversion sequence of an initialization by user-defined conversion (see 13.3.3); in allother contexts, the source types will be the same and the target types will be different.
]13.4 Address of overloaded function1[over.over]A use of an overloaded function name without arguments is resolved in certain contexts to a function, apointer 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 functionselected is the one whose type matches the target type required in the context. The target can be— an object or reference being initialized (8.5, 8.5.3),— the left side of an assignment (5.17),— a parameter of a function (5.2.2),— a parameter of a user-defined operator (13.5),— the return value of a function, operator function, or conversion (6.6.3), or— an explicit type conversion (5.2.3, 5.2.9, 5.4).The overloaded function name can be preceded by the & operator.
An overloaded function name shall notbe used without arguments in contexts other than those listed. [Note: any redundant set of parentheses surrounding the overloaded function name is ignored (5.1). ]2If the name is a function template, template argument deduction is done (14.8.2.2), and if the argumentdeduction succeeds, the deduced template arguments are used to generate a single template function, whichis added to the set of overloaded functions considered.226© ISO/IECISO/IEC 14882:1998(E)13 Overloading13.4 Address of overloaded function3Non-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 overloadedmember functions. If a nonstatic member function is selected, the reference to the overloaded functionname is required to have the form of a pointer to member as described in 5.3.1.4If more than one function is selected, any template functions in the set are eliminated if the set also containsa non-template function, and any given template function is eliminated if the set contains a second templatefunction that is more specialized than the first according to the partial ordering rules of 14.5.5.2.
After sucheliminations, if any, there shall remain exactly one selected function.5[Example:int f(double);int f(int);int (*pfd)(double) = &f;int (*pfi)(int) = &f;int (*pfe)(...) = &f;int (&rfi)(int) = f;int (&rfd)(double) = f;void g() {(int (*)(int))&f;}// selects f(double)// selects f(int)// error: type mismatch// selects f(int)// selects f(double)// cast expression as selectorThe initialization of pfe is ill-formed because no f() with type int(...) has been defined, and notbecause of any ambiguity.
For another example,struct X {int f(int);static int f(long);};int (X::*p1)(int)int(*p2)(int)int(*p3)(long)int (X::*p4)(long)int (X::*p5)(int)int=====&X::f;&X::f;&X::f;&X::f;&(X::f);(*p6)(long) = &(X::f);// OK// error: mismatch// OK// error: mismatch// error: wrong syntax for// pointer to member// OK—end example]6[Note: if f() and g() are both overloaded functions, the cross product of possibilities must be consideredto resolve f(&g), or the equivalent expression f(g).