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