| 1 | // RUN: %clang_cc1 %s -Wno-uninitialized -std=c++11 -fsyntax-only -verify |
| 2 | |
| 3 | struct A { |
| 4 | constexpr A() : a(b + 1), b(a + 1) {} // expected-note {{outside its lifetime}} |
| 5 | int a; |
| 6 | int b; |
| 7 | }; |
| 8 | struct B { |
| 9 | A a; |
| 10 | }; |
| 11 | |
| 12 | constexpr A a; // ok, zero initialization precedes static initialization |
| 13 | void f() { |
| 14 | constexpr A a; // expected-error {{constant expression}} expected-note {{in call to 'A()'}} |
| 15 | } |
| 16 | |
| 17 | constexpr B b1; // ok |
| 18 | constexpr B b2 = B(); // ok |
| 19 | static_assert(b2.a.a == 1, ""); |
| 20 | static_assert(b2.a.b == 2, ""); |
| 21 | |
| 22 | struct C { |
| 23 | int c; |
| 24 | }; |
| 25 | struct D : C { int d; }; |
| 26 | constexpr C c1; // expected-error {{without a user-provided default constructor}} |
| 27 | constexpr C c2 = C(); // ok |
| 28 | constexpr D d1; // expected-error {{without a user-provided default constructor}} |
| 29 | constexpr D d2 = D(); // ok with DR1452 |
| 30 | static_assert(D().c == 0, ""); |
| 31 | static_assert(D().d == 0, ""); |
| 32 | |
| 33 | struct V : virtual C {}; |
| 34 | template<typename T> struct Z : T { |
| 35 | constexpr Z() : V() {} |
| 36 | }; |
| 37 | constexpr int n = Z<V>().c; // expected-error {{constant expression}} expected-note {{non-literal type 'Z<V>'}} |
| 38 | |
| 39 | struct E { |
| 40 | A a[2]; |
| 41 | }; |
| 42 | constexpr E e; // ok |
| 43 | static_assert(e.a[0].a == 1, ""); |
| 44 | static_assert(e.a[0].b == 2, ""); |
| 45 | static_assert(e.a[1].a == 1, ""); |
| 46 | static_assert(e.a[1].b == 2, ""); |
| 47 | |