Clang Project

clang_source_code/test/SemaCXX/cxx1y-variable-templates_in_class.cpp
1// RUN: %clang_cc1 -std=c++98 -verify -fsyntax-only %s -Wno-c++11-extensions -Wno-c++1y-extensions -DPRECXX11
2// RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only -Wno-c++1y-extensions %s
3// RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only %s -DCPP1Y
4
5#define CONST const
6
7#ifdef PRECXX11
8#define static_assert(expr, msg) typedef int static_assert[(expr) ? 1 : -1];
9#endif
10
11class A {
12  template<typename T> CONST T wrong;           // expected-error {{member 'wrong' declared as a template}}
13  template<typename T> CONST T wrong_init = 5;      // expected-error {{member 'wrong_init' declared as a template}}
14  template<typename T, typename T0> static CONST T right = T(100);
15  template<typename T> static CONST T right<T,int> = 5;
16  template<typename T> CONST int right<int,T>;  // expected-error {{member 'right' declared as a template}}
17  template<typename T> CONST float right<float,T> = 5;  // expected-error {{member 'right' declared as a template}}
18  template<> static CONST int right<int,int> = 7;
19  template<> static CONST float right<float,int>;
20  template static CONST int right<int,int>;     // expected-error {{expected '<' after 'template'}}
21};
22
23namespace out_of_line {
24  class B0 {
25    template<typename T, typename T0> static CONST T right = T(100);
26    template<typename T> static CONST T right<T,int> = T(5);
27  };
28  template<> CONST int B0::right<int,int> = 7; // expected-note {{previous}}
29  template CONST int B0::right<int,int>; // expected-warning {{has no effect}}
30  template<> CONST int B0::right<int,float>; // expected-note {{previous}}
31  template CONST int B0::right<int,float>; // expected-warning {{has no effect}}
32
33  class B1 {
34    template<typename T, typename T0> static CONST T right;
35    template<typename T> static CONST T right<T,int>;
36  };
37  template<typename T, typename T0> CONST T B1::right = T(100);
38  template<typename T> CONST T B1::right<T,int> = T(5);
39
40  class B2 {
41    template<typename T, typename T0> static CONST T right = T(100);  // expected-note {{previous initialization is here}}
42    template<typename T> static CONST T right<T,int> = T(5);          // expected-note {{previous initialization is here}}
43  };
44  template<typename T, typename T0> CONST T B2::right = T(100);   // expected-error {{static data member 'right' already has an initializer}}
45  template<typename T> CONST T B2::right<T,int> = T(5);           // expected-error {{static data member 'right' already has an initializer}}
46
47  class B3 {
48    template<typename T, typename T0> static CONST T right = T(100);
49    template<typename T> static CONST T right<T,int> = T(5);
50  };
51  template<typename T, typename T0> CONST T B3::right;
52  template<typename T> CONST T B3::right<T,int>;
53
54  class B4 {
55    template<typename T, typename T0> static CONST T a;
56    template<typename T> static CONST T a<T,int> = T(100);
57    template<typename T, typename T0> static CONST T b = T(100);
58    template<typename T> static CONST T b<T,int>;
59  };
60  template<typename T, typename T0> CONST T B4::a; // expected-error {{default initialization of an object of const type 'const int'}}
61  template<typename T> CONST T B4::a<T,int>;
62  template CONST int B4::a<int,char>; // expected-note {{in instantiation of}}
63  template CONST int B4::a<int,int>;
64
65  template<typename T, typename T0> CONST T B4::b;
66  template<typename T> CONST T B4::b<T,int>; // expected-error {{default initialization of an object of const type 'const int'}}
67  template CONST int B4::b<int,char>;
68  template CONST int B4::b<int,int>; // expected-note {{in instantiation of}}
69}
70
71namespace non_const_init {
72  class A {
73    template<typename T> static T wrong_inst_undefined = T(10); // expected-note {{refers here}}
74    template<typename T> static T wrong_inst_defined = T(10); // expected-error {{non-const static data member must be initialized out of line}}
75    template<typename T> static T wrong_inst_out_of_line;
76  };
77
78  template const int A::wrong_inst_undefined<const int>; // expected-error {{undefined}}
79
80  template<typename T> T A::wrong_inst_defined;
81  template const int A::wrong_inst_defined<const int>;
82  template int A::wrong_inst_defined<int>; // expected-note {{in instantiation of static data member 'non_const_init::A::wrong_inst_defined<int>' requested here}}
83
84  template<typename T> T A::wrong_inst_out_of_line = T(10);
85  template int A::wrong_inst_out_of_line<int>;
86
87  class B {
88    template<typename T> static T wrong_inst; // expected-note {{refers here}}
89    template<typename T> static T wrong_inst<T*> = T(100); // expected-error {{non-const static data member must be initialized out of line}} expected-note {{refers here}}
90
91    template<typename T> static T wrong_inst_fixed;
92    template<typename T> static T wrong_inst_fixed<T*>;
93  };
94  template int B::wrong_inst<int>; // expected-error {{undefined}}
95  // FIXME: It'd be better to produce the 'explicit instantiation of undefined
96  // template' diagnostic here, not the 'must be initialized out of line'
97  // diagnostic.
98  template int B::wrong_inst<int*>; // expected-note {{in instantiation of static data member 'non_const_init::B::wrong_inst<int *>' requested here}}
99  template const int B::wrong_inst<const int*>; // expected-error {{undefined}}
100  template<typename T> T B::wrong_inst_fixed = T(100);
101  template int B::wrong_inst_fixed<int>;
102
103  class C {
104    template<typename T> static CONST T right_inst = T(10); // expected-note {{here}}
105    template<typename T> static CONST T right_inst<T*> = T(100); // expected-note {{here}}
106  };
107  template CONST int C::right_inst<int>; // expected-error {{undefined variable template}}
108  template CONST int C::right_inst<int*>; // expected-error {{undefined variable template}}
109
110  namespace pointers {
111
112    struct C0 {
113      template<typename U> static U Data;
114      template<typename U> static CONST U Data<U*> = U(); // expected-note {{here}}
115
116      template<typename U> static U Data2;
117      template<typename U> static CONST U Data2<U*> = U();
118    };
119    const int c0_test = C0::Data<int*>;
120    static_assert(c0_test == 0, "");
121    template const int C0::Data<int*>; // expected-error {{undefined}}
122
123    template<typename U> const U C0::Data2<U*>;
124    template const int C0::Data2<int*>;
125
126    struct C1a {
127      template<typename U> static U Data;
128      template<typename U> static U* Data<U*>;   // Okay, with out-of-line definition
129    };
130    template<typename T> T* C1a::Data<T*> = new T();
131    template int* C1a::Data<int*>;
132
133    struct C1b {
134      template<typename U> static U Data;
135      template<typename U> static CONST U* Data<U*>;   // Okay, with out-of-line definition
136    };
137    template<typename T> CONST T* C1b::Data<T*> = (T*)(0);
138    template CONST int* C1b::Data<int*>;
139
140    struct C2a {
141      template<typename U> static int Data;
142      template<typename U> static U* Data<U*> = new U();   // expected-error {{non-const static data member must be initialized out of line}}
143    };
144    template int* C2a::Data<int*>; // expected-note {{in instantiation of static data member 'non_const_init::pointers::C2a::Data<int *>' requested here}}
145
146    struct C2b {
147      template<typename U> static int Data;
148      template<typename U> static U *const Data<U*> = (U*)(0); // expected-error {{static data member of type 'int *const'}}
149    };
150    template<typename U> U *const C2b::Data<U*>;
151    template int *const C2b::Data<int*>; // expected-note {{in instantiation of static data member 'non_const_init::pointers::C2b::Data<int *>' requested here}}
152  }
153}
154
155#ifndef PRECXX11
156namespace constexpred {
157  class A {
158    template<typename T> constexpr T wrong;           // expected-error {{member 'wrong' declared as a template}} \
159                                                      // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}}
160    template<typename T> constexpr T wrong_init = 5;      // expected-error {{non-static data member cannot be constexpr; did you intend to make it static?}}
161    template<typename T, typename T0> static constexpr T right = T(100);
162    template<typename T> static constexpr T right<T,int> = 5;
163    template<typename T> constexpr int right<int,T>;  // expected-error {{member 'right' declared as a template}} \
164                                                      // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}}
165    template<typename T> constexpr float right<float,T> = 5;  // expected-error {{non-static data member cannot be constexpr; did you intend to make it static?}}
166    template<> static constexpr int right<int,int> = 7;
167    template<> static constexpr float right<float,int>; // expected-error {{requires an initializer}}
168    template static constexpr int right<int,int>;     // expected-error {{expected '<' after 'template'}}
169  };
170}
171#endif
172
173namespace in_class_template {
174
175  template<typename T>
176  class D0 {
177    template<typename U> static U Data; // expected-note {{here}}
178    template<typename U> static CONST U Data<U*> = U();
179  };
180  template CONST int D0<float>::Data<int*>;
181  template int D0<float>::Data<int>; // expected-error {{undefined}}
182  template<typename T> template<typename U> const U D0<T>::Data<U*>;
183
184  template<typename T>
185  class D1 {
186    template<typename U> static U Data;
187    template<typename U> static U* Data<U*>;
188  };
189  template<typename T>
190  template<typename U> U* D1<T>::Data<U*> = (U*)(0);
191  template int* D1<float>::Data<int*>; // expected-note {{previous}}
192  template int* D1<float>::Data<int*>; // expected-error {{duplicate explicit instantiation}}
193
194  template<typename T>
195  class D2 {
196    template<typename U> static U Data;
197    template<typename U> static U* Data<U*>;
198  };
199  template<>
200  template<typename U> U* D2<float>::Data<U*> = (U*)(0) + 1;
201  template int* D2<float>::Data<int*>; // expected-note {{previous}}
202  template int* D2<float>::Data<int*>; // expected-error {{duplicate explicit instantiation}}
203
204  template<typename T>
205  struct D3 {
206    template<typename U> static CONST U Data = U(100); // expected-note {{here}}
207  };
208  static_assert(D3<float>::Data<int> == 100, "");
209  template const char D3<float>::Data<char>; // expected-error {{undefined}}
210
211  namespace bug_files {
212    template<typename T>
213    class D0a {
214      template<typename U> static U Data;
215      template<typename U> static CONST U Data<U*> = U(10);  // expected-note {{previous declaration is here}}
216    };
217    template<>
218    template<typename U> U D0a<float>::Data<U*> = U(100);  // expected-error {{redefinition of 'Data'}}
219
220    // FIXME: We should accept this, and the corresponding case for class
221    // templates.
222    //
223    // [temp.class.spec.mfunc]/2: If the primary member template is explicitly
224    // specialized for a given specialization of the enclosing class template,
225    // the partial specializations of the member template are ignored
226    template<typename T>
227    class D1 {
228      template<typename U> static U Data;
229      template<typename U> static CONST U Data<U*> = U(10);  // expected-note {{previous declaration is here}}
230    };
231    template<>
232    template<typename U> U D1<float>::Data = U(10);
233    template<>
234    template<typename U> U D1<float>::Data<U*> = U(100);  // expected-error{{redefinition of 'Data'}}
235  }
236
237  namespace definition_after_outer_instantiation {
238    template<typename A> struct S {
239      template<typename B> static const int V1;
240      template<typename B> static const int V2;
241    };
242    template struct S<int>;
243    template<typename A> template<typename B> const int S<A>::V1 = 123;
244    template<typename A> template<typename B> const int S<A>::V2<B*> = 456;
245
246    static_assert(S<int>::V1<int> == 123, "");
247
248    // FIXME: The first and third case below possibly should be accepted. We're
249    // not picking up partial specializations added after the primary template
250    // is instantiated. This is kind of implied by [temp.class.spec.mfunc]/2,
251    // and matches our behavior for member class templates, but it's not clear
252    // that this is intentional. See PR17294 and core-24030.
253    static_assert(S<int>::V2<int*> == 456, ""); // FIXME expected-error {{}}
254    static_assert(S<int>::V2<int&> == 789, ""); // expected-error {{}}
255
256    template<typename A> template<typename B> const int S<A>::V2<B&> = 789;
257    static_assert(S<int>::V2<int&> == 789, ""); // FIXME expected-error {{}}
258
259    // All is OK if the partial specialization is declared before the implicit
260    // instantiation of the class template specialization.
261    static_assert(S<char>::V1<int> == 123, "");
262    static_assert(S<char>::V2<int*> == 456, "");
263    static_assert(S<char>::V2<int&> == 789, "");
264  }
265
266  namespace incomplete_array {
267    template<typename T> extern T var[];
268    template<typename T> T var[] = { 1, 2, 3 };
269    template<> char var<char>[] = "hello";
270    template<typename T> char var<T*>[] = "pointer";
271
272    static_assert(sizeof(var<int>) == 12, "");
273    static_assert(sizeof(var<char>) == 6, "");
274    static_assert(sizeof(var<void*>) == 8, "");
275
276    template<typename...> struct tuple;
277
278    template<typename T> struct A {
279      template<typename U> static T x[];
280      template<typename U> static T y[];
281
282      template<typename...U> static T y<tuple<U...> >[];
283    };
284
285    int *use_before_definition = A<int>::x<char>;
286    template<typename T> template<typename U> T A<T>::x[sizeof(U)];
287    static_assert(sizeof(A<int>::x<char>) == 4, "");
288
289    template<typename T> template<typename...U> T A<T>::y<tuple<U...> >[] = { U()... };
290    static_assert(sizeof(A<int>::y<tuple<char, char, char> >) == 12, "");
291  }
292
293  namespace bad_reference {
294    struct S {
295      template<typename T> static int A; // expected-note 4{{here}}
296    };
297
298    template<typename T> void f() {
299      typename T::template A<int> a; // expected-error {{template name refers to non-type template 'S::template A'}}
300    }
301    template<typename T> void g() {
302      T::template A<int>::B = 0; // expected-error {{template name refers to non-type template 'S::template A'}}
303    }
304    template<typename T> void h() {
305      class T::template A<int> c; // expected-error {{template name refers to non-type template 'S::template A'}}
306    }
307
308    template<typename T>
309    struct X : T::template A<int> {}; // expected-error {{template name refers to non-type template 'S::template A'}}
310
311    template void f<S>(); // expected-note {{in instantiation of}}
312    template void g<S>(); // expected-note {{in instantiation of}}
313    template void h<S>(); // expected-note {{in instantiation of}}
314    template struct X<S>; // expected-note {{in instantiation of}}
315  }
316}
317
318namespace in_nested_classes {
319  // TODO:
320}
321
322namespace bitfield {
323struct S {
324  template <int I>
325  static int f : I; // expected-error {{static member 'f' cannot be a bit-field}}
326};
327}
328
329namespace b20896909 {
330  // This used to crash.
331  template<typename T> struct helper {};
332  template<typename T> class A {
333    template <typename> static helper<typename T::error> x;  // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
334  };
335  void test() {
336    A<int> ai;  // expected-note {{in instantiation of}}
337  }
338}
339namespace member_access_is_ok {
340#ifdef CPP1Y
341  namespace ns1 {
342    struct A {
343      template<class T, T N> constexpr static T Var = N;
344    };
345    static_assert(A{}.Var<int,5> == 5,"");
346  } // end ns1
347#endif // CPP1Y
348
349namespace ns2 {
350  template<class T> struct A {
351    template<class U, T N, U M> static T&& Var;
352  };
353  template<class T> template<class U, T N, U M> T&& A<T>::Var = T(N + M);
354  int *AV = &A<int>().Var<char, 5, 'A'>;
355  
356} //end ns2
357} // end ns member_access_is_ok
358
359#ifdef CPP1Y
360namespace PR24473 {
361struct Value
362{
363    template<class T>
364    static constexpr T value = 0;
365};
366
367template<typename TValue>
368struct Something
369{
370    void foo() {
371        static_assert(TValue::template value<int> == 0, ""); // error
372    }
373};
374
375int main() { 
376    Something<Value>{}.foo();
377    return 0;
378}
379
380} // end ns PR24473
381#endif // CPP1Y
382
383namespace dependent_static_var_template {
384  struct A {
385    template<int = 0> static int n; // expected-note 2{{here}}
386  };
387  int &r = A::template n; // expected-error {{use of variable template 'n' requires template arguments}}
388
389  template<typename T>
390  int &f() { return T::template n; } // expected-error {{use of variable template 'n' requires template arguments}}
391  int &s = f<A>(); // expected-note {{instantiation of}}
392
393  namespace B {
394    template<int = 0> static int n; // expected-note {{here}}
395  }
396  int &t = B::template n; // expected-error {{use of variable template 'n' requires template arguments}}
397}
398