Clang Project

clang_source_code/test/SemaCXX/overload-call.cpp
1// RUN: %clang_cc1 -triple %itanium_abi_triple -pedantic -verify %s
2// RUN: %clang_cc1 -triple %itanium_abi_triple -pedantic -verify -std=c++98 %s
3// RUN: %clang_cc1 -triple %itanium_abi_triple -pedantic -verify -std=c++11 %s
4
5int* f(int) { return 0; }
6float* f(float) { return 0; }
7void f();
8
9void test_f(int iv, float fv) {
10  float* fp = f(fv);
11  int* ip = f(iv);
12}
13
14int* g(int, float, int); // expected-note {{candidate function}}
15float* g(int, int, int); // expected-note {{candidate function}}
16double* g(int, float, float); // expected-note {{candidate function}}
17char* g(int, float, ...); // expected-note {{candidate function}}
18void g();
19
20void test_g(int iv, float fv) {
21  int* ip1 = g(iv, fv, 0);
22  float* fp1 = g(iv, iv, 0);
23  double* dp1 = g(iv, fv, fv);
24  char* cp1 = g(0, 0);
25  char* cp2 = g(0, 0, 0, iv, fv);
26
27  double* dp2 = g(0, fv, 1.5); // expected-error {{call to 'g' is ambiguous}}
28}
29
30double* h(double f);
31int* h(int);
32
33void test_h(float fv, unsigned char cv) {
34  double* dp = h(fv);
35  int* ip = h(cv);
36}
37
38int* i(int);
39double* i(long);
40
41void test_i(short sv, int iv, long lv, unsigned char ucv) {
42  int* ip1 = i(sv);
43  int* ip2 = i(iv);
44  int* ip3 = i(ucv);
45  double* dp1 = i(lv);
46}
47
48int* j(void*);
49double* j(bool);
50
51void test_j(int* ip) {
52  int* ip1 = j(ip);
53}
54
55int* k(char*);
56double* k(bool);
57
58void test_k() {
59  int* ip1 = k("foo");
60#if __cplusplus <= 199711L
61  // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}}
62#else
63  // expected-error@-4 {{cannot initialize a variable of type 'int *' with an rvalue of type 'double *'}}
64#endif
65
66  int* ip2 = k(("foo"));
67#if __cplusplus <= 199711L
68  // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}}
69#else
70  // expected-error@-4 {{cannot initialize a variable of type 'int *' with an rvalue of type 'double *'}}
71#endif
72  double* dp1 = k(L"foo");
73}
74
75int* l(wchar_t*);
76double* l(bool);
77
78void test_l() {
79  int* ip1 = l(L"foo");
80#if __cplusplus <= 199711L
81  // expected-warning@-2 {{conversion from string literal to 'wchar_t *' is deprecated}}
82#else
83  // expected-error@-4 {{cannot initialize a variable of type 'int *' with an rvalue of type 'double *'}}
84#endif
85  double* dp1 = l("foo");
86}
87
88int* m(const char*);
89double* m(char*);
90
91void test_m() {
92  int* ip = m("foo");
93}
94
95int* n(char*);
96double* n(void*);
97class E;
98
99void test_n(E* e) {
100  char ca[7];
101  int* ip1 = n(ca);
102  int* ip2 = n("foo");
103#if __cplusplus <= 199711L
104  // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}}
105#else
106  // expected-warning@-4 {{ISO C++11 does not allow conversion from string literal to 'char *'}}
107#endif
108  float fa[7];
109  double* dp1 = n(fa);
110
111  double* dp2 = n(e);
112}
113
114enum PromotesToInt {
115  PromotesToIntValue = -1
116};
117
118enum PromotesToUnsignedInt {
119  PromotesToUnsignedIntValue = __INT_MAX__ * 2U
120};
121
122int* o(int);
123double* o(unsigned int);
124float* o(long);
125
126void test_o() {
127  int* ip1 = o(PromotesToIntValue);
128  double* dp1 = o(PromotesToUnsignedIntValue);
129}
130
131int* p(int);
132double* p(double);
133
134void test_p() {
135  int* ip = p((short)1);
136  double* dp = p(1.0f);
137}
138
139struct Bits {
140  signed short int_bitfield : 5;
141  unsigned int uint_bitfield : 8;
142};
143
144int* bitfields(int, int);
145float* bitfields(unsigned int, int);
146
147void test_bitfield(Bits bits, int x) {
148  int* ip = bitfields(bits.int_bitfield, 0);
149  float* fp = bitfields(bits.uint_bitfield, 0u);
150}
151
152int* multiparm(long, int, long); // expected-note {{candidate function}}
153float* multiparm(int, int, int); // expected-note {{candidate function}}
154double* multiparm(int, int, short); // expected-note {{candidate function}}
155
156void test_multiparm(long lv, short sv, int iv) {
157  int* ip1 = multiparm(lv, iv, lv);
158  int* ip2 = multiparm(lv, sv, lv);
159  float* fp1 = multiparm(iv, iv, iv);
160  float* fp2 = multiparm(sv, iv, iv);
161  double* dp1 = multiparm(sv, sv, sv);
162  double* dp2 = multiparm(iv, sv, sv);
163  multiparm(sv, sv, lv); // expected-error {{call to 'multiparm' is ambiguous}}
164}
165
166// Test overloading based on qualification vs. no qualification
167// conversion.
168int* quals1(int const * p);
169char* quals1(int * p);
170
171int* quals2(int const * const * pp);
172char* quals2(int * * pp);
173
174int* quals3(int const * * const * ppp);
175char* quals3(int *** ppp);
176
177void test_quals(int * p, int * * pp, int * * * ppp) {
178  char* q1 = quals1(p);
179  char* q2 = quals2(pp);
180  char* q3 = quals3(ppp);
181}
182
183// Test overloading based on qualification ranking (C++ 13.3.2)p3.
184int* quals_rank1(int const * p);
185float* quals_rank1(int const volatile *p);
186char* quals_rank1(char*);
187double* quals_rank1(const char*);
188
189int* quals_rank2(int const * const * pp);
190float* quals_rank2(int * const * pp);
191
192void quals_rank3(int const * const * const volatile * p); // expected-note{{candidate function}}
193void quals_rank3(int const * const volatile * const * p); // expected-note{{candidate function}}
194
195void quals_rank3(int const *); // expected-note{{candidate function}}
196void quals_rank3(int volatile *); // expected-note{{candidate function}}
197
198void test_quals_ranking(int * p, int volatile *pq, int * * pp, int * * * ppp) {
199  int* q1 = quals_rank1(p);
200  float* q2 = quals_rank1(pq); 
201  double* q3 = quals_rank1("string literal");
202  char a[17];
203  const char* ap = a;
204  char* q4 = quals_rank1(a);
205  double* q5 = quals_rank1(ap);
206
207  float* q6 = quals_rank2(pp);
208
209  quals_rank3(ppp); // expected-error {{call to 'quals_rank3' is ambiguous}}
210
211  quals_rank3(p); // expected-error {{call to 'quals_rank3' is ambiguous}}
212  quals_rank3(pq);
213}
214
215// Test overloading based on derived-to-base conversions
216class A { };
217class B : public A { };
218class C : public B { };
219class D : public C { };
220
221int* derived1(A*);
222char* derived1(const A*);
223float* derived1(void*);
224
225int* derived2(A*);
226float* derived2(B*);
227
228int* derived3(A*);
229float* derived3(const B*);
230char* derived3(C*);
231
232void test_derived(B* b, B const* bc, C* c, const C* cc, void* v, D* d) {
233  int* d1 = derived1(b);
234  char* d2 = derived1(bc);
235  int* d3 = derived1(c);
236  char* d4 = derived1(cc);
237  float* d5 = derived1(v);
238
239  float* d6 = derived2(b);
240  float* d7 = derived2(c);
241
242  char* d8 = derived3(d);
243}
244
245void derived4(C*); // expected-note{{candidate function not viable: cannot convert from base class pointer 'A *' to derived class pointer 'C *' for 1st argument}}
246
247void test_base(A* a) {
248  derived4(a); // expected-error{{no matching function for call to 'derived4}}
249}
250
251// Test overloading of references. 
252// (FIXME: tests binding to determine candidate sets, not overload 
253//  resolution per se).
254int* intref(int&);
255float* intref(const int&);
256
257void intref_test() {
258  float* ir1 = intref(5);
259  float* ir2 = intref(5.5); // expected-warning{{implicit conversion from 'double' to 'int' changes value from 5.5 to 5}}
260}
261
262void derived5(C&); // expected-note{{candidate function not viable: cannot bind base class object of type 'A' to derived class reference 'C &' for 1st argument}}
263
264void test_base(A& a) {
265  derived5(a); // expected-error{{no matching function for call to 'derived5}}
266}
267
268// Test reference binding vs. standard conversions.
269int& bind_vs_conv(const double&);
270float& bind_vs_conv(int);
271
272void bind_vs_conv_test()
273{
274  int& i1 = bind_vs_conv(1.0f);
275  float& f1 = bind_vs_conv((short)1);
276}
277
278// Test that cv-qualifiers get subsumed in the reference binding.
279struct X { };
280struct Y { };
281struct Z : X, Y { };
282
283int& cvqual_subsume(X&); // expected-note{{candidate function}}
284float& cvqual_subsume(const Y&); // expected-note{{candidate function}}
285
286int& cvqual_subsume2(X&); // expected-note{{candidate function}}
287float& cvqual_subsume2(volatile Y&); // expected-note{{candidate function}}
288
289void cvqual_subsume_test(Z z) {
290  cvqual_subsume(z); // expected-error{{call to 'cvqual_subsume' is ambiguous}}
291  cvqual_subsume2(z); // expected-error{{call to 'cvqual_subsume2' is ambiguous}}
292}
293
294// Test overloading with cv-qualification differences in reference
295// binding.
296int& cvqual_diff(X&);
297float& cvqual_diff(const X&);
298
299void cvqual_diff_test(X x, Z z) {
300  int& i1 = cvqual_diff(x);
301  int& i2 = cvqual_diff(z);
302}
303
304// Test overloading with derived-to-base differences in reference
305// binding.
306struct Z2 : Z { };
307
308int& db_rebind(X&);
309long& db_rebind(Y&);
310float& db_rebind(Z&);
311
312void db_rebind_test(Z2 z2) {
313  float& f1 = db_rebind(z2);
314}
315
316class string { };
317class opt : public string { };
318
319struct SR {
320  SR(const string&);
321};
322
323void f(SR) { }
324
325void g(opt o) {
326  f(o);
327}
328
329
330namespace PR5756 {
331  int &a(void*, int);
332  float &a(void*, float);
333  void b() { 
334    int &ir = a(0,0);
335    (void)ir;
336  }
337}
338
339// Tests the exact text used to note the candidates
340namespace test1 {
341template <class T>
342void foo(T t, unsigned N);                        // expected-note {{candidate function template not viable: no known conversion from 'const char [6]' to 'unsigned int' for 2nd argument}}
343void foo(int n, char N);                          // expected-note {{candidate function not viable: no known conversion from 'const char [6]' to 'char' for 2nd argument}}
344void foo(int n, const char *s, int t);            // expected-note {{candidate function not viable: requires 3 arguments, but 2 were provided}}
345void foo(int n, const char *s, int t, ...);       // expected-note {{candidate function not viable: requires at least 3 arguments, but 2 were provided}}
346void foo(int n, const char *s, int t, int u = 0); // expected-note {{candidate function not viable: requires at least 3 arguments, but 2 were provided}}
347
348// PR 11857
349void foo(int n);                // expected-note {{candidate function not viable: requires single argument 'n', but 2 arguments were provided}}
350void foo(unsigned n = 10);      // expected-note {{candidate function not viable: allows at most single argument 'n', but 2 arguments were provided}}
351void bar(int n, int u = 0);     // expected-note {{candidate function not viable: requires at least argument 'n', but no arguments were provided}}
352void baz(int n = 0, int u = 0); // expected-note {{candidate function not viable: requires at most 2 arguments, but 3 were provided}}
353
354void test() {
355  foo(4, "hello"); //expected-error {{no matching function for call to 'foo'}}
356  bar();           //expected-error {{no matching function for call to 'bar'}}
357  baz(3, 4, 5);    // expected-error {{no matching function for call to 'baz'}}
358  }
359}
360
361// PR 6014
362namespace test2 {
363  struct QFixed {
364    QFixed(int i);
365    QFixed(long i);
366  };
367
368  bool operator==(const QFixed &f, int i);
369
370  class qrgb666 {
371    inline operator unsigned int () const;
372
373    inline bool operator==(const qrgb666 &v) const;
374    inline bool operator!=(const qrgb666 &v) const { return !(*this == v); }
375  };
376}
377
378// PR 6117
379namespace IncompleteConversion {
380  struct Complete {};
381  struct Incomplete;
382
383  void completeFunction(Complete *); // expected-note 2 {{cannot convert argument of incomplete type}}
384  void completeFunction(Complete &); // expected-note 2 {{cannot convert argument of incomplete type}}
385  
386  void testTypeConversion(Incomplete *P) {
387    completeFunction(P); // expected-error {{no matching function for call to 'completeFunction'}}
388    completeFunction(*P); // expected-error {{no matching function for call to 'completeFunction'}}
389  }
390  
391  void incompletePointerFunction(Incomplete *); // expected-note {{candidate function not viable: cannot convert argument of incomplete type 'IncompleteConversion::Incomplete' to 'IncompleteConversion::Incomplete *' for 1st argument; take the address of the argument with &}}
392  void incompleteReferenceFunction(Incomplete &); // expected-note {{candidate function not viable: cannot convert argument of incomplete type 'IncompleteConversion::Incomplete *' to 'IncompleteConversion::Incomplete &' for 1st argument; dereference the argument with *}}
393  
394  void testPointerReferenceConversion(Incomplete &reference, Incomplete *pointer) {
395    incompletePointerFunction(reference); // expected-error {{no matching function for call to 'incompletePointerFunction'}}
396    incompleteReferenceFunction(pointer); // expected-error {{no matching function for call to 'incompleteReferenceFunction'}}
397  }
398}
399
400namespace DerivedToBaseVsVoid {
401  struct A { };
402  struct B : A { };
403  
404  float &f(void *);
405  int &f(const A*);
406  
407  void g(B *b) {
408    int &ir = f(b);
409  }
410}
411
412// PR 6398 + PR 6421
413namespace test4 {
414  class A;
415  class B {
416    static void foo(); // expected-note {{not viable}}
417    static void foo(int*); // expected-note {{not viable}}
418    static void foo(long*); // expected-note {{not viable}}
419
420    void bar(A *a) { 
421      foo(a); // expected-error {{no matching function for call}}
422    }
423  };
424}
425
426namespace DerivedToBase {
427  struct A { };
428  struct B : A { };
429  struct C : B { };
430  
431  int &f0(const A&);
432  float &f0(B);
433  
434  void g() {
435    float &fr = f0(C());
436  }
437}
438
439namespace PR6483 {
440  struct X0 {
441    operator const unsigned int & () const;
442  };
443
444  struct X1 {
445    operator unsigned int & () const;
446  };
447
448  void f0(const bool &);
449  void f1(bool &); // expected-note 2{{not viable}}
450
451  void g(X0 x0, X1 x1) {
452    f0(x0);
453    f1(x0); // expected-error{{no matching function for call}}
454    f0(x1);
455    f1(x1); // expected-error{{no matching function for call}}
456  }  
457}
458
459namespace PR6078 {
460  struct A {
461    A(short); // expected-note{{candidate constructor}}
462    A(long); // expected-note{{candidate constructor}}
463  };
464  struct S {
465    typedef void ft(A);
466    operator ft*();
467  };
468
469  void f() {
470    S()(0); // expected-error{{conversion from 'int' to 'PR6078::A' is ambiguous}}
471  }
472}
473
474namespace PR6177 {
475  struct String { String(char const*); };
476
477  void f(bool const volatile&);
478  int &f(String);
479
480  void g() { int &r = f(""); }
481}
482
483namespace PR7095 {
484  struct X { };
485
486  struct Y {
487    operator const X*();
488
489  private:
490    operator X*();
491  };
492
493  void f(const X *);
494  void g(Y y) { f(y); }
495}
496
497namespace PR7224 {
498  class A {};
499  class B : public A {};
500
501  int &foo(A *const d);
502  float &foo(const A *const d);
503
504  void bar()
505  {
506    B *const d = 0;
507    B const *const d2 = 0;
508    int &ir = foo(d);
509    float &fr = foo(d2);
510  }
511}
512
513namespace NontrivialSubsequence {
514  struct X0;
515
516  class A {
517    operator X0 *();
518  public:
519    operator const X0 *();
520  };
521 
522  A a;
523  void foo( void const * );
524
525  void g() {
526    foo(a);
527  }
528}
529
530// rdar://rdar8499524
531namespace rdar8499524 {
532  struct W {};
533  struct S {
534      S(...);
535  };
536
537  void g(const S&);
538  void f() {
539    g(W());
540  }
541}
542
543namespace rdar9173984 {
544  template <typename T, unsigned long N> int &f(const T (&)[N]);
545  template <typename T> float &f(const T *);
546
547  void test() {
548    int arr[2] = {0, 0};
549    int *arrp = arr;
550    int &ir = f(arr);
551    float &fr = f(arrp);
552  }
553}
554
555namespace PR9507 {
556  void f(int * const&); // expected-note{{candidate function}}
557  void f(int const(&)[1]); // expected-note{{candidate function}}
558 
559  int main() {
560    int n[1];
561    f(n); // expected-error{{call to 'f' is ambiguous}}
562  }
563}
564
565namespace rdar9803316 {
566  void foo(float);
567  int &foo(int);
568
569  void bar() {
570    int &ir = (&foo)(0);
571  }
572}
573
574namespace IncompleteArg {
575  // Ensure that overload resolution attempts to complete argument types when
576  // performing ADL.
577  template<typename T> struct S {
578    friend int f(const S&);
579  };
580  extern S<int> s;
581  int k = f(s);
582
583  template<typename T> struct Op {
584    friend bool operator==(const Op &, const Op &);
585  };
586  extern Op<char> op;
587  bool b = op == op;
588
589  // ... and not in other cases! Nothing here requires U<int()> to be complete.
590  // (Note that instantiating U<int()> will fail.)
591  template<typename T> struct U {
592    T t;
593  };
594  struct Consumer {
595    template<typename T>
596    int operator()(const U<T> &);
597  };
598  template<typename T> U<T> &make();
599  Consumer c;
600  int n = sizeof(c(make<int()>()));
601}
602
603namespace PR12142 {
604  void fun(int (*x)[10]); // expected-note{{candidate function not viable: 1st argument ('const int (*)[10]') would lose const qualifier}}
605  void g() { fun((const int(*)[10])0); } // expected-error{{no matching function for call to 'fun'}}
606}
607
608// DR1152: Take 'volatile' into account when handling reference bindings in
609//         overload resolution.
610namespace PR12931 {
611  void f(const int &, ...);
612  void f(const volatile int &, int);
613  void g() { f(0, 0); }
614}
615
616void test5() {
617  struct {
618    typedef void F1(int);
619    typedef void F2(double);
620    operator F1*();  // expected-note{{conversion candidate}}
621    operator F2*();  // expected-note{{conversion candidate}}
622  } callable;
623  callable();  // expected-error{{no matching function for call}}
624}
625
626namespace PR20218 {
627  void f(void (*const &)()); // expected-note 2{{candidate}}
628  void f(void (&&)()) = delete; // expected-note 2{{candidate}}
629#if __cplusplus <= 199711L
630  // expected-warning@-2 {{rvalue references are a C++11 extension}}
631  // expected-warning@-3 {{deleted function definitions are a C++11 extension}}
632#endif
633  void g(void (&&)()) = delete; // expected-note 2{{candidate}}
634#if __cplusplus <= 199711L
635  // expected-warning@-2 {{rvalue references are a C++11 extension}}
636  // expected-warning@-3 {{deleted function definitions are a C++11 extension}}
637#endif
638  void g(void (*const &)()); // expected-note 2{{candidate}}
639
640  void x();
641  typedef void (&fr)();
642  struct Y { operator fr(); } y;
643
644  void h() {
645    f(x); // expected-error {{ambiguous}}
646    g(x); // expected-error {{ambiguous}}
647    f(y); // expected-error {{ambiguous}}
648    g(y); // expected-error {{ambiguous}}
649  }
650}
651
652namespace StringLiteralToCharAmbiguity {
653  void f(char *, int);
654  void f(const char *, unsigned);
655  void g() { f("foo", 0); }
656#if __cplusplus <= 199711L
657  // expected-error@-2 {{call to 'f' is ambiguous}}
658  // expected-note@-5 {{candidate function}}
659  // expected-note@-5 {{candidate function}}
660#endif
661}
662
663namespace ProduceNotesAfterSFINAEFailure {
664  struct A {
665    template<typename T, typename U = typename T::x> A(T); // expected-warning 0-1{{extension}}
666  };
667  void f(void*, A); // expected-note {{candidate function not viable}}
668  void g() { f(1, 2); } // expected-error {{no matching function}}
669}
670
671namespace PR19808 {
672  struct B {
673    int i;
674    void bar();
675  };
676  struct D : public B{};
677
678  void f(bool);
679  void f(int D::*);
680  void f(void (D::*)());
681
682  void Usage() {
683    int B::*pmem;
684    void (B::*pmf)();
685
686    // These should not be ambiguous.
687    f(pmem);
688    f(pmf);
689  }
690}
691