blob: f309f29eafab768eaa5537f51f60fc80fdecb98f [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregor3d7a12a2009-03-25 23:32:15 +00002template<typename T, T Divisor>
3class X {
4public:
Richard Smithd7c56e12011-12-29 21:57:33 +00005 static const T value = 10 / Divisor; // expected-error{{in-class initializer for static data member is not a constant expression}}
Douglas Gregor3d7a12a2009-03-25 23:32:15 +00006};
7
8int array1[X<int, 2>::value == 5? 1 : -1];
John McCall7c2342d2010-03-10 11:27:22 +00009X<int, 0> xi0; // expected-note{{in instantiation of template class 'X<int, 0>' requested here}}
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000010
11
12template<typename T>
13class Y {
Richard Smith947be192011-09-29 23:18:34 +000014 static const T value = 0; // expected-warning{{in-class initializer for static data member of type 'const float' is a GNU extension}}
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000015};
16
John McCall7c2342d2010-03-10 11:27:22 +000017Y<float> fy; // expected-note{{in instantiation of template class 'Y<float>' requested here}}
Douglas Gregor65b90052009-07-27 17:43:39 +000018
19
20// out-of-line static member variables
21
22template<typename T>
23struct Z {
24 static T value;
25};
26
27template<typename T>
28T Z<T>::value; // expected-error{{no matching constructor}}
29
30struct DefCon {};
31
32struct NoDefCon {
John McCallb1622a12010-01-06 09:43:14 +000033 NoDefCon(const NoDefCon&); // expected-note{{candidate constructor}}
Douglas Gregor65b90052009-07-27 17:43:39 +000034};
35
36void test() {
37 DefCon &DC = Z<DefCon>::value;
38 NoDefCon &NDC = Z<NoDefCon>::value; // expected-note{{instantiation}}
Owen Anderson08e25242009-07-27 22:29:56 +000039}
Douglas Gregor2afce722009-11-26 00:44:06 +000040
41// PR5609
42struct X1 {
43 ~X1(); // The errors won't be triggered without this dtor.
44};
45
46template <typename T>
47struct Y1 {
48 static char Helper(T);
49 static const int value = sizeof(Helper(T()));
50};
51
52struct X2 {
53 virtual ~X2();
54};
55
56namespace std {
57 class type_info { };
58}
59
60template <typename T>
61struct Y2 {
62 static T &Helper();
63 static const int value = sizeof(typeid(Helper()));
64};
65
66template <int>
67struct Z1 {};
68
69void Test() {
70 Z1<Y1<X1>::value> x;
71 int y[Y1<X1>::value];
72 Z1<Y2<X2>::value> x2;
73 int y2[Y2<X2>::value];
74}
Douglas Gregor1f5f3a42009-12-03 17:10:37 +000075
76// PR5672
77template <int n>
78struct X3 {};
79
80class Y3 {
81 public:
82 ~Y3(); // The error isn't triggered without this dtor.
83
84 void Foo(X3<1>);
85};
86
87template <typename T>
88struct SizeOf {
89 static const int value = sizeof(T);
90};
91
92void MyTest3() {
93 Y3().Foo(X3<SizeOf<char>::value>());
94}
Douglas Gregor449d0a82010-03-01 19:11:54 +000095
96namespace PR6449 {
97 template<typename T>
98 struct X0 {
99 static const bool var = false;
100 };
101
102 template<typename T>
103 const bool X0<T>::var;
104
105 template<typename T>
106 struct X1 : public X0<T> {
107 static const bool var = false;
108 };
109
110 template<typename T>
111 const bool X1<T>::var;
112
113 template class X0<char>;
114 template class X1<char>;
115
116}