Home | History | Annotate | Download | only in class.union
      1 // RUN: %clang_cc1 -verify -std=c++11 %s
      2 
      3 // Unlike in C++98, C++11 allows unions to have static data members.
      4 
      5 union U1 {
      6   static constexpr int k1 = 0;
      7   static const int k2 = k1;
      8   static int k3 = k2; // expected-error {{non-const static data member must be initialized out of line}}
      9   static constexpr double k4 = k2;
     10   static const double k5 = k4; // expected-error {{requires 'constexpr' specifier}} expected-note {{add 'constexpr'}}
     11   int n[k1 + 3];
     12 };
     13 
     14 constexpr int U1::k1;
     15 constexpr int U1::k2;
     16 int U1::k3;
     17 
     18 const double U1::k4;
     19 const double U1::k5;
     20 
     21 template<typename T>
     22 union U2 {
     23   static const int k1;
     24   static double k2;
     25   T t;
     26 };
     27 template<typename T> constexpr int U2<T>::k1 = sizeof(U2<T>);
     28 template<typename T> double U2<T>::k2 = 5.3;
     29 
     30 static_assert(U2<int>::k1 == sizeof(int), "");
     31 static_assert(U2<char>::k1 == sizeof(char), "");
     32 
     33 union U3 {
     34   static const int k;
     35   U3() : k(0) {} // expected-error {{does not name a non-static data member}}
     36 };
     37 
     38 struct S {
     39   union {
     40     static const int n; // expected-error {{static members cannot be declared in an anonymous union}}
     41     int a;
     42     int b;
     43   };
     44 };
     45 static union {
     46   static const int k; // expected-error {{static members cannot be declared in an anonymous union}}
     47   int n;
     48 };
     49