xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/addr-of-overloaded-function.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc int f(double); // expected-note{{candidate function}}
3*f4a2713aSLionel Sambuc int f(int); // expected-note{{candidate function}}
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc int (*pfd)(double) = f; // selects f(double)
6*f4a2713aSLionel Sambuc int (*pfd2)(double) = &f; // selects f(double)
7*f4a2713aSLionel Sambuc int (*pfd3)(double) = ((&((f)))); // selects f(double)
8*f4a2713aSLionel Sambuc int (*pfi)(int) = &f;    // selects f(int)
9*f4a2713aSLionel Sambuc // FIXME: This error message is not very good. We need to keep better
10*f4a2713aSLionel Sambuc // track of what went wrong when the implicit conversion failed to
11*f4a2713aSLionel Sambuc // give a better error message here.
12*f4a2713aSLionel Sambuc int (*pfe)(...) = &f;    // expected-error{{address of overloaded function 'f' does not match required type 'int (...)'}}
13*f4a2713aSLionel Sambuc int (&rfi)(int) = f;     // selects f(int)
14*f4a2713aSLionel Sambuc int (&rfd)(double) = f;  // selects f(double)
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc void g(int (*fp)(int));   // expected-note{{candidate function}}
17*f4a2713aSLionel Sambuc void g(int (*fp)(float));
18*f4a2713aSLionel Sambuc void g(int (*fp)(double)); // expected-note{{candidate function}}
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc int g1(int);
21*f4a2713aSLionel Sambuc int g1(char);
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc int g2(int);
24*f4a2713aSLionel Sambuc int g2(double);
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc template<typename T> T g3(T);
27*f4a2713aSLionel Sambuc int g3(int);
28*f4a2713aSLionel Sambuc int g3(char);
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc void g_test() {
31*f4a2713aSLionel Sambuc   g(g1);
32*f4a2713aSLionel Sambuc   g(g2); // expected-error{{call to 'g' is ambiguous}}
33*f4a2713aSLionel Sambuc   g(g3);
34*f4a2713aSLionel Sambuc }
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc template<typename T> T h1(T);
37*f4a2713aSLionel Sambuc template<typename R, typename A1> R h1(A1);
38*f4a2713aSLionel Sambuc int h1(char);
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc void ha(int (*fp)(int));
41*f4a2713aSLionel Sambuc void hb(int (*fp)(double));
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc void h_test() {
44*f4a2713aSLionel Sambuc   ha(h1);
45*f4a2713aSLionel Sambuc   hb(h1);
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc struct A { };
49*f4a2713aSLionel Sambuc void f(void (*)(A *));
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc struct B
52*f4a2713aSLionel Sambuc {
53*f4a2713aSLionel Sambuc   void g() { f(d); }
54*f4a2713aSLionel Sambuc   void d(void *);
55*f4a2713aSLionel Sambuc   static void d(A *);
56*f4a2713aSLionel Sambuc };
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc struct C {
59*f4a2713aSLionel Sambuc   C &getC() {
60*f4a2713aSLionel Sambuc     return makeAC; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}}
61*f4a2713aSLionel Sambuc   }
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   // FIXME: filter by const so we can unambiguously suggest '()' & point to just the one candidate, probably
64*f4a2713aSLionel Sambuc   C &makeAC(); // expected-note{{possible target for call}}
65*f4a2713aSLionel Sambuc   const C &makeAC() const; // expected-note{{possible target for call}}
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc   static void f(); // expected-note{{candidate function}}
68*f4a2713aSLionel Sambuc   static void f(int); // expected-note{{candidate function}}
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc   void g() {
71*f4a2713aSLionel Sambuc     int (&fp)() = f; // expected-error{{address of overloaded function 'f' does not match required type 'int ()'}}
72*f4a2713aSLionel Sambuc   }
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc   template<typename T>
75*f4a2713aSLionel Sambuc   void q1(int); // expected-note{{possible target for call}}
76*f4a2713aSLionel Sambuc   template<typename T>
77*f4a2713aSLionel Sambuc   void q2(T t = T()); // expected-note{{possible target for call}}
78*f4a2713aSLionel Sambuc   template<typename T>
79*f4a2713aSLionel Sambuc   void q3(); // expected-note{{possible target for call}}
80*f4a2713aSLionel Sambuc   template<typename T1, typename T2>
81*f4a2713aSLionel Sambuc   void q4(); // expected-note{{possible target for call}}
82*f4a2713aSLionel Sambuc   template<typename T1 = int> // expected-warning{{default template arguments for a function template are a C++11 extension}}
83*f4a2713aSLionel Sambuc   void q5(); // expected-note{{possible target for call}}
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc   void h() {
86*f4a2713aSLionel Sambuc     // Do not suggest '()' since an int argument is required
87*f4a2713aSLionel Sambuc     q1<int>; // expected-error-re{{reference to non-static member function must be called$}}
88*f4a2713aSLionel Sambuc     // Suggest '()' since there's a default value for the only argument & the
89*f4a2713aSLionel Sambuc     // type argument is already provided
90*f4a2713aSLionel Sambuc     q2<int>; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}}
91*f4a2713aSLionel Sambuc     // Suggest '()' since no arguments are required & the type argument is
92*f4a2713aSLionel Sambuc     // already provided
93*f4a2713aSLionel Sambuc     q3<int>; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}}
94*f4a2713aSLionel Sambuc     // Do not suggest '()' since another type argument is required
95*f4a2713aSLionel Sambuc     q4<int>; // expected-error-re{{reference to non-static member function must be called$}}
96*f4a2713aSLionel Sambuc     // Suggest '()' since the type parameter has a default value
97*f4a2713aSLionel Sambuc     q5; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}}
98*f4a2713aSLionel Sambuc   }
99*f4a2713aSLionel Sambuc };
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc // PR6886
102*f4a2713aSLionel Sambuc namespace test0 {
103*f4a2713aSLionel Sambuc   void myFunction(void (*)(void *));
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc   class Foo {
106*f4a2713aSLionel Sambuc     void foo();
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc     static void bar(void*);
109*f4a2713aSLionel Sambuc     static void bar();
110*f4a2713aSLionel Sambuc   };
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc   void Foo::foo() {
113*f4a2713aSLionel Sambuc     myFunction(bar);
114*f4a2713aSLionel Sambuc   }
115*f4a2713aSLionel Sambuc }
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc namespace PR7971 {
118*f4a2713aSLionel Sambuc   struct S {
119*f4a2713aSLionel Sambuc     void g() {
120*f4a2713aSLionel Sambuc       f(&g);
121*f4a2713aSLionel Sambuc     }
122*f4a2713aSLionel Sambuc     void f(bool (*)(int, char));
123*f4a2713aSLionel Sambuc     static bool g(int, char);
124*f4a2713aSLionel Sambuc   };
125*f4a2713aSLionel Sambuc }
126*f4a2713aSLionel Sambuc 
127*f4a2713aSLionel Sambuc namespace PR8033 {
128*f4a2713aSLionel Sambuc   template <typename T1, typename T2> int f(T1 *, const T2 *); // expected-note {{candidate function [with T1 = const int, T2 = int]}} \
129*f4a2713aSLionel Sambuc   // expected-note{{candidate function}}
130*f4a2713aSLionel Sambuc   template <typename T1, typename T2> int f(const T1 *, T2 *); // expected-note {{candidate function [with T1 = int, T2 = const int]}} \
131*f4a2713aSLionel Sambuc   // expected-note{{candidate function}}
132*f4a2713aSLionel Sambuc   int (*p)(const int *, const int *) = f; // expected-error{{address of overloaded function 'f' is ambiguous}} \
133*f4a2713aSLionel Sambuc   // expected-error{{address of overloaded function 'f' is ambiguous}}
134*f4a2713aSLionel Sambuc 
135*f4a2713aSLionel Sambuc }
136*f4a2713aSLionel Sambuc 
137*f4a2713aSLionel Sambuc namespace PR8196 {
138*f4a2713aSLionel Sambuc   template <typename T> struct mcdata {
139*f4a2713aSLionel Sambuc     typedef int result_type;
140*f4a2713aSLionel Sambuc   };
141*f4a2713aSLionel Sambuc   template <class T>
142*f4a2713aSLionel Sambuc     typename mcdata<T>::result_type wrap_mean(mcdata<T> const&);
143*f4a2713aSLionel Sambuc   void add_property(double(*)(mcdata<double> const &)); // expected-note{{candidate function not viable: no overload of 'wrap_mean' matching}}
144*f4a2713aSLionel Sambuc   void f() {
145*f4a2713aSLionel Sambuc     add_property(&wrap_mean); // expected-error{{no matching function for call to 'add_property'}}
146*f4a2713aSLionel Sambuc   }
147*f4a2713aSLionel Sambuc }
148*f4a2713aSLionel Sambuc 
149*f4a2713aSLionel Sambuc namespace PR7425 {
150*f4a2713aSLionel Sambuc   template<typename T>
151*f4a2713aSLionel Sambuc   void foo()
152*f4a2713aSLionel Sambuc   {
153*f4a2713aSLionel Sambuc   }
154*f4a2713aSLionel Sambuc 
155*f4a2713aSLionel Sambuc   struct B
156*f4a2713aSLionel Sambuc   {
157*f4a2713aSLionel Sambuc     template<typename T>
158*f4a2713aSLionel Sambuc     B(const T&)
159*f4a2713aSLionel Sambuc     {
160*f4a2713aSLionel Sambuc     }
161*f4a2713aSLionel Sambuc   };
162*f4a2713aSLionel Sambuc 
163*f4a2713aSLionel Sambuc   void bar(const B& b)
164*f4a2713aSLionel Sambuc   {
165*f4a2713aSLionel Sambuc   }
166*f4a2713aSLionel Sambuc 
167*f4a2713aSLionel Sambuc   void bar2(const B& b = foo<int>)
168*f4a2713aSLionel Sambuc   {
169*f4a2713aSLionel Sambuc   }
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc   void test(int argc, char** argv)
172*f4a2713aSLionel Sambuc   {
173*f4a2713aSLionel Sambuc     bar(foo<int>);
174*f4a2713aSLionel Sambuc     bar2();
175*f4a2713aSLionel Sambuc   }
176*f4a2713aSLionel Sambuc }
177*f4a2713aSLionel Sambuc 
178*f4a2713aSLionel Sambuc namespace test1 {
179*f4a2713aSLionel Sambuc   void fun(int x) {}
180*f4a2713aSLionel Sambuc 
181*f4a2713aSLionel Sambuc   void parameter_number() {
182*f4a2713aSLionel Sambuc     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)}}
183*f4a2713aSLionel Sambuc     void (*ptr2)(int, int);
184*f4a2713aSLionel Sambuc     ptr2 = &fun;  // expected-error {{assigning to 'void (*)(int, int)' from incompatible type 'void (*)(int)': different number of parameters (2 vs 1)}}
185*f4a2713aSLionel Sambuc   }
186*f4a2713aSLionel Sambuc 
187*f4a2713aSLionel Sambuc   void parameter_mismatch() {
188*f4a2713aSLionel Sambuc     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')}}
189*f4a2713aSLionel Sambuc     void (*ptr2)(double);
190*f4a2713aSLionel Sambuc     ptr2 = &fun; // expected-error {{assigning to 'void (*)(double)' from incompatible type 'void (*)(int)': type mismatch at 1st parameter ('double' vs 'int')}}
191*f4a2713aSLionel Sambuc   }
192*f4a2713aSLionel Sambuc 
193*f4a2713aSLionel Sambuc   void return_type_test() {
194*f4a2713aSLionel Sambuc     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')}}
195*f4a2713aSLionel Sambuc     int (*ptr2)(int);
196*f4a2713aSLionel Sambuc     ptr2 = &fun;  // expected-error {{assigning to 'int (*)(int)' from incompatible type 'void (*)(int)': different return type ('int' vs 'void')}}
197*f4a2713aSLionel Sambuc   }
198*f4a2713aSLionel Sambuc 
199*f4a2713aSLionel Sambuc   int foo(double x, double y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}}
200*f4a2713aSLionel Sambuc   int foo(int x, int y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}}
201*f4a2713aSLionel Sambuc   int foo(double x) {return 0;} // expected-note {{candidate function has type mismatch at 1st parameter (expected 'int' but has 'double')}}
202*f4a2713aSLionel Sambuc   double foo(float x, float y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}}
203*f4a2713aSLionel Sambuc   double foo(int x, float y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}}
204*f4a2713aSLionel Sambuc   double foo(float x) {return 0;} // expected-note {{candidate function has type mismatch at 1st parameter (expected 'int' but has 'float')}}
205*f4a2713aSLionel Sambuc   double foo(int x) {return 0;} // expected-note {{candidate function has different return type ('int' expected but has 'double')}}
206*f4a2713aSLionel Sambuc 
207*f4a2713aSLionel Sambuc   int (*ptr)(int) = &foo; // expected-error {{address of overloaded function 'foo' does not match required type 'int (int)'}}
208*f4a2713aSLionel Sambuc 
209*f4a2713aSLionel Sambuc   struct Qualifiers {
210*f4a2713aSLionel Sambuc     void N() {};
211*f4a2713aSLionel Sambuc     void C() const {};
212*f4a2713aSLionel Sambuc     void V() volatile {};
213*f4a2713aSLionel Sambuc     void R() __restrict {};
214*f4a2713aSLionel Sambuc     void CV() const volatile {};
215*f4a2713aSLionel Sambuc     void CR() const __restrict {};
216*f4a2713aSLionel Sambuc     void VR() volatile __restrict {};
217*f4a2713aSLionel Sambuc     void CVR() const volatile __restrict {};
218*f4a2713aSLionel Sambuc   };
219*f4a2713aSLionel Sambuc 
220*f4a2713aSLionel Sambuc 
221*f4a2713aSLionel Sambuc   void QualifierTest() {
222*f4a2713aSLionel Sambuc     void (Qualifiers::*X)();
223*f4a2713aSLionel Sambuc     X = &Qualifiers::C; // expected-error {{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() const': different qualifiers (none vs const)}}
224*f4a2713aSLionel Sambuc     X = &Qualifiers::V; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() volatile': different qualifiers (none vs volatile)}}
225*f4a2713aSLionel Sambuc     X = &Qualifiers::R; // expected-error{{assigning to 'void (test1::Qualifiers::*)()' from incompatible type 'void (test1::Qualifiers::*)() restrict': different qualifiers (none vs restrict)}}
226*f4a2713aSLionel Sambuc     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)}}
227*f4a2713aSLionel Sambuc     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)}}
228*f4a2713aSLionel Sambuc     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)}}
229*f4a2713aSLionel Sambuc     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)}}
230*f4a2713aSLionel Sambuc   }
231*f4a2713aSLionel Sambuc 
232*f4a2713aSLionel Sambuc   struct Dummy {
233*f4a2713aSLionel Sambuc     void N() {};
234*f4a2713aSLionel Sambuc   };
235*f4a2713aSLionel Sambuc 
236*f4a2713aSLionel Sambuc   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')}}
237*f4a2713aSLionel Sambuc }
238*f4a2713aSLionel Sambuc 
239*f4a2713aSLionel Sambuc template <typename T> class PR16561 {
240*f4a2713aSLionel Sambuc   virtual bool f() { if (f) {} return false; } // expected-error {{reference to non-static member function must be called}}
241*f4a2713aSLionel Sambuc };
242