xref: /llvm-project/clang/test/CXX/over/over.over/p2-resolve-single-template-id.cpp (revision 32cbff7809fa6ddf713937a012fee9a1eacb95dc)
1aeef2ae8SDavid Blaikie // RUN: %clang_cc1 -fsyntax-only -verify -Wno-bool-conversion %s
2b491ed36SDouglas Gregor 
3b491ed36SDouglas Gregor typedef __typeof__(((int*)0)-((int*)0)) ptrdiff_t;
4b491ed36SDouglas Gregor 
5b491ed36SDouglas Gregor namespace DontResolveTooEarly_WaitForOverloadResolution
6b491ed36SDouglas Gregor {
7b491ed36SDouglas Gregor   template <class T> T* f(int);	// #1
8b491ed36SDouglas Gregor   template <class T, class U> T& f(U); // #2
9b491ed36SDouglas Gregor 
g()10b491ed36SDouglas Gregor   void g() {
11b491ed36SDouglas Gregor     int *ip = f<int>(1);	// calls #1
12b491ed36SDouglas Gregor   }
13b491ed36SDouglas Gregor 
14b491ed36SDouglas Gregor   template <class T>
15b491ed36SDouglas Gregor     T* f2(int);
16b491ed36SDouglas Gregor   template <class T, class U>
17b491ed36SDouglas Gregor     T& f2(U);
18b491ed36SDouglas Gregor 
g2()19b491ed36SDouglas Gregor   void g2() {
20b491ed36SDouglas Gregor     int*ip = (f2<int>)(1); // ok
21b491ed36SDouglas Gregor   }
22b491ed36SDouglas Gregor 
23b491ed36SDouglas Gregor } // End namespace
24b491ed36SDouglas Gregor 
250ec210bcSDouglas Gregor namespace DontAllowUnresolvedOverloadedExpressionInAnUnusedExpression
260ec210bcSDouglas Gregor {
one()270ec210bcSDouglas Gregor   void one() { }
oneT()280ec210bcSDouglas Gregor   template<class T> void oneT() { }
290ec210bcSDouglas Gregor 
two()3050a2c2c1SJohn McCall   void two() { } // expected-note 2 {{possible target for call}}
two(int)3150a2c2c1SJohn McCall   void two(int) { } // expected-note 2 {{possible target for call}}
twoT()3250a2c2c1SJohn McCall   template<class T> void twoT() { }  // expected-note 2 {{possible target for call}}
twoT(T)3350a2c2c1SJohn McCall   template<class T> void twoT(T) { }  // expected-note 2 {{possible target for call}}
340ec210bcSDouglas Gregor 
check()350ec210bcSDouglas Gregor   void check()
360ec210bcSDouglas Gregor   {
370ec210bcSDouglas Gregor     one; // expected-warning {{expression result unused}}
3850a2c2c1SJohn McCall     two; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}}
390ec210bcSDouglas Gregor     oneT<int>; // expected-warning {{expression result unused}}
4050a2c2c1SJohn McCall     twoT<int>; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
410ec210bcSDouglas Gregor   }
420ec210bcSDouglas Gregor 
430ec210bcSDouglas Gregor   // check the template function case
check()440ec210bcSDouglas Gregor   template<class T> void check()
450ec210bcSDouglas Gregor   {
460ec210bcSDouglas Gregor     one; // expected-warning {{expression result unused}}
4750a2c2c1SJohn McCall     two; // expected-error{{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}}
480ec210bcSDouglas Gregor     oneT<int>; // expected-warning {{expression result unused}}
4950a2c2c1SJohn McCall     twoT<int>; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
500ec210bcSDouglas Gregor 
510ec210bcSDouglas Gregor   }
520ec210bcSDouglas Gregor 
530ec210bcSDouglas Gregor }
540ec210bcSDouglas Gregor 
55b491ed36SDouglas Gregor   template<typename T>
twoT()56b491ed36SDouglas Gregor     void twoT() { }
57b491ed36SDouglas Gregor   template<typename T, typename U>
twoT(T)58b491ed36SDouglas Gregor     void twoT(T) { }
59b491ed36SDouglas Gregor 
60b491ed36SDouglas Gregor 
two()61b491ed36SDouglas Gregor   void two() { }; //expected-note 5{{candidate}}
two(int)62b491ed36SDouglas Gregor   void two(int) { }; //expected-note 5{{candidate}}
63b491ed36SDouglas Gregor 
64b491ed36SDouglas Gregor 
65b491ed36SDouglas Gregor 
one()66b491ed36SDouglas Gregor   void one() { }
67b491ed36SDouglas Gregor   template<class T>
oneT()68b491ed36SDouglas Gregor     void oneT() { }
69b491ed36SDouglas Gregor 
70b491ed36SDouglas Gregor   template<class T>
cant_resolve()71b491ed36SDouglas Gregor   void cant_resolve() { } //expected-note 3{{candidate}}
72b491ed36SDouglas Gregor 
cant_resolve(T)73b491ed36SDouglas Gregor   template<class T> void cant_resolve(T) { }//expected-note 3{{candidate}}
74b491ed36SDouglas Gregor 
75b491ed36SDouglas Gregor 
main()76b491ed36SDouglas Gregor int main()
77b491ed36SDouglas Gregor {
780ec210bcSDouglas Gregor 
79b491ed36SDouglas Gregor   { static_cast<void>(one); }
80b491ed36SDouglas Gregor   { (void)(one); }
81b491ed36SDouglas Gregor   { static_cast<void>(oneT<int>); }
82b491ed36SDouglas Gregor   { (void)(oneT<int>); }
83b491ed36SDouglas Gregor 
843aef3d87SJohn McCall   { static_cast<void>(two); } // expected-error {{address of overloaded function 'two' cannot be static_cast to type 'void'}}
853aef3d87SJohn McCall   { (void)(two); } // expected-error {{address of overloaded function 'two' cannot be cast to type 'void'}}
86b491ed36SDouglas Gregor   { static_cast<void>(twoT<int>); }
87b491ed36SDouglas Gregor   { (void)(twoT<int>); }
88b491ed36SDouglas Gregor 
89b491ed36SDouglas Gregor 
90b491ed36SDouglas Gregor   { ptrdiff_t x = reinterpret_cast<ptrdiff_t>(oneT<int>); }
91b491ed36SDouglas Gregor   { (void) reinterpret_cast<int (*)(char, double)>(oneT<int>); }
92b491ed36SDouglas Gregor   { (void) reinterpret_cast<ptrdiff_t>(one); }
93b491ed36SDouglas Gregor   { (void) reinterpret_cast<int (*)(char, double)>(one); }
94b491ed36SDouglas Gregor 
95b491ed36SDouglas Gregor   { ptrdiff_t x = reinterpret_cast<ptrdiff_t>(twoT<int>); }
96b491ed36SDouglas Gregor   { (void) reinterpret_cast<int (*)(char, double)>(twoT<int>); }
97b491ed36SDouglas Gregor   { (void) reinterpret_cast<void (*)(int)>(two); } //expected-error {{reinterpret_cast}}
98b491ed36SDouglas Gregor   { (void) static_cast<void (*)(int)>(two); } //ok
99b491ed36SDouglas Gregor 
100b491ed36SDouglas Gregor   { (void) reinterpret_cast<int>(two); } //expected-error {{reinterpret_cast}}
101b491ed36SDouglas Gregor   { (void) reinterpret_cast<int (*)(char, double)>(two); } //expected-error {{reinterpret_cast}}
102b491ed36SDouglas Gregor 
10310eb4b67SDavid Blaikie   { bool b = (twoT<int>); }
10410eb4b67SDavid Blaikie   { bool b = (twoT<int, int>); }
105b491ed36SDouglas Gregor 
106b491ed36SDouglas Gregor   { bool b = &twoT<int>; //&foo<int>; }
107b491ed36SDouglas Gregor     b = &(twoT<int>); }
108b491ed36SDouglas Gregor 
109b491ed36SDouglas Gregor   { ptrdiff_t x = (ptrdiff_t) &twoT<int>;
110b491ed36SDouglas Gregor       x = (ptrdiff_t) &twoT<int>; }
111b491ed36SDouglas Gregor 
112b491ed36SDouglas Gregor   { ptrdiff_t x = (ptrdiff_t) twoT<int>;
113b491ed36SDouglas Gregor       x = (ptrdiff_t) twoT<int>; }
114b491ed36SDouglas Gregor 
115b491ed36SDouglas Gregor 
116b491ed36SDouglas Gregor   { ptrdiff_t x = (ptrdiff_t) &twoT<int,int>;
117b491ed36SDouglas Gregor   x = (ptrdiff_t) &twoT<int>; }
118b491ed36SDouglas Gregor 
119b491ed36SDouglas Gregor   { oneT<int>;   &oneT<int>; } //expected-warning 2{{expression result unused}}
120b491ed36SDouglas Gregor   { static_cast<void>(cant_resolve<int>); } // expected-error {{address of overload}}
121b491ed36SDouglas Gregor   { bool b = cant_resolve<int>; } // expected-error {{address of overload}}
122b491ed36SDouglas Gregor   { (void) cant_resolve<int>; } // expected-error {{address of overload}}
123b491ed36SDouglas Gregor 
124b491ed36SDouglas Gregor }
125b491ed36SDouglas Gregor 
126ffce2457SChandler Carruth namespace member_pointers {
127ffce2457SChandler Carruth   struct S {
fmember_pointers::S1286df859d8SDavid Blaikie     template <typename T> bool f(T) { return false; } // expected-note 4 {{possible target for call}}
gmember_pointers::S129ffce2457SChandler Carruth     template <typename T> static bool g(T) { return false; }
130b491ed36SDouglas Gregor 
hmember_pointers::S13150a2c2c1SJohn McCall     template <typename T> bool h(T) { return false; }  // expected-note 3 {{possible target for call}}
hmember_pointers::S13250a2c2c1SJohn McCall     template <int N> static bool h(int) { return false; } // expected-note 3 {{possible target for call}}
133ffce2457SChandler Carruth   };
134ffce2457SChandler Carruth 
test(S s)135ffce2457SChandler Carruth   void test(S s) {
136ffce2457SChandler Carruth     if (S::f<char>) return; // expected-error {{call to non-static member function without an object argument}}
137ffce2457SChandler Carruth     if (S::f<int>) return; // expected-error {{call to non-static member function without an object argument}}
138ffce2457SChandler Carruth     if (&S::f<char>) return;
139ffce2457SChandler Carruth     if (&S::f<int>) return;
14050a2c2c1SJohn McCall     if (s.f<char>) return; // expected-error {{reference to non-static member function must be called}}
14150a2c2c1SJohn McCall     if (s.f<int>) return; // expected-error {{reference to non-static member function must be called}}
1420009fcc3SJohn McCall     if (&s.f<char>) return; // expected-error {{cannot create a non-constant pointer to member function}}
1430009fcc3SJohn McCall     if (&s.f<int>) return; // expected-error {{cannot create a non-constant pointer to member function}}
144ffce2457SChandler Carruth 
14510eb4b67SDavid Blaikie     if (S::g<char>) return;
14610eb4b67SDavid Blaikie     if (S::g<int>) return;
147ffce2457SChandler Carruth     if (&S::g<char>) return;
148ffce2457SChandler Carruth     if (&S::g<int>) return;
14910eb4b67SDavid Blaikie     if (s.g<char>) return;
15010eb4b67SDavid Blaikie     if (s.g<int>) return;
151ffce2457SChandler Carruth     if (&s.g<char>) return;
152ffce2457SChandler Carruth     if (&s.g<int>) return;
153ffce2457SChandler Carruth 
15410eb4b67SDavid Blaikie     if (S::h<42>) return;
15550a2c2c1SJohn McCall     if (S::h<int>) return; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
156ffce2457SChandler Carruth     if (&S::h<42>) return;
157ffce2457SChandler Carruth     if (&S::h<int>) return;
15810eb4b67SDavid Blaikie     if (s.h<42>) return;
15950a2c2c1SJohn McCall     if (s.h<int>) return; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
160ffce2457SChandler Carruth     if (&s.h<42>) return;
16150a2c2c1SJohn McCall     if (&s.h<int>) return; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
162ffce2457SChandler Carruth 
163ffce2457SChandler Carruth     { bool b = S::f<char>; } // expected-error {{call to non-static member function without an object argument}}
164ffce2457SChandler Carruth     { bool b = S::f<int>; } // expected-error {{call to non-static member function without an object argument}}
165ffce2457SChandler Carruth     { bool b = &S::f<char>; }
166ffce2457SChandler Carruth     { bool b = &S::f<int>; }
1670009fcc3SJohn McCall     // These next two errors are terrible.
1684124c492SJohn McCall     { bool b = s.f<char>; } // expected-error {{reference to non-static member function must be called}}
1694124c492SJohn McCall     { bool b = s.f<int>; } // expected-error {{reference to non-static member function must be called}}
1700009fcc3SJohn McCall     { bool b = &s.f<char>; } // expected-error {{cannot create a non-constant pointer to member function}}
1710009fcc3SJohn McCall     { bool b = &s.f<int>; } // expected-error {{cannot create a non-constant pointer to member function}}
172ffce2457SChandler Carruth 
17310eb4b67SDavid Blaikie     { bool b = S::g<char>; }
17410eb4b67SDavid Blaikie     { bool b = S::g<int>; }
175ffce2457SChandler Carruth     { bool b = &S::g<char>; }
176ffce2457SChandler Carruth     { bool b = &S::g<int>; }
17710eb4b67SDavid Blaikie     { bool b = s.g<char>; }
17810eb4b67SDavid Blaikie     { bool b = s.g<int>; }
179ffce2457SChandler Carruth     { bool b = &s.g<char>; }
180ffce2457SChandler Carruth     { bool b = &s.g<int>; }
181ffce2457SChandler Carruth 
18210eb4b67SDavid Blaikie     { bool b = S::h<42>; }
183*32cbff78SDavide Italiano     { bool b = S::h<int>; } // expected-error {{cannot form member pointer of type 'bool' without '&' and class name}}
184ffce2457SChandler Carruth     { bool b = &S::h<42>; }
185ffce2457SChandler Carruth     { bool b = &S::h<int>; }
18610eb4b67SDavid Blaikie     { bool b = s.h<42>; }
187*32cbff78SDavide Italiano     { bool b = s.h<int>; } // expected-error {{cannot form member pointer of type 'bool' without '&' and class name}}
188ffce2457SChandler Carruth     { bool b = &s.h<42>; }
189*32cbff78SDavide Italiano     { bool b = &s.h<int>; } // expected-error {{cannot form member pointer of type 'bool' without '&' and class name}}
190ffce2457SChandler Carruth   }
191ffce2457SChandler Carruth }
192