instantiate-static-var.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. template<typename T, T Divisor>
  3. class X {
  4. public:
  5. static const T value = 10 / Divisor; // expected-error{{in-class initializer for static data member is not a constant expression}}
  6. };
  7. int array1[X<int, 2>::value == 5? 1 : -1];
  8. X<int, 0> xi0; // expected-note{{in instantiation of template class 'X<int, 0>' requested here}}
  9. template<typename T>
  10. class Y {
  11. static const T value = 0; // expected-warning{{in-class initializer for static data member of type 'const float' is a GNU extension}}
  12. };
  13. Y<float> fy; // expected-note{{in instantiation of template class 'Y<float>' requested here}}
  14. // out-of-line static member variables
  15. template<typename T>
  16. struct Z {
  17. static T value;
  18. };
  19. template<typename T>
  20. T Z<T>::value; // expected-error{{no matching constructor}}
  21. struct DefCon {};
  22. struct NoDefCon {
  23. NoDefCon(const NoDefCon&); // expected-note{{candidate constructor}}
  24. };
  25. void test() {
  26. DefCon &DC = Z<DefCon>::value;
  27. NoDefCon &NDC = Z<NoDefCon>::value; // expected-note{{instantiation}}
  28. }
  29. // PR5609
  30. struct X1 {
  31. ~X1(); // The errors won't be triggered without this dtor.
  32. };
  33. template <typename T>
  34. struct Y1 {
  35. static char Helper(T);
  36. static const int value = sizeof(Helper(T()));
  37. };
  38. struct X2 {
  39. virtual ~X2();
  40. };
  41. namespace std {
  42. class type_info { };
  43. }
  44. template <typename T>
  45. struct Y2 {
  46. static T &Helper();
  47. static const int value = sizeof(typeid(Helper()));
  48. };
  49. template <int>
  50. struct Z1 {};
  51. void Test() {
  52. Z1<Y1<X1>::value> x;
  53. int y[Y1<X1>::value];
  54. Z1<Y2<X2>::value> x2;
  55. int y2[Y2<X2>::value];
  56. }
  57. // PR5672
  58. template <int n>
  59. struct X3 {};
  60. class Y3 {
  61. public:
  62. ~Y3(); // The error isn't triggered without this dtor.
  63. void Foo(X3<1>);
  64. };
  65. template <typename T>
  66. struct SizeOf {
  67. static const int value = sizeof(T);
  68. };
  69. void MyTest3() {
  70. Y3().Foo(X3<SizeOf<char>::value>());
  71. }
  72. namespace PR6449 {
  73. template<typename T>
  74. struct X0 {
  75. static const bool var = false;
  76. };
  77. template<typename T>
  78. const bool X0<T>::var;
  79. template<typename T>
  80. struct X1 : public X0<T> {
  81. static const bool var = false;
  82. };
  83. template<typename T>
  84. const bool X1<T>::var;
  85. template class X0<char>;
  86. template class X1<char>;
  87. }
  88. typedef char MyString[100];
  89. template <typename T>
  90. struct StaticVarWithTypedefString {
  91. static MyString str;
  92. };
  93. template <typename T>
  94. MyString StaticVarWithTypedefString<T>::str = "";
  95. void testStaticVarWithTypedefString() {
  96. (void)StaticVarWithTypedefString<int>::str;
  97. }