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