overload-call.cpp revision 15da57e66cade0c2cab752f925e838b22daadafc
1// RUN: clang -fsyntax-only -pedantic -verify %s
2int* f(int);
3float* f(float);
4void f();
5
6void test_f(int iv, float fv) {
7  float* fp = f(fv);
8  int* ip = f(iv);
9}
10
11int* g(int, float, int); // expected-note {{ candidate function }}
12float* g(int, int, int); // expected-note {{ candidate function }}
13double* g(int, float, float); // expected-note {{ candidate function }}
14char* g(int, float, ...); // expected-note {{ candidate function }}
15void g();
16
17void test_g(int iv, float fv) {
18  int* ip1 = g(iv, fv, 0);
19  float* fp1 = g(iv, iv, 0);
20  double* dp1 = g(iv, fv, fv);
21  char* cp1 = g(0, 0);
22  char* cp2 = g(0, 0, 0, iv, fv);
23
24  double* dp2 = g(0, fv, 1.5); // expected-error {{ call to 'g' is ambiguous; candidates are: }}
25}
26
27double* h(double f);
28int* h(int);
29
30void test_h(float fv, unsigned char cv) {
31  double* dp = h(fv);
32  int* ip = h(cv);
33}
34
35int* i(int);
36double* i(long);
37
38void test_i(short sv, int iv, long lv, unsigned char ucv) {
39  int* ip1 = i(sv);
40  int* ip2 = i(iv);
41  int* ip3 = i(ucv);
42  double* dp1 = i(lv);
43}
44
45int* j(void*);
46double* j(bool);
47
48void test_j(int* ip) {
49  int* ip1 = j(ip);
50}
51
52int* k(char*);
53double* k(bool);
54
55void test_k() {
56  int* ip1 = k("foo");
57  double* dp1 = k(L"foo");
58}
59
60int* l(wchar_t*);
61double* l(bool);
62
63void test_l() {
64  int* ip1 = l(L"foo");
65  double* dp1 = l("foo");
66}
67
68int* m(const char*);
69double* m(char*);
70
71void test_m() {
72  int* ip = m("foo");
73}
74
75int* n(char*);
76double* n(void*);
77
78void test_n() {
79  char ca[7];
80  int* ip1 = n(ca);
81  int* ip2 = n("foo");
82
83  float fa[7];
84  double* dp1 = n(fa);
85}
86
87enum PromotesToInt {
88  PromotesToIntValue = 1
89};
90
91enum PromotesToUnsignedInt {
92  PromotesToUnsignedIntValue = (unsigned int)-1
93};
94
95int* o(int);
96double* o(unsigned int);
97float* o(long);
98
99void test_o() {
100  int* ip1 = o(PromotesToIntValue);
101  double* dp1 = o(PromotesToUnsignedIntValue);
102}
103
104int* p(int);
105double* p(double);
106
107void test_p() {
108  int* ip = p((short)1);
109  double* dp = p(1.0f);
110}
111
112struct Bits {
113  signed short int_bitfield : 5;
114  unsigned int uint_bitfield : 8;
115};
116
117int* bitfields(int, int);
118float* bitfields(unsigned int, int);
119
120void test_bitfield(Bits bits, int x) {
121  int* ip = bitfields(bits.int_bitfield, 0);
122  float* fp = bitfields(bits.uint_bitfield, 0u);
123}
124
125int* multiparm(long, int, long); // expected-note {{ candidate function }}
126float* multiparm(int, int, int); // expected-note {{ candidate function }}
127double* multiparm(int, int, short); // expected-note {{ candidate function }}
128
129void test_multiparm(long lv, short sv, int iv) {
130  int* ip1 = multiparm(lv, iv, lv);
131  int* ip2 = multiparm(lv, sv, lv);
132  float* fp1 = multiparm(iv, iv, iv);
133  float* fp2 = multiparm(sv, iv, iv);
134  double* dp1 = multiparm(sv, sv, sv);
135  double* dp2 = multiparm(iv, sv, sv);
136  multiparm(sv, sv, lv); // expected-error {{ call to 'multiparm' is ambiguous; candidates are: }}
137}
138
139// Test overloading based on qualification vs. no qualification
140// conversion.
141int* quals1(int const * p);
142char* quals1(int * p);
143
144int* quals2(int const * const * pp);
145char* quals2(int * * pp);
146
147int* quals3(int const * * const * ppp);
148char* quals3(int *** ppp);
149
150void test_quals(int * p, int * * pp, int * * * ppp) {
151  char* q1 = quals1(p);
152  char* q2 = quals2(pp);
153  char* q3 = quals3(ppp);
154}
155
156// Test overloading based on qualification ranking (C++ 13.3.2)p3.
157int* quals_rank1(int const * p);
158float* quals_rank1(int const volatile *p);
159char* quals_rank1(char*);
160double* quals_rank1(const char*);
161
162int* quals_rank2(int const * const * pp);
163float* quals_rank2(int * const * pp);
164
165void quals_rank3(int const * const * const volatile * p); // expected-note{{candidate function}}
166void quals_rank3(int const * const volatile * const * p); // expected-note{{candidate function}}
167
168void quals_rank3(int const *); // expected-note{{candidate function}}
169void quals_rank3(int volatile *); // expected-note{{candidate function}}
170
171void test_quals_ranking(int * p, int volatile *pq, int * * pp, int * * * ppp) {
172  int* q1 = quals_rank1(p);
173  float* q2 = quals_rank1(pq);
174  double* q3 = quals_rank1("string literal");
175  char a[17];
176  const char* ap = a;
177  char* q4 = quals_rank1(a);
178  double* q5 = quals_rank1(ap);
179
180  float* q6 = quals_rank2(pp);
181
182  quals_rank3(ppp); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}}
183
184  quals_rank3(p); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}}
185  quals_rank3(pq);
186}
187
188// Test overloading based on derived-to-base conversions
189class A { };
190class B : public A { };
191class C : public B { };
192class D : public C { };
193
194int* derived1(A*);
195char* derived1(const A*);
196float* derived1(void*);
197
198int* derived2(A*);
199float* derived2(B*);
200
201int* derived3(A*);
202float* derived3(const B*);
203char* derived3(C*);
204
205void test_derived(B* b, B const* bc, C* c, const C* cc, void* v, D* d) {
206  int* d1 = derived1(b);
207  char* d2 = derived1(bc);
208  int* d3 = derived1(c);
209  char* d4 = derived1(cc);
210  float* d5 = derived1(v);
211
212  float* d6 = derived2(b);
213  float* d7 = derived2(c);
214
215  char* d8 = derived3(d);
216}
217
218// Test overloading of references.
219// (FIXME: tests binding to determine candidate sets, not overload
220//  resolution per se).
221int* intref(int&);
222float* intref(const int&);
223
224void intref_test() {
225  float* ir1 = intref(5);
226  float* ir2 = intref(5.5);
227}
228
229// Test reference binding vs. standard conversions.
230int& bind_vs_conv(const double&);
231float& bind_vs_conv(int);
232
233void bind_vs_conv_test()
234{
235  int& i1 = bind_vs_conv(1.0f);
236  float& f1 = bind_vs_conv((short)1);
237}
238
239// Test that cv-qualifiers get subsumed in the reference binding.
240struct X { };
241struct Y { };
242struct Z : X, Y { };
243
244int& cvqual_subsume(X&); // expected-note{{candidate function}}
245float& cvqual_subsume(const Y&); // expected-note{{candidate function}}
246
247int& cvqual_subsume2(const X&);
248float& cvqual_subsume2(const volatile Y&);
249
250Z get_Z();
251
252void cvqual_subsume_test(Z z) {
253  cvqual_subsume(z); // expected-error{{call to 'cvqual_subsume' is ambiguous; candidates are:}}
254  int& x = cvqual_subsume2(get_Z()); // okay: only binds to the first one
255}
256