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