xref: /llvm-project/clang/test/SemaTemplate/dependent-names.cpp (revision f46d1463b835560d90ad3ac02b63c771e4ebe566)
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2 
3 typedef double A;
4 template<typename T> class B {
5   typedef int A;
6 };
7 
8 template<typename T> struct X : B<T> {
9   static A a;
10 };
11 
12 int a0[sizeof(X<int>::a) == sizeof(double) ? 1 : -1];
13 
14 // PR4365.
15 template<class T> class Q;
16 template<class T> class R : Q<T> {T current;};
17 
18 
19 namespace test0 {
20   template <class T> class Base {
21   public:
22     void instance_foo();
23     static void static_foo();
24     class Inner {
25     public:
26       void instance_foo();
27       static void static_foo();
28     };
29   };
30 
31   template <class T> class Derived1 : Base<T> {
32   public:
test0()33     void test0() {
34       Base<T>::static_foo();
35       Base<T>::instance_foo();
36     }
37 
test1()38     void test1() {
39       Base<T>::Inner::static_foo();
40       Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
41     }
42 
test2()43     static void test2() {
44       Base<T>::static_foo();
45       Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
46     }
47 
test3()48     static void test3() {
49       Base<T>::Inner::static_foo();
50       Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
51     }
52   };
53 
54   template <class T> class Derived2 : Base<T>::Inner {
55   public:
test0()56     void test0() {
57       Base<T>::static_foo();
58       Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
59     }
60 
test1()61     void test1() {
62       Base<T>::Inner::static_foo();
63       Base<T>::Inner::instance_foo();
64     }
65 
test2()66     static void test2() {
67       Base<T>::static_foo();
68       Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
69     }
70 
test3()71     static void test3() {
72       Base<T>::Inner::static_foo();
73       Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
74     }
75   };
76 
test0()77   void test0() {
78     Derived1<int> d1;
79     d1.test0();
80     d1.test1(); // expected-note {{in instantiation of member function}}
81     d1.test2(); // expected-note {{in instantiation of member function}}
82     d1.test3(); // expected-note {{in instantiation of member function}}
83 
84     Derived2<int> d2;
85     d2.test0(); // expected-note {{in instantiation of member function}}
86     d2.test1();
87     d2.test2(); // expected-note {{in instantiation of member function}}
88     d2.test3(); // expected-note {{in instantiation of member function}}
89   }
90 }
91 
92 namespace test1 {
93   template <class T> struct Base {
94     void foo(T); // expected-note {{member is declared here}}
95   };
96 
97   template <class T> struct Derived : Base<T> {
doFootest1::Derived98     void doFoo(T v) {
99       foo(v); // expected-error {{explicit qualification required to use member 'foo' from dependent base class}}
100     }
101   };
102 
103   template struct Derived<int>; // expected-note {{requested here}}
104 }
105 
106 namespace PR8966 {
107   template <class T>
108   class MyClassCore
109   {
110   };
111 
112   template <class T>
113   class MyClass : public MyClassCore<T>
114   {
115   public:
116     enum  {
117       N
118     };
119 
120     // static member declaration
121     static const char* array [N];
122 
f()123     void f() {
124       MyClass<T>::InBase = 17;
125     }
126   };
127 
128   // static member definition
129   template <class T>
130   const char* MyClass<T>::array [MyClass<T>::N] = { "A", "B", "C" };
131 }
132 
133 namespace std {
134   inline namespace v1 {
135     template<typename T> struct basic_ostream;
136   }
137   namespace inner {
138     template<typename T> struct vector {};
139   }
140   using inner::vector;
141   template<typename T, typename U> struct pair {};
142   typedef basic_ostream<char> ostream;
143   extern ostream cout;
144   std::ostream &operator<<(std::ostream &out, const char *);
145 }
146 
147 namespace PR10053 {
148   template<typename T> struct A {
149     T t;
APR10053::A150     A() {
151       f(t); // expected-error {{call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup}}
152     }
153   };
154 
155   void f(int&); // expected-note {{'f' should be declared prior to the call site}}
156 
157   A<int> a; // expected-note {{in instantiation of member function}}
158 
159 
160   namespace N {
161     namespace M {
g(T t)162       template<typename T> int g(T t) {
163         f(t); // expected-error {{call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup}}
164       };
165     }
166 
167     void f(char&); // expected-note {{'f' should be declared prior to the call site}}
168   }
169 
170   void f(char&);
171 
172   int k = N::M::g<char>(0);; // expected-note {{in instantiation of function}}
173 
174 
175   namespace O {
176     int f(char&); // expected-note {{candidate function not viable}}
177 
178     template<typename T> struct C {
179       static const int n = f(T()); // expected-error {{no matching function}}
180     };
181   }
182 
183   int f(double); // no note, shadowed by O::f
184   O::C<double> c; // expected-note {{requested here}}
185 
186 
187   // Example from www/compatibility.html
188   namespace my_file {
Squared(T x)189     template <typename T> T Squared(T x) {
190       return Multiply(x, x); // expected-error {{neither visible in the template definition nor found by argument-dependent lookup}}
191     }
192 
Multiply(int x,int y)193     int Multiply(int x, int y) { // expected-note {{should be declared prior to the call site}}
194       return x * y;
195     }
196 
main()197     int main() {
198       Squared(5); // expected-note {{here}}
199     }
200   }
201 
202   // Example from www/compatibility.html
203   namespace my_file2 {
204     template<typename T>
Dump(const T & value)205     void Dump(const T& value) {
206       std::cout << value << "\n"; // expected-error {{neither visible in the template definition nor found by argument-dependent lookup}}
207     }
208 
209     namespace ns {
210       struct Data {};
211     }
212 
operator <<(std::ostream & out,ns::Data data)213     std::ostream& operator<<(std::ostream& out, ns::Data data) { // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2::ns'}}
214       return out << "Some data";
215     }
216 
Use()217     void Use() {
218       Dump(ns::Data()); // expected-note {{here}}
219     }
220   }
221 
222   namespace my_file2_a {
223     template<typename T>
Dump(const T & value)224     void Dump(const T &value) {
225       print(std::cout, value); // expected-error 4{{neither visible in the template definition nor found by argument-dependent lookup}}
226     }
227 
228     namespace ns {
229       struct Data {};
230     }
231     namespace ns2 {
232       struct Data {};
233     }
234 
235     std::ostream &print(std::ostream &out, int); // expected-note-re {{should be declared prior to the call site{{$}}}}
236     std::ostream &print(std::ostream &out, ns::Data); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2_a::ns'}}
237     std::ostream &print(std::ostream &out, std::vector<ns2::Data>); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2_a::ns2'}}
238     std::ostream &print(std::ostream &out, std::pair<ns::Data, ns2::Data>); // expected-note {{should be declared prior to the call site or in an associated namespace of one of its arguments}}
239 
Use()240     void Use() {
241       Dump(0); // expected-note {{requested here}}
242       Dump(ns::Data()); // expected-note {{requested here}}
243       Dump(std::vector<ns2::Data>()); // expected-note {{requested here}}
244       Dump(std::pair<ns::Data, ns2::Data>()); // expected-note {{requested here}}
245     }
246   }
247 
248   namespace unary {
249     template<typename T>
Negate(const T & value)250     T Negate(const T& value) {
251       return !value; // expected-error {{call to function 'operator!' that is neither visible in the template definition nor found by argument-dependent lookup}}
252     }
253 
254     namespace ns {
255       struct Data {};
256     }
257 
258     ns::Data operator!(ns::Data); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::unary::ns'}}
259 
Use()260     void Use() {
261       Negate(ns::Data()); // expected-note {{requested here}}
262     }
263   }
264 }
265 
266 namespace PR10187 {
267   namespace A1 {
268     template<typename T>
269     struct S {
fPR10187::A1::S270       void f() {
271         for (auto &a : e)
272           __range(a); // expected-error {{undeclared identifier '__range'}}
273       }
274       int e[10];
275     };
276   }
277 
278   namespace A2 {
279     template<typename T>
280     struct S {
fPR10187::A2::S281       void f() {
282         for (auto &a : e)
283           __range(a); // expected-error {{undeclared identifier '__range'}}
284       }
285       T e[10];
286     };
g()287     void g() {
288       S<int>().f(); // expected-note {{here}}
289     }
290     struct X {};
291     void __range(X);
h()292     void h() {
293       S<X>().f();
294     }
295   }
296 
297   namespace B {
298     template<typename T> void g(); // expected-note {{not viable}}
f()299     template<typename T> void f() {
300       g<int>(T()); // expected-error {{no matching function}}
301     }
302 
303     namespace {
304       struct S {};
305     }
306     void g(S);
307 
308     template void f<S>(); // expected-note {{here}}
309   }
310 }
311 
312 namespace rdar11242625 {
313 
314 template <typename T>
315 struct Main {
316   struct default_names {
317     typedef int id;
318   };
319 
320   template <typename T2 = typename default_names::id>
321   struct TS {
322     T2 q;
323   };
324 };
325 
326 struct Sub : public Main<int> {
327   TS<> ff;
328 };
329 
330 int arr[sizeof(Sub)];
331 
332 }
333 
334 namespace PR11421 {
335 template < unsigned > struct X {
336   static const unsigned dimension = 3;
337   template<unsigned dim=dimension>
338   struct Y: Y<dim> { }; // expected-error{{base class has incomplete type}}
339                         // expected-note@-1{{definition of 'Y<dim>' is not complete until the closing '}'}}
340 };
341 typedef X<3> X3;
342 X3::Y<>::iterator it; // expected-error {{no type named 'iterator' in 'PR11421::X<3>::Y<>'}}
343 }
344 
345 namespace rdar12629723 {
346   template<class T>
347   struct X {
348     struct C : public C { }; // expected-error{{base class has incomplete type}}
349                              // expected-note@-1{{definition of 'rdar12629723::X::C' is not complete until the closing '}'}}
350 
351     struct B;
352 
353     struct A : public B {
foordar12629723::X::A354       virtual void foo() { }
355     };
356 
357     struct D : T::foo { };
358     struct E : D { };
359   };
360 
361   template<class T>
362   struct X<T>::B : public A {
foordar12629723::X::B363     virtual void foo() { }
364   };
365 }
366 
367 namespace test_reserved_identifiers {
tempf(A a,B b)368   template<typename A, typename B> void tempf(A a, B b) {
369     a + b;  // expected-error{{call to function 'operator+' that is neither visible in the template definition nor found by argument-dependent lookup}}
370   }
371   namespace __gnu_cxx { struct X {}; }
372   namespace ns { struct Y {}; }
373   void operator+(__gnu_cxx::X, ns::Y);  // expected-note{{or in namespace 'test_reserved_identifiers::ns'}}
test()374   void test() {
375     __gnu_cxx::X x;
376     ns::Y y;
377     tempf(x, y);  // expected-note{{in instantiation of}}
378   }
379 }
380 
381 // This test must live in the global namespace.
382 struct PR14695_X {};
383 // FIXME: This note is bogus; it is the using directive which would need to move
384 // to prior to the call site to fix the problem.
385 namespace PR14695_A { void PR14695_f(PR14695_X); } // expected-note {{'PR14695_f' should be declared prior to the call site or in the global namespace}}
PR14695_g(T t)386 template<typename T> void PR14695_g(T t) { PR14695_f(t); } // expected-error {{call to function 'PR14695_f' that is neither visible in the template definition nor found by argument-dependent lookup}}
387 using namespace PR14695_A;
388 template void PR14695_g(PR14695_X); // expected-note{{requested here}}
389 
390 namespace OperatorNew {
f(T t)391   template<typename T> void f(T t) {
392     operator new(100, t); // expected-error{{call to function 'operator new' that is neither visible in the template definition nor found by argument-dependent lookup}}
393     // FIXME: This should give the same error.
394     new (t) int;
395   }
396   struct X {};
397 };
398 using size_t = decltype(sizeof(0));
399 void *operator new(size_t, OperatorNew::X); // expected-note-re {{should be declared prior to the call site{{$}}}}
400 template void OperatorNew::f(OperatorNew::X); // expected-note {{instantiation of}}
401 
402 namespace PR19936 {
T()403   template<typename T> decltype(*T()) f() {} // expected-note {{previous}}
T()404   template<typename T> decltype(T() * T()) g() {} // expected-note {{previous}}
405 
406   // Create some overloaded operators so we build an overload operator call
407   // instead of a builtin operator call for the dependent expression.
408   enum E {};
409   int operator*(E);
410   int operator*(E, E);
411 
412   // Check that they still profile the same.
T()413   template<typename T> decltype(*T()) f() {} // expected-error {{redefinition}}
T()414   template<typename T> decltype(T() * T()) g() {} // expected-error {{redefinition}}
415 }
416 
417 template <typename> struct CT2 {
418   template <class U> struct X;
419 };
420 template <typename T> int CT2<int>::X<>; // expected-error {{template parameter list matching the non-templated nested type 'CT2<int>' should be empty}}
421 
422 namespace DependentTemplateIdWithNoArgs {
f()423   template<typename T> void f() { T::template f(); } // expected-error {{a template argument list is expected after a name prefixed by the template keyword}}
424   struct X {
425     template<int = 0> static void f();
426   };
g()427   void g() { f<X>(); }
428 }
429 
430 namespace DependentUnresolvedUsingTemplate {
431   template<typename T>
432   struct X : T {
433     using T::foo;
fDependentUnresolvedUsingTemplate::X434     void f() { this->template foo(); } // expected-error {{does not refer to a template}} expected-error {{a template argument list is expected after a name prefixed by the template keyword}}
gDependentUnresolvedUsingTemplate::X435     void g() { this->template foo<>(); } // expected-error {{does not refer to a template}}
hDependentUnresolvedUsingTemplate::X436     void h() { this->template foo<int>(); } // expected-error {{does not refer to a template}}
437   };
438   struct A { template<typename = int> int foo(); };
439   struct B { int foo(); }; // expected-note 3{{non-template here}}
test(X<A> xa,X<B> xb)440   void test(X<A> xa, X<B> xb) {
441     xa.f();
442     xa.g();
443     xa.h();
444     xb.f(); // expected-note {{instantiation of}}
445     xb.g(); // expected-note {{instantiation of}}
446     xb.h(); // expected-note {{instantiation of}}
447   }
448 }
449 
450 namespace PR37680 {
451   template <class a> struct b : a {
452     using a::add;
addPR37680::b453     template<int> int add() { return this->template add(0); } // expected-error {{a template argument list is expected after a name prefixed by the template keyword}}
454   };
455   struct a {
456     template<typename T = void> int add(...);
457     void add(int);
458   };
f(b<a> ba)459   int f(b<a> ba) { return ba.add<0>(); }
460 }
461