1 // RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 2 // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 3 // RUN: %clang_cc1 -std=c++1y %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 4 5 #if __cplusplus < 201103L 6 // expected-no-diagnostics 7 #endif 8 9 namespace dr1460 { // dr1460: 3.5 10 #if __cplusplus >= 201103L 11 namespace DRExample { 12 union A { 13 union {}; 14 union {}; 15 constexpr A() {} 16 }; 17 constexpr A a = A(); 18 19 union B { 20 union {}; 21 union {}; 22 constexpr B() = default; 23 }; 24 constexpr B b = B(); 25 26 union C { 27 union {}; 28 union {}; 29 }; 30 constexpr C c = C(); 31 #if __cplusplus > 201103L 32 constexpr void f() { C c; } 33 static_assert((f(), true), ""); 34 #endif 35 } 36 37 union A {}; 38 union B { int n; }; // expected-note +{{here}} 39 union C { int n = 0; }; 40 struct D { union {}; }; 41 struct E { union { int n; }; }; // expected-note +{{here}} 42 struct F { union { int n = 0; }; }; 43 44 struct X { 45 friend constexpr A::A() noexcept; 46 friend constexpr B::B() noexcept; // expected-error {{follows non-constexpr declaration}} 47 friend constexpr C::C() noexcept; 48 friend constexpr D::D() noexcept; 49 friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}} 50 friend constexpr F::F() noexcept; 51 }; 52 53 // These are OK, because value-initialization doesn't actually invoke the 54 // constructor. 55 constexpr A a = A(); 56 constexpr B b = B(); 57 constexpr C c = C(); 58 constexpr D d = D(); 59 constexpr E e = E(); 60 constexpr F f = F(); 61 62 namespace Defaulted { 63 union A { constexpr A() = default; }; 64 union B { int n; constexpr B() = default; }; // expected-error {{not constexpr}} 65 union C { int n = 0; constexpr C() = default; }; 66 struct D { union {}; constexpr D() = default; }; 67 struct E { union { int n; }; constexpr E() = default; }; // expected-error {{not constexpr}} 68 struct F { union { int n = 0; }; constexpr F() = default; }; 69 70 struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; // expected-error {{not constexpr}} 71 struct H { 72 union { 73 int n = 0; 74 }; 75 union { // expected-note 2{{member not initialized}} 76 int m; 77 }; 78 constexpr H() {} // expected-error {{must initialize all members}} 79 constexpr H(bool) : m(1) {} 80 constexpr H(char) : n(1) {} // expected-error {{must initialize all members}} 81 constexpr H(double) : m(1), n(1) {} 82 }; 83 } 84 85 #if __cplusplus > 201103L 86 template<typename T> constexpr bool check() { 87 T t; // expected-note-re 2{{non-constexpr constructor '{{[BE]}}'}} 88 return true; 89 } 90 static_assert(check<A>(), ""); 91 static_assert(check<B>(), ""); // expected-error {{constant}} expected-note {{in call}} 92 static_assert(check<C>(), ""); 93 static_assert(check<D>(), ""); 94 static_assert(check<E>(), ""); // expected-error {{constant}} expected-note {{in call}} 95 static_assert(check<F>(), ""); 96 #endif 97 98 union G { 99 int a = 0; // expected-note {{previous initialization is here}} 100 int b = 0; // expected-error {{initializing multiple members of union}} 101 }; 102 union H { 103 union { 104 int a = 0; // expected-note {{previous initialization is here}} 105 }; 106 union { 107 int b = 0; // expected-error {{initializing multiple members of union}} 108 }; 109 }; 110 struct I { 111 union { 112 int a = 0; // expected-note {{previous initialization is here}} 113 int b = 0; // expected-error {{initializing multiple members of union}} 114 }; 115 }; 116 struct J { 117 union { int a = 0; }; 118 union { int b = 0; }; 119 }; 120 121 namespace Overriding { 122 struct A { 123 int a = 1, b, c = 3; 124 constexpr A() : b(2) {} 125 }; 126 static_assert(A().a == 1 && A().b == 2 && A().c == 3, ""); 127 128 union B { 129 int a, b = 2, c; 130 constexpr B() : a(1) {} 131 constexpr B(char) : b(4) {} 132 constexpr B(int) : c(3) {} 133 constexpr B(const char*) {} 134 }; 135 static_assert(B().a == 1, ""); 136 static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 137 static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}} 138 static_assert(B('x').b == 4, ""); 139 static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 140 static_assert(B(123).c == 3, ""); 141 static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 142 static_assert(B("").b == 2, ""); 143 static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 144 145 struct C { 146 union { int a, b = 2, c; }; 147 union { int d, e = 5, f; }; 148 constexpr C() : a(1) {} 149 constexpr C(char) : c(3) {} 150 constexpr C(int) : d(4) {} 151 constexpr C(float) : f(6) {} 152 constexpr C(const char*) {} 153 }; 154 155 static_assert(C().a == 1, ""); 156 static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 157 static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 158 static_assert(C().e == 5, ""); 159 160 static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 161 static_assert(C('x').c == 3, ""); 162 static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 163 static_assert(C('x').e == 5, ""); 164 165 static_assert(C(1).b == 2, ""); 166 static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 167 static_assert(C(1).d == 4, ""); 168 static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 169 170 static_assert(C(1.f).b == 2, ""); 171 static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 172 static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 173 static_assert(C(1.f).f == 6, ""); 174 175 static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 176 static_assert(C("").b == 2, ""); 177 static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 178 static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 179 static_assert(C("").e == 5, ""); 180 static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}} 181 182 struct D; 183 extern const D d; 184 struct D { 185 int a; 186 union { 187 int b = const_cast<D&>(d).a = 1; // not evaluated 188 int c; 189 }; 190 constexpr D() : a(0), c(0) {} 191 }; 192 constexpr D d {}; 193 static_assert(d.a == 0, ""); 194 } 195 #endif 196 } 197