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