cxx1y-variable-templates_in_class.cpp revision 567f917df048d42732997a479b2b257403fc88ef
1// RUN: %clang_cc1 -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 4 5#define CONST const 6 7class A { 8 template<typename T> CONST T wrong; // expected-error {{member 'wrong' declared as a template}} 9 template<typename T> CONST T wrong_init = 5; // expected-error {{member 'wrong_init' declared as a template}} 10 template<typename T, typename T0> static CONST T right = T(100); 11 template<typename T> static CONST T right<T,int> = 5; 12 template<typename T> CONST int right<int,T>; // expected-error {{member 'right' declared as a template}} 13 template<typename T> CONST float right<float,T> = 5; // expected-error {{member 'right' declared as a template}} 14 template<> static CONST int right<int,int> = 7; // expected-error {{explicit specialization of 'right' in class scope}} 15 template<> static CONST float right<float,int>; // expected-error {{explicit specialization of 'right' in class scope}} 16 template static CONST int right<int,int>; // expected-error {{template specialization requires 'template<>'}} \ 17 // expected-error {{explicit specialization of 'right' in class scope}} 18}; 19 20namespace out_of_line { 21 class B0 { 22 template<typename T, typename T0> static CONST T right = T(100); 23 template<typename T> static CONST T right<T,int> = T(5); 24 }; 25 template<> CONST int B0::right<int,int> = 7; 26 template CONST int B0::right<int,int>; 27 template<> CONST int B0::right<int,float>; 28 template CONST int B0::right<int,float>; 29 30 class B1 { 31 template<typename T, typename T0> static CONST T right; 32 template<typename T> static CONST T right<T,int>; 33 }; 34 template<typename T, typename T0> CONST T B1::right = T(100); 35 template<typename T> CONST T B1::right<T,int> = T(5); 36 37 class B2 { 38 template<typename T, typename T0> static CONST T right = T(100); // expected-note {{previous definition is here}} 39 template<typename T> static CONST T right<T,int> = T(5); // expected-note {{previous definition is here}} 40 }; 41 template<typename T, typename T0> CONST T B2::right = T(100); // expected-error {{redefinition of 'right'}} 42 template<typename T> CONST T B2::right<T,int> = T(5); // expected-error {{redefinition of 'right'}} 43 44 class B3 { 45 template<typename T, typename T0> static CONST T right = T(100); 46 template<typename T> static CONST T right<T,int> = T(5); 47 }; 48 template<typename T, typename T0> CONST T B3::right; // expected-error {{forward declaration of variable template cannot have a nested name specifier}} 49 template<typename T> CONST T B3::right<T,int>; // expected-error {{forward declaration of variable template partial specialization cannot have a nested name specifier}} 50 51 class B4 { 52 template<typename T, typename T0> static CONST T right; 53 template<typename T> static CONST T right<T,int>; 54 template<typename T, typename T0> static CONST T right_def = T(100); 55 template<typename T> static CONST T right_def<T,int>; // expected-note {{explicit instantiation refers here}} 56 }; 57 template<typename T, typename T0> CONST T B4::right; // expected-error {{forward declaration of variable template cannot have a nested name specifier}} 58 template<typename T> CONST T B4::right<T,int>; // expected-error {{forward declaration of variable template partial specialization cannot have a nested name specifier}} \ 59 // expected-note {{explicit instantiation refers here}} 60 template CONST int B4::right<int,int>; // expected-error {{explicit instantiation of undefined static data member template 'right' of class}} 61 template CONST int B4::right_def<int,int>; // expected-error {{explicit instantiation of undefined static data member template 'right_def' of class}} 62} 63 64namespace non_const_init { 65 class A { 66 template<typename T> static T wrong_inst = T(10); // expected-error {{non-const static data member must be initialized out of line}} 67 template<typename T> static T wrong_inst_fixed; 68 }; 69 template int A::wrong_inst<int>; // expected-note {{in instantiation of static data member 'non_const_init::A::wrong_inst<int>' requested here}} 70 template<typename T> T A::wrong_inst_fixed = T(10); 71 template int A::wrong_inst_fixed<int>; 72 73 class B { 74 template<typename T> static T wrong_inst; 75 template<typename T> static T wrong_inst<T*> = T(100); // expected-error {{non-const static data member must be initialized out of line}} 76 77 template<typename T> static T wrong_inst_fixed; 78 template<typename T> static T wrong_inst_fixed<T*>; 79 }; 80 template int B::wrong_inst<int*>; // expected-note {{in instantiation of static data member 'non_const_init::B::wrong_inst<int *>' requested here}} 81 template<typename T> T B::wrong_inst_fixed = T(100); 82 template int B::wrong_inst_fixed<int>; 83 84 class C { 85 template<typename T> static CONST T right_inst = T(10); 86 template<typename T> static CONST T right_inst<T*> = T(100); 87 }; 88 template CONST int C::right_inst<int>; 89 template CONST int C::right_inst<int*>; 90 91 namespace pointers { 92 93 struct C0 { 94 template<typename U> static U Data; 95 template<typename U> static CONST U Data<U*> = U(); // Okay 96 }; 97 template CONST int C0::Data<int*>; 98 99 struct C1a { 100 template<typename U> static U Data; 101 template<typename U> static U* Data<U>; // Okay, with out-of-line definition 102 }; 103 template<typename T> T* C1a::Data<T> = new T(); 104 template int* C1a::Data<int>; 105 106 struct C1b { 107 template<typename U> static U Data; 108 template<typename U> static CONST U* Data<U>; // Okay, with out-of-line definition 109 }; 110 template<typename T> CONST T* C1b::Data<T> = (T*)(0); 111 template CONST int* C1b::Data<int>; 112 113 struct C2a { 114 template<typename U> static U Data; 115 template<typename U> static U* Data<U> = new U(); // expected-error {{non-const static data member must be initialized out of line}} 116 }; 117 template int* C2a::Data<int>; // expected-note {{in instantiation of static data member 'non_const_init::pointers::C2a::Data<int>' requested here}} 118 119 struct C2b { // FIXME: ?!? Should this be an error? pointer-types are automatically non-const? 120 template<typename U> static U Data; 121 template<typename U> static CONST U* Data<U> = (U*)(0); // expected-error {{non-const static data member must be initialized out of line}} 122 }; 123 template CONST int* C2b::Data<int>; // expected-note {{in instantiation of static data member 'non_const_init::pointers::C2b::Data<int>' requested here}} 124 } 125} 126 127struct matrix_constants { 128 // TODO: (?) 129}; 130 131namespace in_class_template { 132 133 template<typename T> 134 class D0 { 135 template<typename U> static U Data; 136 template<typename U> static CONST U Data<U*> = U(); 137 }; 138 template CONST int D0<float>::Data<int*>; 139 140 template<typename T> 141 class D1 { 142 template<typename U> static U Data; 143 template<typename U> static U* Data<U*>; 144 }; 145 template<typename T> 146 template<typename U> U* D1<T>::Data<U*> = (U*)(0); 147 template int* D1<float>::Data<int*>; 148 149 template<typename T> 150 class D2 { 151 template<typename U> static U Data; 152 template<typename U> static U* Data<U*>; 153 }; 154 template<> 155 template<typename U> U* D2<float>::Data<U*> = (U*)(0) + 1; 156 template int* D1<float>::Data<int*>; 157 158 template<typename T> 159 struct D3 { 160 template<typename U> static CONST U Data = U(100); 161 }; 162 template CONST int D3<float>::Data<int>; 163#ifndef PRECXX11 164 static_assert(D3<float>::Data<int> == 100, ""); 165#endif 166 167 namespace bug_files { 168 // FIXME: A bug has been filed addressing an issue similar to these. 169 // No error diagnosis should be produced, because an 170 // explicit specialization of a member templates of class 171 // template specialization should not inherit the partial 172 // specializations from the class template specialization. 173 174 template<typename T> 175 class D0 { 176 template<typename U> static U Data; 177 template<typename U> static CONST U Data<U*> = U(10); // expected-note {{previous definition is here}} 178 }; 179 template<> 180 template<typename U> U D0<float>::Data<U*> = U(100); // expected-error{{redefinition of 'Data'}} 181 182 template<typename T> 183 class D1 { 184 template<typename U> static U Data; 185 template<typename U> static U* Data<U*>; // expected-note {{previous definition is here}} 186 }; 187 template<typename T> 188 template<typename U> U* D1<T>::Data<U*> = (U*)(0); 189 template<> 190 template<typename U> U* D1<float>::Data<U*> = (U*)(0) + 1; // expected-error{{redefinition of 'Data'}} 191 } 192 193 namespace other_bugs { 194 // FIXME: This fails to properly initilize the variable 'k'. 195 196 template<typename A> struct S { 197 template<typename B> static int V; 198 template<typename B> static int V0; 199 }; 200 template struct S<int>; 201 template<typename A> template<typename B> int S<A>::V0 = 123; 202 template<typename A> template<typename B> int S<A>::V<B> = 123; 203 int k = S<int>::V<void>; 204 } 205} 206 207namespace in_nested_classes { 208 // TODO: 209} 210 211