2 /* 1. A standard covnersion sequence is better than a user-defined sequence
3 which is better than an elipses conversion sequence. */
6 class B
: public A
{public: operator int (){ return 1;}};
8 // standard vs user-defined
9 int foo0 (int) { return 10; }
10 int foo1 (int) { return 11; } // B -> int : user defined
11 int foo1 (A
) { return 12; } // B -> A : standard
17 // user-defined vs ellipsis
18 int foo2 (int) { return 13;} // B -> int : user defined
19 int foo2 (...) { return 14;} // B -> ... : ellipsis
25 /* 2. Standard Conversion squence S1 is better than standard Conversion
28 // - S1 has a better rank than S2
29 // see overload.exp for more comprehensive testing of this.
30 int foo3 (double) { return 21; } // float->double is 'promotion rank'
31 int foo3 (int) { return 22; } // float->int is 'conversion rank'
33 return foo3 (1.0f
); // 21
36 // - S1 and S2 are both 'qualification conversions' but S1 cv-qualification
37 // is a subset of S2 cv-qualification.
38 int foo4 (const volatile int*) { return 23; }
39 int foo4 ( volatile int*) { return 24; }
42 return foo4(&a
); // 24
45 // - S1 and S2 have the same rank but:
46 // - S2 is a conversion of pointer or memeber-pointer to bool
47 int foo5 (bool) { return 25; }
48 int foo5 (void*) { return 26; }
54 // - Class B publicly extends class A and S1 is a conversion of
55 // B* to A* and S2 is a conversion B* to void*
56 int foo6 (void*) { return 27; }
57 int foo6 (A
*) { return 28; }
60 return foo6(bp
); // 28
63 // - Class C publicly extends Class B which publicly extends
64 // class A and S1 is a conversion of C* to B* and S2 is a
65 // conversion C* to A*.
67 int foo7 (A
*) { return 29; }
68 int foo7 (B
*) { return 210; }
71 return foo7(cp
); // 210
74 // - Same as above but for references.
75 int foo8 (A
&) { return 211; }
76 int foo8 (B
&) { return 212; }
79 return foo8(c
); // 212
82 // - Same as above but passing by copy.
83 int foo9 (A
) { return 213; }
84 int foo9 (B
) { return 214; }
87 return foo9(c
); // 212
90 // - S1 is a conversion of A::* to B::* and S2 is a conversion of
92 int foo10 (void (C::*)()) { return 215; }
93 int foo10 (void (B::*)()) { return 216; }
96 return foo10(amp
); // 216
99 // - S1 is a subsequence of S2
100 int foo101 (volatile const char*) { return 217; } // array-to-pointer conversion
101 // plus qualification conversion
102 int foo101 ( const char*) { return 218; } // array-to-pointer conversion
105 return foo101("abc"); // 216
108 /* 3. User defined conversion U1 is better than user defined Conversion U2,
109 if U1 and U2 are using the same conversion function but U1 has a better
110 second standard conversion sequence than U2. */
111 class D
{public: operator short(){ return 0;}};
112 int foo11 (float) { return 31; }
113 int foo11 (int) { return 32; }
116 return foo11(d
); // 32
119 /* 4. Function Level Ranking.
120 All else being equal some functions are preferred by overload resolution.
121 Function F1 is better than function F2 if: */
122 // - F1 is a non-template function and F2 is a template function
123 template<class T
> int foo12(T
) { return 41; }
124 int foo12(int) { return 42; }
126 return foo12(1); //42
129 // - F1 is a more specialized template instance
130 template<class T
> int foo13(T
) { return 43; }
131 template<class T
> int foo13(T
*) { return 44; }
134 return foo13(c
); // 44
137 // - The context is user defined conversion and F1 has
138 // a better return type than F2
141 operator double () {return 45; }
142 operator int () {return 46; }
144 int foo14 (int a
) {return a
;}
147 return foo14(e
); // 46
206 return 0; // end of main