1 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++98 -verify -fsyntax-only %s -Wno-c++11-extensions -Wno-c++1y-extensions -DPRECXX11
2 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++11 -verify -fsyntax-only -Wno-c++1y-extensions %s
3 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++1y -verify -fsyntax-only %s -DCPP1Y
8 #define static_assert _Static_assert
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'}}
23 namespace out_of_line
{
25 template<typename T
, typename T0
> static CONST T right
= T(100);
26 template<typename T
> static CONST T right
<T
,int> = T(5);
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}}
34 template<typename T
, typename T0
> static CONST T right
;
35 template<typename T
> static CONST T right
<T
,int>;
37 template<typename T
, typename T0
> CONST T
B1::right
= T(100);
38 template<typename T
> CONST T
B1::right
<T
,int> = T(5);
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}}
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}}
48 template<typename T
, typename T0
> static CONST T right
= T(100);
49 template<typename T
> static CONST T right
<T
,int> = T(5);
51 template<typename T
, typename T0
> CONST T
B3::right
;
52 template<typename T
> CONST T
B3::right
<T
,int>;
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>;
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>;
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}}
71 namespace non_const_init
{
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
;
78 template const int A::wrong_inst_undefined
<const int>; // expected-error {{undefined}}
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}}
84 template<typename T
> T
A::wrong_inst_out_of_line
= T(10);
85 template int A::wrong_inst_out_of_line
<int>;
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}}
91 template<typename T
> static T wrong_inst_fixed
;
92 template<typename T
> static T wrong_inst_fixed
<T
*>;
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'
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>;
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}}
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}}
113 template<typename U
> static U Data
;
114 template<typename U
> static CONST U Data
<U
*> = U(); // expected-note {{here}}
116 template<typename U
> static U Data2
;
117 template<typename U
> static CONST U Data2
<U
*> = U();
119 const int c0_test
= C0::Data
<int*>;
120 static_assert(c0_test
== 0, "");
121 template const int C0::Data
<int*>; // expected-error {{undefined}}
123 template<typename U
> const U
C0::Data2
<U
*>;
124 template const int C0::Data2
<int*>;
127 template<typename U
> static U Data
;
128 template<typename U
> static U
* Data
<U
*>; // Okay, with out-of-line definition
130 template<typename T
> T
* C1a::Data
<T
*> = new T();
131 template int* C1a::Data
<int*>;
134 template<typename U
> static U Data
;
135 template<typename U
> static CONST U
* Data
<U
*>; // Okay, with out-of-line definition
137 template<typename T
> CONST T
* C1b::Data
<T
*> = (T
*)(0);
138 template CONST
int* C1b::Data
<int*>;
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}}
144 template int* C2a::Data
<int*>; // expected-note {{in instantiation of static data member 'non_const_init::pointers::C2a::Data<int *>' requested here}}
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'}}
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}}
156 namespace constexpred
{
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 {{declaration of constexpr static data member 'right<float, int>' requires an initializer}}
168 template static constexpr int right
<int,int>; // expected-error {{expected '<' after 'template'}}
173 namespace in_class_template
{
177 template<typename U
> static U Data
; // expected-note {{here}}
178 template<typename U
> static CONST U Data
<U
*> = U();
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
*>;
186 template<typename U
> static U Data
;
187 template<typename U
> static U
* Data
<U
*>;
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}}
196 template<typename U
> static U Data
;
197 template<typename U
> static U
* Data
<U
*>;
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}}
206 template<typename U
> static CONST U Data
= U(100); // expected-note {{here}}
208 static_assert(D3
<float>::Data
<int> == 100, "");
209 template const char D3
<float>::Data
<char>; // expected-error {{undefined}}
211 namespace bug_files
{
214 template<typename U
> static U Data
;
215 template<typename U
> static CONST U Data
<U
*> = U(10); // expected-note {{previous declaration is here}}
218 template<typename U
> U D0a
<float>::Data
<U
*> = U(100); // expected-error {{redefinition of 'Data'}}
220 // FIXME: We should accept this, and the corresponding case for class
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
228 template<typename U
> static U Data
;
229 template<typename U
> static CONST U Data
<U
*> = U(10); // expected-note {{previous declaration is here}}
232 template<typename U
> U D1
<float>::Data
= U(10);
234 template<typename U
> U D1
<float>::Data
<U
*> = U(100); // expected-error{{redefinition of 'Data'}}
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
; // expected-note 3{{here}}
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;
246 static_assert(S
<int>::V1
<int> == 123, "");
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 {{}} expected-note {{initializer of 'V2<int *>' is unknown}}
254 static_assert(S
<int>::V2
<int&> == 789, ""); // expected-error {{}} expected-note {{initializer of 'V2<int &>' is unknown}}
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 {{}} expected-note {{initializer of 'V2<int &>' is unknown}}
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, "");
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";
272 static_assert(sizeof(var
<int>) == 12, "");
273 static_assert(sizeof(var
<char>) == 6, "");
274 static_assert(sizeof(var
<void*>) == 8, "");
276 template<typename
...> struct tuple
;
278 template<typename T
> struct A
{
279 template<typename U
> static T x
[];
280 template<typename U
> static T y
[];
282 template<typename
...U
> static T y
<tuple
<U
...> >[];
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, "");
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, "");
293 namespace bad_reference
{
295 template<typename T
> static int A
; // expected-note 4{{here}}
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'}}
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'}}
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'}}
309 struct X
: T::template A
<int> {}; // expected-error {{template name refers to non-type template 'S::template A'}}
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}}
318 namespace in_nested_classes
{
325 static int f
: I
; // expected-error {{static member 'f' cannot be a bit-field}}
329 namespace 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}}
336 A
<int> ai
; // expected-note {{in instantiation of}}
339 namespace member_access_is_ok
{
343 template<class T
, T N
> constexpr static T Var
= N
;
345 static_assert(A
{}.Var
<int,5> == 5,"");
350 template<class T
> struct A
{
351 template<class U
, T N
, U M
> static T
&& Var
;
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'>;
357 } // end ns member_access_is_ok
364 static constexpr T value
= 0;
367 template<typename TValue
>
371 static_assert(TValue::template value
<int> == 0, ""); // error
376 Something
<Value
>{}.foo();
383 namespace dependent_static_var_template
{
385 template<int = 0> static int n
; // expected-note 2{{here}}
387 int &r
= A::template n
; // expected-error {{use of variable template 'n' requires template arguments}}
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}}
394 template<int = 0> static int n
; // expected-note {{here}}
396 int &t
= B::template n
; // expected-error {{use of variable template 'n' requires template arguments}}
399 template <class T
> static T G
;
401 template<class T
> T
C::G
= T(6);
403 template <class T
> T
F() {
408 int cf() { return F
<int>(); }
412 namespace template_vars_in_template
{
413 template <int> struct TakesInt
{};
418 static constexpr int v
= 42;
422 constexpr int val
= v
<T
>;
426 constexpr int val
= v
<char>;
428 (void)ti
.x
; // expected-error{{no member named 'x' in 'template_vars_in_template::TakesInt<42>'}}
435 x
.mf2(); // expected-note{{in instantiation of member function 'template_vars_in_template::S<int>::mf2' requested here}}