blob: 5fb8a671e31b5a975ebe47cacc4b9559a9abada4 [file] [log] [blame]
Richard Smithb9c64d82012-02-16 20:41:22 +00001// RUN: %clang_cc1 -verify -std=c++11 %s
2
3// Unlike in C++98, C++11 allows unions to have static data members.
4
5union 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;
David Blaikiea367e9d2013-01-29 22:26:08 +000010 static const double k5 = k4; // expected-error {{requires 'constexpr' specifier}} expected-note {{add 'constexpr'}}
Richard Smithb9c64d82012-02-16 20:41:22 +000011 int n[k1 + 3];
12};
13
14constexpr int U1::k1;
15constexpr int U1::k2;
16int U1::k3;
17
18const double U1::k4;
19const double U1::k5;
20
21template<typename T>
22union U2 {
23 static const int k1;
24 static double k2;
25 T t;
26};
27template<typename T> constexpr int U2<T>::k1 = sizeof(U2<T>);
28template<typename T> double U2<T>::k2 = 5.3;
29
30static_assert(U2<int>::k1 == sizeof(int), "");
31static_assert(U2<char>::k1 == sizeof(char), "");
32
33union U3 {
34 static const int k;
35 U3() : k(0) {} // expected-error {{does not name a non-static data member}}
36};
Richard Smith449d4f02012-02-16 21:23:54 +000037
38struct 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};
45static union {
46 static const int k; // expected-error {{static members cannot be declared in an anonymous union}}
47 int n;
48};