1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s 287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s 387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar 53d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregortemplate<typename T, T Divisor> 63d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregorclass X { 73d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregorpublic: 8d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith static const T value = 10 / Divisor; // expected-error{{in-class initializer for static data member is not a constant expression}} 93d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}; 103d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 113d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregorint array1[X<int, 2>::value == 5? 1 : -1]; 127c2342dd4c9947806842e5aca3d2bb2e542853c9John McCallX<int, 0> xi0; // expected-note{{in instantiation of template class 'X<int, 0>' requested here}} 133d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 143d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 153d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregortemplate<typename T> 163d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregorclass Y { 1787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar static const T value = 0; 1887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar#if __cplusplus <= 199711L 1987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// expected-warning@-2 {{in-class initializer for static data member of type 'const float' is a GNU extension}} 2087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar#else 2187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// expected-error@-4 {{in-class initializer for static data member of type 'const float' requires 'constexpr' specifier}} 2287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// expected-note@-5 {{add 'constexpr'}} 2387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar#endif 243d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}; 253d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 267c2342dd4c9947806842e5aca3d2bb2e542853c9John McCallY<float> fy; // expected-note{{in instantiation of template class 'Y<float>' requested here}} 2765b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor 2865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor 2965b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor// out-of-line static member variables 3065b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor 3165b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregortemplate<typename T> 3265b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregorstruct Z { 3365b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor static T value; 3465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor}; 3565b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor 3665b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregortemplate<typename T> 3765b90055dd30cfb83d8344ff62ed428257431be4Douglas GregorT Z<T>::value; // expected-error{{no matching constructor}} 3865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor 3965b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregorstruct DefCon {}; 4065b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor 4165b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregorstruct NoDefCon { 42b1622a1fd7b7f4ab8d00d0183d17c90ad25c14e3John McCall NoDefCon(const NoDefCon&); // expected-note{{candidate constructor}} 4365b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor}; 4465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor 4565b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregorvoid test() { 4665b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor DefCon &DC = Z<DefCon>::value; 4765b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor NoDefCon &NDC = Z<NoDefCon>::value; // expected-note{{instantiation}} 4808e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson} 492afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor 502afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor// PR5609 512afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregorstruct X1 { 522afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor ~X1(); // The errors won't be triggered without this dtor. 532afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor}; 542afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor 552afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregortemplate <typename T> 562afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregorstruct Y1 { 572afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor static char Helper(T); 582afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor static const int value = sizeof(Helper(T())); 592afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor}; 602afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor 612afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregorstruct X2 { 622afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor virtual ~X2(); 632afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor}; 642afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor 652afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregornamespace std { 662afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor class type_info { }; 672afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor} 682afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor 692afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregortemplate <typename T> 702afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregorstruct Y2 { 712afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor static T &Helper(); 722afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor static const int value = sizeof(typeid(Helper())); 732afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor}; 742afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor 752afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregortemplate <int> 762afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregorstruct Z1 {}; 772afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor 782afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregorvoid Test() { 792afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor Z1<Y1<X1>::value> x; 802afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor int y[Y1<X1>::value]; 812afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor Z1<Y2<X2>::value> x2; 822afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor int y2[Y2<X2>::value]; 832afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor} 841f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor 851f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor// PR5672 861f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregortemplate <int n> 871f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregorstruct X3 {}; 881f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor 891f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregorclass Y3 { 901f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor public: 911f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor ~Y3(); // The error isn't triggered without this dtor. 921f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor 931f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor void Foo(X3<1>); 941f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor}; 951f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor 961f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregortemplate <typename T> 971f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregorstruct SizeOf { 981f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor static const int value = sizeof(T); 991f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor}; 1001f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor 1011f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregorvoid MyTest3() { 1021f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor Y3().Foo(X3<SizeOf<char>::value>()); 1031f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor} 104449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor 105449d0a829007ea654912098e6a73134a2c529d61Douglas Gregornamespace PR6449 { 106449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor template<typename T> 107449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor struct X0 { 108449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor static const bool var = false; 109449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor }; 110449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor 111449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor template<typename T> 112449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor const bool X0<T>::var; 113449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor 114449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor template<typename T> 115449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor struct X1 : public X0<T> { 116449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor static const bool var = false; 117449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor }; 118449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor 119449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor template<typename T> 120449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor const bool X1<T>::var; 121449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor 122449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor template class X0<char>; 123449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor template class X1<char>; 124449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor 125449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor} 1260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines 1270e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestypedef char MyString[100]; 1280e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestemplate <typename T> 1290e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesstruct StaticVarWithTypedefString { 1300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines static MyString str; 1310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines}; 1320e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestemplate <typename T> 1330e2c34f92f00628d48968dfea096d36381f494cbStephen HinesMyString StaticVarWithTypedefString<T>::str = ""; 1340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines 1350e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesvoid testStaticVarWithTypedefString() { 1360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines (void)StaticVarWithTypedefString<int>::str; 1370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines} 138