addr-of-overloaded-function.cpp revision a6dc7eff27a8c59bee42809270971931b811dd44
1// RUN: %clang_cc1 -fsyntax-only -verify %s 2int f(double); // expected-note{{candidate function}} 3int f(int); // expected-note{{candidate function}} 4 5int (*pfd)(double) = f; // selects f(double) 6int (*pfd2)(double) = &f; // selects f(double) 7int (*pfd3)(double) = ((&((f)))); // selects f(double) 8int (*pfi)(int) = &f; // selects f(int) 9// FIXME: This error message is not very good. We need to keep better 10// track of what went wrong when the implicit conversion failed to 11// give a better error message here. 12int (*pfe)(...) = &f; // expected-error{{address of overloaded function 'f' does not match required type 'int (...)'}} 13int (&rfi)(int) = f; // selects f(int) 14int (&rfd)(double) = f; // selects f(double) 15 16void g(int (*fp)(int)); // expected-note{{note: candidate function}} 17void g(int (*fp)(float)); 18void g(int (*fp)(double)); // expected-note{{note: candidate function}} 19 20int g1(int); 21int g1(char); 22 23int g2(int); 24int g2(double); 25 26template<typename T> T g3(T); 27int g3(int); 28int g3(char); 29 30void g_test() { 31 g(g1); 32 g(g2); // expected-error{{call to 'g' is ambiguous; candidates are:}} 33 g(g3); 34} 35 36template<typename T> T h1(T); 37template<typename R, typename A1> R h1(A1); 38int h1(char); 39 40void ha(int (*fp)(int)); 41void hb(int (*fp)(double)); 42 43void h_test() { 44 ha(h1); 45 hb(h1); 46} 47 48struct A { }; 49void f(void (*)(A *)); 50 51struct B 52{ 53 void g() { f(d); } 54 void d(void *); 55 static void d(A *); 56}; 57 58struct C { 59 C &getC() { 60 return makeAC; // expected-error{{reference to non-static member function must be called}} 61 } 62 63 C &makeAC(); 64 const C &makeAC() const; 65 66 static void f(); // expected-note{{candidate function}} 67 static void f(int); // expected-note{{candidate function}} 68 69 void g() { 70 int (&fp)() = f; // expected-error{{address of overloaded function 'f' does not match required type 'int ()'}} 71 } 72}; 73 74// PR6886 75namespace test0 { 76 void myFunction(void (*)(void *)); 77 78 class Foo { 79 void foo(); 80 81 static void bar(void*); 82 static void bar(); 83 }; 84 85 void Foo::foo() { 86 myFunction(bar); 87 } 88} 89 90namespace PR7971 { 91 struct S { 92 void g() { 93 f(&g); 94 } 95 void f(bool (*)(int, char)); 96 static bool g(int, char); 97 }; 98} 99 100namespace PR8033 { 101 template <typename T1, typename T2> int f(T1 *, const T2 *); // expected-note 2{{candidate function [with T1 = const int, T2 = int]}} 102 template <typename T1, typename T2> int f(const T1 *, T2 *); // expected-note 2{{candidate function [with T1 = int, T2 = const int]}} 103 int (*p)(const int *, const int *) = f; // expected-error{{address of overloaded function 'f' is ambiguous}} \ 104 // expected-error{{address of overloaded function 'f' is ambiguous}} 105 106} 107 108namespace PR8196 { 109 template <typename T> struct mcdata { 110 typedef int result_type; 111 }; 112 template <class T> 113 typename mcdata<T>::result_type wrap_mean(mcdata<T> const&); 114 void add_property(double(*)(mcdata<double> const &)); // expected-note{{candidate function not viable: no overload of 'wrap_mean' matching}} 115 void f() { 116 add_property(&wrap_mean); // expected-error{{no matching function for call to 'add_property'}} 117 } 118} 119 120namespace PR7425 { 121 template<typename T> 122 void foo() 123 { 124 } 125 126 struct B 127 { 128 template<typename T> 129 B(const T&) 130 { 131 } 132 }; 133 134 void bar(const B& b) 135 { 136 } 137 138 void bar2(const B& b = foo<int>) 139 { 140 } 141 142 void test(int argc, char** argv) 143 { 144 bar(foo<int>); 145 bar2(); 146 } 147} 148 149namespace test1 { 150 void fun(int x) {} 151 152 void parameter_number() { 153 void (*ptr1)(int, int) = &fun; // expected-error {{cannot initialize a variable of type 'void (*)(int, int)' with an rvalue of type 'void (*)(int)': different number of parameters (2 vs 1)}} 154 void (*ptr2)(int, int); 155 ptr2 = &fun; // expected-error {{assigning to 'void (*)(int, int)' from incompatible type 'void (*)(int)': different number of parameters (2 vs 1)}} 156 } 157 158 void parameter_mismatch() { 159 void (*ptr1)(double) = &fun; // expected-error {{cannot initialize a variable of type 'void (*)(double)' with an rvalue of type 'void (*)(int)': type mismatch at 1st parameter ('double' vs 'int')}} 160 void (*ptr2)(double); 161 ptr2 = &fun; // expected-error {{assigning to 'void (*)(double)' from incompatible type 'void (*)(int)': type mismatch at 1st parameter ('double' vs 'int')}} 162 } 163 164 void return_type_test() { 165 int (*ptr1)(int) = &fun; // expected-error {{cannot initialize a variable of type 'int (*)(int)' with an rvalue of type 'void (*)(int)': different return type ('int' vs 'void')}} 166 int (*ptr2)(int); 167 ptr2 = &fun; // expected-error {{assigning to 'int (*)(int)' from incompatible type 'void (*)(int)': different return type ('int' vs 'void')}} 168 } 169 170 int foo(double x, double y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} 171 int foo(int x, int y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} 172 int foo(double x) {return 0;} // expected-note {{candidate function has type mismatch at 1st parameter (expected 'int' but has 'double')}} 173 double foo(float x, float y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} 174 double foo(int x, float y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} 175 double foo(float x) {return 0;} // expected-note {{candidate function has type mismatch at 1st parameter (expected 'int' but has 'float')}} 176 double foo(int x) {return 0;} // expected-note {{candidate function has different return type ('int' expected but has 'double')}} 177 178 int (*ptr)(int) = &foo; // expected-error {{address of overloaded function 'foo' does not match required type 'int (int)'}} 179 180 struct Qualifiers { 181 void N() {}; 182 void C() const {}; 183 void V() volatile {}; 184 void R() __restrict {}; 185 void CV() const volatile {}; 186 void CR() const __restrict {}; 187 void VR() volatile __restrict {}; 188 void CVR() const volatile __restrict {}; 189 }; 190 191 192 void QualifierTest() { 193 void (Qualifiers::*X)(); 194 X = &Qualifiers::C; // expected-error {{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() const': different qualifiers (none vs const)}} 195 X = &Qualifiers::V; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() volatile': different qualifiers (none vs volatile)}} 196 X = &Qualifiers::R; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() restrict': different qualifiers (none vs restrict)}} 197 X = &Qualifiers::CV; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() const volatile': different qualifiers (none vs const and volatile)}} 198 X = &Qualifiers::CR; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() const restrict': different qualifiers (none vs const and restrict)}} 199 X = &Qualifiers::VR; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() volatile restrict': different qualifiers (none vs volatile and restrict)}} 200 X = &Qualifiers::CVR; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() const volatile restrict': different qualifiers (none vs const, volatile, and restrict)}} 201 } 202 203 struct Dummy { 204 void N() {}; 205 }; 206 207 void (Qualifiers::*X)() = &Dummy::N; // expected-error{{cannot initialize a variable of type 'void (test1::Qualifiers::*)()' with an rvalue of type 'void (test1::Dummy::*)()': different classes ('test1::Qualifiers' vs 'test1::Dummy')}} 208} 209