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++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 4 // RUN: %clang_cc1 -std=c++1z %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 5 6 #if __cplusplus < 201103L 7 // expected-no-diagnostics 8 #endif 9 10 namespace dr1460 { // dr1460: 3.5 11 #if __cplusplus >= 201103L 12 namespace DRExample { 13 union A { 14 union {}; 15 union {}; A()16 constexpr A() {} 17 }; 18 constexpr A a = A(); 19 20 union B { 21 union {}; 22 union {}; 23 constexpr B() = default; 24 }; 25 constexpr B b = B(); 26 27 union C { 28 union {}; 29 union {}; 30 }; 31 constexpr C c = C(); 32 #if __cplusplus > 201103L f()33 constexpr void f() { C c; } 34 static_assert((f(), true), ""); 35 #endif 36 } 37 38 union A {}; 39 union B { int n; }; // expected-note +{{here}} 40 union C { int n = 0; }; 41 struct D { union {}; }; 42 struct E { union { int n; }; }; // expected-note +{{here}} 43 struct F { union { int n = 0; }; }; 44 45 struct X { 46 friend constexpr A::A() noexcept; 47 friend constexpr B::B() noexcept; // expected-error {{follows non-constexpr declaration}} 48 friend constexpr C::C() noexcept; 49 friend constexpr D::D() noexcept; 50 friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}} 51 friend constexpr F::F() noexcept; 52 }; 53 54 // These are OK, because value-initialization doesn't actually invoke the 55 // constructor. 56 constexpr A a = A(); 57 constexpr B b = B(); 58 constexpr C c = C(); 59 constexpr D d = D(); 60 constexpr E e = E(); 61 constexpr F f = F(); 62 63 namespace Defaulted { 64 union A { constexpr A() = default; }; 65 union B { int n; constexpr B() = default; }; // expected-error {{not constexpr}} 66 union C { int n = 0; constexpr C() = default; }; 67 struct D { union {}; constexpr D() = default; }; 68 struct E { union { int n; }; constexpr E() = default; }; // expected-error {{not constexpr}} 69 struct F { union { int n = 0; }; constexpr F() = default; }; 70 71 struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; // expected-error {{not constexpr}} 72 struct H { 73 union { 74 int n = 0; 75 }; 76 union { // expected-note 2{{member not initialized}} 77 int m; 78 }; Hdr1460::Defaulted::H79 constexpr H() {} // expected-error {{must initialize all members}} Hdr1460::Defaulted::H80 constexpr H(bool) : m(1) {} Hdr1460::Defaulted::H81 constexpr H(char) : n(1) {} // expected-error {{must initialize all members}} Hdr1460::Defaulted::H82 constexpr H(double) : m(1), n(1) {} 83 }; 84 } 85 86 #if __cplusplus > 201103L check()87 template<typename T> constexpr bool check() { 88 T t; // expected-note-re 2{{non-constexpr constructor '{{[BE]}}'}} 89 return true; 90 } 91 static_assert(check<A>(), ""); 92 static_assert(check<B>(), ""); // expected-error {{constant}} expected-note {{in call}} 93 static_assert(check<C>(), ""); 94 static_assert(check<D>(), ""); 95 static_assert(check<E>(), ""); // expected-error {{constant}} expected-note {{in call}} 96 static_assert(check<F>(), ""); 97 #endif 98 99 union G { 100 int a = 0; // expected-note {{previous initialization is here}} 101 int b = 0; // expected-error {{initializing multiple members of union}} 102 }; 103 union H { 104 union { 105 int a = 0; // expected-note {{previous initialization is here}} 106 }; 107 union { 108 int b = 0; // expected-error {{initializing multiple members of union}} 109 }; 110 }; 111 struct I { 112 union { 113 int a = 0; // expected-note {{previous initialization is here}} 114 int b = 0; // expected-error {{initializing multiple members of union}} 115 }; 116 }; 117 struct J { 118 union { int a = 0; }; 119 union { int b = 0; }; 120 }; 121 122 namespace Overriding { 123 struct A { 124 int a = 1, b, c = 3; Adr1460::Overriding::A125 constexpr A() : b(2) {} 126 }; 127 static_assert(A().a == 1 && A().b == 2 && A().c == 3, ""); 128 129 union B { 130 int a, b = 2, c; B()131 constexpr B() : a(1) {} B(char)132 constexpr B(char) : b(4) {} B(int)133 constexpr B(int) : c(3) {} B(const char *)134 constexpr B(const char*) {} 135 }; 136 static_assert(B().a == 1, ""); 137 static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 138 static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}} 139 static_assert(B('x').b == 4, ""); 140 static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 141 static_assert(B(123).c == 3, ""); 142 static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 143 static_assert(B("").b == 2, ""); 144 static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 145 146 struct C { 147 union { int a, b = 2, c; }; 148 union { int d, e = 5, f; }; Cdr1460::Overriding::C149 constexpr C() : a(1) {} Cdr1460::Overriding::C150 constexpr C(char) : c(3) {} Cdr1460::Overriding::C151 constexpr C(int) : d(4) {} Cdr1460::Overriding::C152 constexpr C(float) : f(6) {} Cdr1460::Overriding::C153 constexpr C(const char*) {} 154 }; 155 156 static_assert(C().a == 1, ""); 157 static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 158 static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 159 static_assert(C().e == 5, ""); 160 161 static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}} 162 static_assert(C('x').c == 3, ""); 163 static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 164 static_assert(C('x').e == 5, ""); 165 166 static_assert(C(1).b == 2, ""); 167 static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 168 static_assert(C(1).d == 4, ""); 169 static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 170 171 static_assert(C(1.f).b == 2, ""); 172 static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 173 static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}} 174 static_assert(C(1.f).f == 6, ""); 175 176 static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}} 177 static_assert(C("").b == 2, ""); 178 static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}} 179 static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}} 180 static_assert(C("").e == 5, ""); 181 static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}} 182 183 struct D; 184 extern const D d; 185 struct D { 186 int a; 187 union { 188 int b = const_cast<D&>(d).a = 1; // not evaluated 189 int c; 190 }; Ddr1460::Overriding::D191 constexpr D() : a(0), c(0) {} 192 }; 193 constexpr D d {}; 194 static_assert(d.a == 0, ""); 195 } 196 #endif 197 } 198 199 #if __cplusplus >= 201103L 200 namespace std { 201 typedef decltype(sizeof(int)) size_t; 202 203 // libc++'s implementation 204 template <class _E> 205 class initializer_list 206 { 207 const _E* __begin_; 208 size_t __size_; 209 initializer_list(const _E * __b,size_t __s)210 initializer_list(const _E* __b, size_t __s) 211 : __begin_(__b), __size_(__s) {} 212 213 public: 214 typedef _E value_type; 215 typedef const _E& reference; 216 typedef const _E& const_reference; 217 typedef size_t size_type; 218 219 typedef const _E* iterator; 220 typedef const _E* const_iterator; 221 initializer_list()222 initializer_list() : __begin_(nullptr), __size_(0) {} 223 size() const224 size_t size() const {return __size_;} begin() const225 const _E* begin() const {return __begin_;} end() const226 const _E* end() const {return __begin_ + __size_;} 227 }; 228 } // std 229 230 namespace dr1467 { // dr1467: 3.7 c++11 231 // List-initialization of aggregate from same-type object 232 233 namespace basic0 { 234 struct S { 235 int i = 42; 236 }; 237 238 S a; 239 S b(a); 240 S c{a}; 241 242 struct SS : public S { } x; 243 S y(x); 244 S z{x}; 245 } // basic0 246 247 namespace basic1 { 248 struct S { 249 int i{42}; 250 }; 251 252 S a; 253 S b(a); 254 S c{a}; 255 256 struct SS : public S { } x; 257 S y(x); 258 S z{x}; 259 } // basic1 260 261 namespace basic2 { 262 struct S { 263 int i = {42}; 264 }; 265 266 S a; 267 S b(a); 268 S c{a}; 269 270 struct SS : public S { } x; 271 S y(x); 272 S z{x}; 273 } // basic2 274 275 namespace dr_example { 276 struct OK { 277 OK() = default; 278 OK(const OK&) = default; OKdr1467::dr_example::OK279 OK(int) { } 280 }; 281 282 OK ok; 283 OK ok2{ok}; 284 285 struct X { 286 X() = default; 287 X(const X&) = default; 288 }; 289 290 X x; 291 X x2{x}; 292 } // dr_example 293 294 namespace nonaggregate { 295 struct NonAggregate { NonAggregatedr1467::nonaggregate::NonAggregate296 NonAggregate() {} 297 }; 298 299 struct WantsIt { 300 WantsIt(NonAggregate); 301 }; 302 303 void f(NonAggregate); 304 void f(WantsIt); 305 test1()306 void test1() { 307 NonAggregate n; 308 f({n}); 309 } 310 test2()311 void test2() { 312 NonAggregate x; 313 NonAggregate y{x}; 314 NonAggregate z{{x}}; 315 } 316 } // nonaggregate 317 318 namespace SelfInitIsNotListInit { 319 struct S { 320 S(); 321 explicit S(S &); 322 S(const S &); 323 }; 324 S s1; 325 S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor 326 } 327 328 struct NestedInit { int a, b, c; }; 329 NestedInit ni[1] = {{NestedInit{1, 2, 3}}}; 330 331 namespace NestedInit2 { 332 struct Pair { int a, b; }; 333 struct TwoPairs { TwoPairs(Pair, Pair); }; 334 struct Value { Value(Pair); Value(TwoPairs); }; f()335 void f() { Value{{{1,2},{3,4}}}; } 336 } 337 } // dr1467 338 339 namespace dr1490 { // dr1490: 3.7 c++11 340 // List-initialization from a string literal 341 342 char s[4]{"abc"}; // Ok 343 std::initializer_list<char>{"abc"}; // expected-error {{expected unqualified-id}}} 344 } // dr190 345 #endif 346