overload-call.cpp revision 1984eb9a1522ad56e1310643a85f66b2b3424c91
1// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s 2int* f(int) { return 0; } 3float* f(float) { return 0; } 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"); // expected-warning{{conversion from string literal to 'char *' is deprecated}} 57 int* ip2 = k(("foo")); // expected-warning{{conversion from string literal to 'char *' is deprecated}} 58 double* dp1 = k(L"foo"); 59} 60 61int* l(wchar_t*); 62double* l(bool); 63 64void test_l() { 65 int* ip1 = l(L"foo"); // expected-warning{{conversion from string literal to 'wchar_t *' is deprecated}} 66 double* dp1 = l("foo"); 67} 68 69int* m(const char*); 70double* m(char*); 71 72void test_m() { 73 int* ip = m("foo"); 74} 75 76int* n(char*); 77double* n(void*); 78class E; 79 80void test_n(E* e) { 81 char ca[7]; 82 int* ip1 = n(ca); 83 int* ip2 = n("foo"); // expected-warning{{conversion from string literal to 'char *' is deprecated}} 84 85 float fa[7]; 86 double* dp1 = n(fa); 87 88 double* dp2 = n(e); 89} 90 91enum PromotesToInt { 92 PromotesToIntValue = -1 93}; 94 95enum PromotesToUnsignedInt { 96 PromotesToUnsignedIntValue = __INT_MAX__ * 2U 97}; 98 99int* o(int); 100double* o(unsigned int); 101float* o(long); 102 103void test_o() { 104 int* ip1 = o(PromotesToIntValue); 105 double* dp1 = o(PromotesToUnsignedIntValue); 106} 107 108int* p(int); 109double* p(double); 110 111void test_p() { 112 int* ip = p((short)1); 113 double* dp = p(1.0f); 114} 115 116struct Bits { 117 signed short int_bitfield : 5; 118 unsigned int uint_bitfield : 8; 119}; 120 121int* bitfields(int, int); 122float* bitfields(unsigned int, int); 123 124void test_bitfield(Bits bits, int x) { 125 int* ip = bitfields(bits.int_bitfield, 0); 126 float* fp = bitfields(bits.uint_bitfield, 0u); 127} 128 129int* multiparm(long, int, long); // expected-note {{ candidate function }} 130float* multiparm(int, int, int); // expected-note {{ candidate function }} 131double* multiparm(int, int, short); // expected-note {{ candidate function }} 132 133void test_multiparm(long lv, short sv, int iv) { 134 int* ip1 = multiparm(lv, iv, lv); 135 int* ip2 = multiparm(lv, sv, lv); 136 float* fp1 = multiparm(iv, iv, iv); 137 float* fp2 = multiparm(sv, iv, iv); 138 double* dp1 = multiparm(sv, sv, sv); 139 double* dp2 = multiparm(iv, sv, sv); 140 multiparm(sv, sv, lv); // expected-error {{ call to 'multiparm' is ambiguous; candidates are: }} 141} 142 143// Test overloading based on qualification vs. no qualification 144// conversion. 145int* quals1(int const * p); 146char* quals1(int * p); 147 148int* quals2(int const * const * pp); 149char* quals2(int * * pp); 150 151int* quals3(int const * * const * ppp); 152char* quals3(int *** ppp); 153 154void test_quals(int * p, int * * pp, int * * * ppp) { 155 char* q1 = quals1(p); 156 char* q2 = quals2(pp); 157 char* q3 = quals3(ppp); 158} 159 160// Test overloading based on qualification ranking (C++ 13.3.2)p3. 161int* quals_rank1(int const * p); 162float* quals_rank1(int const volatile *p); 163char* quals_rank1(char*); 164double* quals_rank1(const char*); 165 166int* quals_rank2(int const * const * pp); 167float* quals_rank2(int * const * pp); 168 169void quals_rank3(int const * const * const volatile * p); // expected-note{{candidate function}} 170void quals_rank3(int const * const volatile * const * p); // expected-note{{candidate function}} 171 172void quals_rank3(int const *); // expected-note{{candidate function}} 173void quals_rank3(int volatile *); // expected-note{{candidate function}} 174 175void test_quals_ranking(int * p, int volatile *pq, int * * pp, int * * * ppp) { 176 int* q1 = quals_rank1(p); 177 float* q2 = quals_rank1(pq); 178 double* q3 = quals_rank1("string literal"); 179 char a[17]; 180 const char* ap = a; 181 char* q4 = quals_rank1(a); 182 double* q5 = quals_rank1(ap); 183 184 float* q6 = quals_rank2(pp); 185 186 quals_rank3(ppp); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}} 187 188 quals_rank3(p); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}} 189 quals_rank3(pq); 190} 191 192// Test overloading based on derived-to-base conversions 193class A { }; 194class B : public A { }; 195class C : public B { }; 196class D : public C { }; 197 198int* derived1(A*); 199char* derived1(const A*); 200float* derived1(void*); 201 202int* derived2(A*); 203float* derived2(B*); 204 205int* derived3(A*); 206float* derived3(const B*); 207char* derived3(C*); 208 209void test_derived(B* b, B const* bc, C* c, const C* cc, void* v, D* d) { 210 int* d1 = derived1(b); 211 char* d2 = derived1(bc); 212 int* d3 = derived1(c); 213 char* d4 = derived1(cc); 214 float* d5 = derived1(v); 215 216 float* d6 = derived2(b); 217 float* d7 = derived2(c); 218 219 char* d8 = derived3(d); 220} 221 222// Test overloading of references. 223// (FIXME: tests binding to determine candidate sets, not overload 224// resolution per se). 225int* intref(int&); 226float* intref(const int&); 227 228void intref_test() { 229 float* ir1 = intref(5); 230 float* ir2 = intref(5.5); 231} 232 233// Test reference binding vs. standard conversions. 234int& bind_vs_conv(const double&); 235float& bind_vs_conv(int); 236 237void bind_vs_conv_test() 238{ 239 int& i1 = bind_vs_conv(1.0f); 240 float& f1 = bind_vs_conv((short)1); 241} 242 243// Test that cv-qualifiers get subsumed in the reference binding. 244struct X { }; 245struct Y { }; 246struct Z : X, Y { }; 247 248int& cvqual_subsume(X&); // expected-note{{candidate function}} 249float& cvqual_subsume(const Y&); // expected-note{{candidate function}} 250 251int& cvqual_subsume2(const X&); // expected-note{{candidate function}} 252float& cvqual_subsume2(const volatile Y&); // expected-note{{candidate function}} 253 254Z get_Z(); 255 256void cvqual_subsume_test(Z z) { 257 cvqual_subsume(z); // expected-error{{call to 'cvqual_subsume' is ambiguous; candidates are:}} 258 int& x = cvqual_subsume2(get_Z()); // expected-error{{call to 'cvqual_subsume2' is ambiguous; candidates are:}} 259} 260 261// Test overloading with cv-qualification differences in reference 262// binding. 263int& cvqual_diff(X&); 264float& cvqual_diff(const X&); 265 266void cvqual_diff_test(X x, Z z) { 267 int& i1 = cvqual_diff(x); 268 int& i2 = cvqual_diff(z); 269} 270 271// Test overloading with derived-to-base differences in reference 272// binding. 273struct Z2 : Z { }; 274 275int& db_rebind(X&); 276long& db_rebind(Y&); 277float& db_rebind(Z&); 278 279void db_rebind_test(Z2 z2) { 280 float& f1 = db_rebind(z2); 281} 282 283class string { }; 284class opt : public string { }; 285 286struct SR { 287 SR(const string&); 288}; 289 290void f(SR) { } 291 292void g(opt o) { 293 f(o); 294} 295 296 297namespace PR5756 { 298 int &a(void*, int); 299 float &a(void*, float); 300 void b() { 301 int &ir = a(0,0); 302 (void)ir; 303 } 304} 305 306// Tests the exact text used to note the candidates 307namespace test1 { 308 template <class T> void foo(T t, unsigned N); // expected-note {{candidate function [with T = int] not viable: no known conversion from 'char const [6]' to 'unsigned int' for 2nd argument}} 309 void foo(int n, char N); // expected-note {{candidate function not viable: no known conversion from 'char const [6]' to 'char' for 2nd argument}} 310 void foo(int n); // expected-note {{candidate function not viable: requires 1 argument, but 2 were provided}} 311 void foo(unsigned n = 10); // expected-note {{candidate function not viable: requires at most 1 argument, but 2 were provided}} 312 void foo(int n, const char *s, int t); // expected-note {{candidate function not viable: requires 3 arguments, but 2 were provided}} 313 void foo(int n, const char *s, int t, ...); // expected-note {{candidate function not viable: requires at least 3 arguments, but 2 were provided}} 314 void 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}} 315 316 void test() { 317 foo(4, "hello"); //expected-error {{no matching function for call to 'foo'}} 318 } 319} 320 321// PR 6014 322namespace test2 { 323 struct QFixed { 324 QFixed(int i); 325 QFixed(long i); 326 }; 327 328 bool operator==(const QFixed &f, int i); 329 330 class qrgb666 { 331 inline operator unsigned int () const; 332 333 inline bool operator==(const qrgb666 &v) const; 334 inline bool operator!=(const qrgb666 &v) const { return !(*this == v); } 335 }; 336} 337 338// PR 6117 339namespace test3 { 340 struct Base {}; 341 struct Incomplete; 342 343 void foo(Base *); // expected-note 2 {{cannot convert argument of incomplete type}} 344 void foo(Base &); // expected-note 2 {{cannot convert argument of incomplete type}} 345 346 void test(Incomplete *P) { 347 foo(P); // expected-error {{no matching function for call to 'foo'}} 348 foo(*P); // expected-error {{no matching function for call to 'foo'}} 349 } 350} 351 352namespace DerivedToBaseVsVoid { 353 struct A { }; 354 struct B : A { }; 355 356 float &f(void *); 357 int &f(const A*); 358 359 void g(B *b) { 360 int &ir = f(b); 361 } 362} 363 364// PR 6398 + PR 6421 365namespace test4 { 366 class A; 367 class B { 368 static void foo(); // expected-note {{not viable}} 369 static void foo(int*); // expected-note {{not viable}} 370 static void foo(long*); // expected-note {{not viable}} 371 372 void bar(A *a) { 373 foo(a); // expected-error {{no matching function for call}} 374 } 375 }; 376} 377 378namespace DerivedToBase { 379 struct A { }; 380 struct B : A { }; 381 struct C : B { }; 382 383 int &f0(const A&); 384 float &f0(B); 385 386 void g() { 387 float &fr = f0(C()); 388 } 389} 390 391namespace PR6483 { 392 struct X0 { 393 operator const unsigned int & () const; 394 }; 395 396 struct X1 { 397 operator unsigned int & () const; 398 }; 399 400 void f0(const bool &); 401 void f1(bool &); // expected-note 2{{not viable}} 402 403 void g(X0 x0, X1 x1) { 404 f0(x0); 405 f1(x0); // expected-error{{no matching function for call}} 406 f0(x1); 407 f1(x1); // expected-error{{no matching function for call}} 408 } 409} 410 411namespace PR6078 { 412 struct A { 413 A(short); // expected-note{{candidate constructor}} 414 A(long); // expected-note{{candidate constructor}} 415 }; 416 struct S { 417 typedef void ft(A); 418 operator ft*(); 419 }; 420 421 void f() { 422 S()(0); // expected-error{{conversion from 'int' to 'PR6078::A' is ambiguous}} 423 } 424} 425 426namespace PR6177 { 427 struct String { String(char const*); }; 428 429 void f(bool const volatile&); // expected-note{{passing argument to parameter here}} 430 void f(String); 431 432 void g() { f(""); } // expected-error{{volatile lvalue reference to type 'bool const volatile' cannot bind to a value of unrelated type 'char const [1]'}} 433} 434 435namespace PR7095 { 436 struct X { }; 437 438 struct Y { 439 operator const X*(); 440 441 private: 442 operator X*(); 443 }; 444 445 void f(const X *); 446 void g(Y y) { f(y); } 447} 448 449namespace PR7224 { 450 class A {}; 451 class B : public A {}; 452 453 int &foo(A *const d); 454 float &foo(const A *const d); 455 456 void bar() 457 { 458 B *const d = 0; 459 B const *const d2 = 0; 460 int &ir = foo(d); 461 float &fr = foo(d2); 462 } 463} 464 465namespace NontrivialSubsequence { 466 struct X0; 467 468 class A { 469 operator X0 *(); 470 public: 471 operator const X0 *(); 472 }; 473 474 A a; 475 void foo( void const * ); 476 477 void g() { 478 foo(a); 479 } 480} 481