blob: 61fccac038bb12e36be27d857b3a8aa37bdb1ca7 [file] [log] [blame]
Larisse Voufo39a1e502013-08-06 01:03:05 +00001// RUN: %clang_cc1 -verify -fsyntax-only -Wno-c++11-extensions -Wno-c++1y-extensions %s
2// RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only -Wno-c++1y-extensions %s -DCXX11
3// RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only %s -DCXX11
4
5#ifdef CXX11
6 #define CONST constexpr
7#else
8 #define CONST const
9#endif
10
11template<typename T>
12T pi = T(3.1415926535897932385); // expected-note {{template is declared here}}
13
14template<typename T>
15CONST T cpi = T(3.1415926535897932385); // expected-note {{template is declared here}}
16
Larisse Voufoa11bd8a2013-08-13 02:02:26 +000017template<typename T> extern CONST T vc;
18#ifdef CXX11
19// expected-error@-2 {{constexpr variable declaration must be a definition}}
20#endif
21
Larisse Voufo39a1e502013-08-06 01:03:05 +000022namespace use_in_top_level_funcs {
23
24 void good() {
25 int ipi = pi<int>;
26 int icpi = cpi<int>;
27 double dpi = pi<double>;
28 double dcpi = cpi<double>;
29 }
30
31 void no_deduce() {
32 // template arguments are not deduced for uses of variable templates.
33 int ipi = pi; // expected-error {{cannot refer to variable template 'pi' without a template argument list}}
34 int icpi = cpi; // expected-error {{cannot refer to variable template 'cpi' without a template argument list}}
35 }
36
37 template<typename T>
38 T circular_area(T r) {
39 return pi<T> * r * r;
40 }
41
42 template<typename T>
43 CONST T const_circular_area(T r) {
44 return cpi<T> * r * r;
45 }
46
47 double use_circular_area(double r) {
48 CONST float t = const_circular_area(2.0) - 12;
49#ifdef CXX11
50 static_assert(const_circular_area(2) == 12, "");
51 CONST int test = (t > 0) && (t < 1);
52 static_assert(test, "");
53#endif
54 return circular_area(r);
55 }
56}
57
58namespace shadow {
59 void foo() {
60 int ipi0 = pi<int>;
61 int pi;
62 int a = pi;
63 int ipi = pi<int>; // expected-error {{expected '(' for function-style cast or type construction}} \
64 // expected-error {{expected expression}}
65 }
66}
67
68namespace odr_tmpl {
69 namespace pv_cvt {
70 int v; // expected-note {{previous definition is here}}
71 template<typename T> T v; // expected-error {{redefinition of 'v' as different kind of symbol}}
72 }
73 namespace pvt_cv {
74 template<typename T> T v; // expected-note {{previous definition is here}}
Larisse Voufod8dd97c2013-08-14 03:09:19 +000075 int v; // expected-error {{redefinition of 'v' as different kind of symbol}}
Larisse Voufo39a1e502013-08-06 01:03:05 +000076 }
77 namespace pvt_cvt {
78 template<typename T> T v0; // expected-note {{previous definition is here}}
79 template<typename T> T v0; // expected-error {{redefinition of 'v0'}}
80
81 template<typename T> T v; // expected-note {{previous definition is here}}
82 template<typename T> int v; // expected-error {{redefinition of 'v'}}
83
84 template<typename T> int v1; // expected-note {{previous template declaration is here}}
85 template<int I> int v1; // expected-error {{template parameter has a different kind in template redeclaration}}
86 }
87 namespace pvt_use {
88 template<typename T> T v;
89 v = 10; // expected-error {{C++ requires a type specifier for all declarations}}
90 }
91
92 namespace pvt_diff_params {
93 // FIXME: (?) Redefinitions should simply be not allowed, whether the
94 // template parameters match or not. However, this current behaviour also
95 // matches that of class templates...
96 template<typename T, typename> T v; // expected-note 2{{previous template declaration is here}}
97 template<typename T> T v; // expected-error {{too few template parameters in template redeclaration}}
98 template<typename T, typename, typename> T v; // expected-error {{too many template parameters in template redeclaration}}
99 }
100
101 namespace pvt_extern {
102 template<typename T> T v = T();
103 template<typename T> extern T v; // redeclaration is allowed \
104 // expected-note {{previous definition is here}}
105 template<typename T> extern int v; // expected-error {{redefinition of 'v' with a different type: 'int' vs 'T'}}
106
107#ifdef CXX11
108 template<typename T> extern auto v; // expected-error {{declaration of variable 'v' with type 'auto' requires an initializer}}
109#endif
Larisse Voufod8dd97c2013-08-14 03:09:19 +0000110
111 template<typename T> T var = T(); // expected-note {{previous definition is here}}
112 extern int var; // expected-error {{redefinition of 'var' as different kind of symbol}}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000113 }
114
115#ifdef CXX11
116 namespace pvt_auto {
117 template<typename T> auto v0; // expected-error {{declaration of variable 'v0' with type 'auto' requires an initializer}}
118 template<typename T> auto v1 = T(); // expected-note {{previous definition is here}}
119 template<typename T> int v1; // expected-error {{redefinition of 'v1' with a different type: 'int' vs 'auto'}}
120 template<typename T> auto v2 = T(); // expected-note {{previous definition is here}}
121 template<typename T> T v2; // expected-error {{redefinition of 'v2'}}
122 template<typename T> auto v3 = T(); // expected-note {{previous definition is here}}
123 template<typename T> extern T v3; // expected-error {{redefinition of 'v3' with a different type: 'T' vs 'auto'}}
124 template<typename T> auto v4 = T();
125 template<typename T> extern auto v4; // expected-error {{declaration of variable 'v4' with type 'auto' requires an initializer}}
126 }
127#endif
128
Larisse Voufodbd65772013-08-14 20:15:02 +0000129}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000130
131namespace explicit_instantiation {
132 template<typename T>
133 T pi0a = T(3.1415926535897932385); // expected-note {{variable template 'pi0a' declared here}}
134 template float pi0a<int>; // expected-error {{type 'float' of explicit instantiation of 'pi0a' does not match expected type 'int'}}
135
136 template<typename T>
137 T pi0b = T(3.1415926535897932385); // expected-note {{variable template 'pi0b' declared here}}
138 template CONST int pi0b<int>; // expected-error {{type 'const int' of explicit instantiation of 'pi0b' does not match expected type 'int'}}
139
140 template<typename T>
141 T pi0c = T(3.1415926535897932385); // expected-note {{variable template 'pi0c' declared here}}
142 template int pi0c<const int>; // expected-error {{type 'int' of explicit instantiation of 'pi0c' does not match expected type 'const int'}}
143
144 template<typename T>
145 T pi0 = T(3.1415926535897932385);
146 template int pi0<int>; // expected-note {{previous explicit instantiation is here}}
147 template int pi0<int>; // expected-error {{duplicate explicit instantiation of 'pi0<int>'}}
148
149 template<typename T>
150 CONST T pi1a = T(3.1415926535897932385); // expected-note {{variable template 'pi1a' declared here}}
151 template int pi1a<int>; // expected-error {{type 'int' of explicit instantiation of 'pi1a' does not match expected type 'const int'}}
152
153 template<typename T>
154 CONST T pi1b = T(3.1415926535897932385); // expected-note {{variable template 'pi1b' declared here}}
155 template int pi1b<const int>; // expected-error {{type 'int' of explicit instantiation of 'pi1b' does not match expected type 'const const int'}}
156
157 template<typename T>
158 CONST T pi1 = T(3.1415926535897932385);
159 template CONST int pi1<int>; // expected-note {{previous explicit instantiation is here}}
160 template CONST int pi1<int>; // expected-error {{duplicate explicit instantiation of 'pi1<int>'}}
161
162#ifdef CXX11
163 namespace auto_var {
164 template<typename T> auto var0 = T();
165 template auto var0<int>; // expected-error {{'auto' variable template instantiation is not allowed}}
166
167 template<typename T> auto var = T();
168 template int var<int>;
169 }
170#endif
171
172 namespace extern_var {
173 // TODO:
174 }
175}
176
177namespace explicit_specialization {
178
179 namespace good {
180 template<typename T1, typename T2>
181 CONST int pi2 = 1;
182
183 template<typename T>
184 CONST int pi2<T,int> = 2;
185
186 template<typename T>
187 CONST int pi2<int,T> = 3;
188
189 template<> CONST int pi2<int,int> = 4;
190
191#ifdef CXX11
192 void foo() {
193 static_assert(pi2<int,int> == 4, "");
194 static_assert(pi2<float,int> == 2, "");
195 static_assert(pi2<int,float> == 3, "");
196 static_assert(pi2<int,float> == pi2<int,double>, "");
197 static_assert(pi2<float,float> == 1, "");
198 static_assert(pi2<float,float> == pi2<float,double>, "");
199 }
200#endif
201 }
202
203 namespace ambiguous {
204
205 template<typename T1, typename T2>
206 CONST int pi2 = 1;
207
208 template<typename T>
209 CONST int pi2<T,int> = 2; // expected-note {{partial specialization matches [with T = int]}}
210
211 template<typename T>
212 CONST int pi2<int,T> = 3; // expected-note {{partial specialization matches [with T = int]}}
213
214 void foo() {
215 int a = pi2<int,int>; // expected-error {{ambiguous partial specializations of 'pi2<int, int>'}}
216 }
217 }
218
219 namespace type_changes {
220
221 template<typename T>
222 T pi0 = T(3.1415926535897932385);
223
224 template<> float pi0<int> = 10;
225 template<> int pi0<const int> = 10;
226
227 template<typename T>
228 T pi1 = T(3.1415926535897932385);
229 template<> CONST int pi1<int> = 10;
230
231 template<typename T>
232 T pi2 = T(3.1415926535897932385);
233 template<> int pi2<const int> = 10;
234
235 template<typename T>
236 CONST T pi4 = T(3.1415926535897932385);
237 template<> int pi4<int> = 10;
238 }
239
240 namespace redefinition {
241 template<typename T>
242 T pi0 = T(3.1415926535897932385);
243
244 template<> int pi0<int> = 10; // expected-note 3{{previous definition is here}}
245#ifdef CXX11
246// expected-note@-2 {{previous definition is here}}
247#endif
248 template<> int pi0<int> = 10; // expected-error {{redefinition of 'pi0<int>'}}
249 template<> CONST int pi0<int> = 10; // expected-error {{redefinition of 'pi0' with a different type: 'const int' vs 'int'}}
250 template<> float pi0<int> = 10; // expected-error {{redefinition of 'pi0' with a different type: 'float' vs 'int'}}
251#ifdef CXX11
252 template<> auto pi0<int> = 10; // expected-error {{redefinition of 'pi0<int>'}}
253#endif
254
255
256 template<typename T>
257 CONST T pi1 = T(3.1415926535897932385);
258
259 template<> CONST int pi1<int> = 10; // expected-note {{previous definition is here}}
260 template<> CONST int pi1<int> = 10; // expected-error {{redefinition of 'pi1<int>'}}
261 }
262
263 namespace before_instantiation {
264 template<typename T>
265 T pi0 = T(3.1415926535897932385); // expected-note {{variable template 'pi0' declared here}}
266
267 template<> int pi0<int> = 10;
268 template int pi0<int>;
269 template float pi0<int>; // expected-error {{type 'float' of explicit instantiation of 'pi0' does not match expected type}}
270
271 template<typename T1, typename T2>
272 CONST int pi2 = 1;
273
274 template<typename T> CONST int pi2<T,int> = 2;
275 template CONST int pi2<int,int>;
276 }
277 namespace after_instantiation {
278 template<typename T>
279 T pi0 = T(3.1415926535897932385);
280
281 template int pi0<int>; // expected-note 2{{explicit instantiation first required here}}
282 template<> int pi0<int> = 10; // expected-error {{explicit specialization of 'pi0' after instantiation}}
283 template<> float pi0<int>; // expected-error {{explicit specialization of 'pi0' after instantiation}}
284
285 template<typename T1, typename T2>
286 CONST int pi2 = 1;
287
288 template CONST int pi2<int,int>;
289 template<typename T> CONST int pi2<T,int> = 2;
290 }
291
292#ifdef CXX11
293 namespace auto_var {
294 template<typename T, typename> auto var0 = T();
295 template<typename T> auto var0<T,int> = T();
296 template<> auto var0<int,int> = 7;
297
298 template<typename T, typename> auto var = T();
299 template<typename T> T var<T,int> = T(5);
300 template<> int var<int,int> = 7;
301
302 void foo() {
303 int i0 = var0<int,int>;
304 int b = var<int,int>;
305 }
306 }
307#endif
308
309 namespace extern_var {
310 // TODO:
311 }
312
313 namespace diff_type {
314 // TODO:
315 template<typename T> T var = T();
316 template<typename T> T* var<T> = new T();
317#ifdef CXX11
318 template<typename T> auto var<T*> = T(); // expected-note {{previous definition is here}}
319 template<typename T> T var<T*> = T(); // expected-error {{redefinition of 'var' with a different type: 'T' vs 'auto'}}
320#endif
321 }
322}
323
Larisse Voufodbd65772013-08-14 20:15:02 +0000324namespace narrowing {
325 template<typename T> T v = {1234}; // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1234 to}}
326#ifdef CXX11
327 // expected-error@-2 {{constant expression evaluates to 1234 which cannot be narrowed to type 'char'}}\
328 // expected-note@-2 {{override this message by inserting an explicit cast}}
329#endif
330 int k = v<char>; // expected-note {{in instantiation of variable template specialization 'narrowing::v<char>' requested here}}
331}
332
Larisse Voufo39a1e502013-08-06 01:03:05 +0000333namespace use_in_structs {
334 // TODO:
335}
336
337namespace attributes {
338 // TODO:
339}
340
341#ifdef CXX11
342namespace arrays {
343 template<typename T>
344 T* arr = new T[10]{T(10), T(23)};
345
346 float f = 10.5;
347 template<> float* arr<float> = &f;
348
349 void bar() {
350 int *iarr = arr<int>;
351 iarr[0] = 1;
352 iarr[2] = 3;
353 iarr[6] = -2;
354
355 float ff = *arr<float>;
356 float nof = arr<float>[3]; // No bounds-check in C++
357 }
358}
359#endif
360
361namespace nested {
362
363 namespace n0a {
364 template<typename T>
365 T pi0a = T(3.1415926535897932385);
366 }
367
368 using namespace n0a;
369 int i0a = pi0a<int>;
370
371 template float pi0a<float>;
372 float f0a = pi0a<float>;
373
374 template<> double pi0a<double> = 5.2;
375 double d0a = pi0a<double>;
376
377 namespace n0b {
378 template<typename T>
379 T pi0b = T(3.1415926535897932385);
380 }
381
382 int i0b = n0b::pi0b<int>;
383
384 template float n0b::pi0b<float>;
385 float f0b = n0b::pi0b<float>;
386
387 template<> double n0b::pi0b<double> = 5.2;
388 double d0b = n0b::pi0b<double>;
389
390 namespace n1 {
391 template<typename T>
392 T pi1a = T(3.1415926535897932385);
393#ifdef CXX11
394// expected-note@-2 {{explicit instantiation refers here}}
395#endif
396
397 template<typename T>
398 T pi1b = T(3.1415926535897932385); // expected-note {{explicitly specialized declaration is here}}
399#ifdef CXX11
400// expected-note@-2 {{explicit instantiation refers here}}
401#endif
402 }
403
404 namespace use_n1a {
405 using namespace n1;
406 int i1 = pi1a<int>;
407
408 template float pi1a<float>;
409#ifdef CXX11
410// expected-error@-2 {{explicit instantiation of 'pi1a<float>' not in a namespace enclosing 'n1'}}
411#endif
412 float f1 = pi1a<float>;
413
414 template<> double pi1a<double> = 5.2; // expected-error {{no variable template matches specialization}}
415 double d1 = pi1a<double>;
416 }
417
418 namespace use_n1b {
419 int i1 = n1::pi1b<int>;
420
421 template float n1::pi1b<float>;
422#ifdef CXX11
423// expected-error@-2 {{explicit instantiation of 'pi1b<float>' not in a namespace enclosing 'n1'}}
424#endif
425 float f1 = n1::pi1b<float>;
426
427 template<> double n1::pi1b<double> = 5.2; // expected-error {{cannot define or redeclare 'pi1b' here because namespace 'use_n1b' does not enclose namespace 'n1'}} \
428 // expected-error {{variable template specialization of 'pi1b' must originally be declared in namespace 'n1'}}
429 double d1 = n1::pi1b<double>;
430 }
431}
432