| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
// PR c++/102286 |
| 3 |
|
| 4 |
#include "construct_at.h" |
| 5 |
|
| 6 |
struct S { const int a; int b; }; |
| 7 |
union U { int k; S s; }; |
| 8 |
|
| 9 |
constexpr int test1() { |
| 10 |
U u {}; |
| 11 |
std::construct_at(&u.s, S{ 1, 2 }); |
| 12 |
return u.s.b; |
| 13 |
} |
| 14 |
static_assert(test1() == 2); |
| 15 |
|
| 16 |
constexpr int test2() { |
| 17 |
U u {}; |
| 18 |
int* p = &u.s.b; |
| 19 |
std::construct_at(p, 5); // { dg-message "in .constexpr. expansion" } |
| 20 |
return u.s.b; |
| 21 |
} |
| 22 |
constexpr int x2 = test2(); // { dg-message "in .constexpr. expansion" } |
| 23 |
|
| 24 |
constexpr void foo(S* s) { |
| 25 |
s->b = 10; // { dg-error "accessing .U::s. member instead of initialized .U::k." } |
| 26 |
} |
| 27 |
constexpr int test3() { |
| 28 |
U u {}; |
| 29 |
foo(&u.s); // { dg-message "in .constexpr. expansion" } |
| 30 |
return u.s.b; |
| 31 |
} |
| 32 |
constexpr int x3 = test3(); // { dg-message "in .constexpr. expansion" } |
| 33 |
|
| 34 |
struct S2 { int a; int b; }; |
| 35 |
union U2 { int k; S2 s; }; |
| 36 |
constexpr int test4() { |
| 37 |
U2 u; |
| 38 |
int* p = &u.s.b; |
| 39 |
std::construct_at(p, 8); // { dg-message "in .constexpr. expansion" } |
| 40 |
return u.s.b; |
| 41 |
}; |
| 42 |
constexpr int x4 = test4(); // { dg-message "in .constexpr. expansion" } |
| 43 |
|
| 44 |
constexpr int test5() { |
| 45 |
union { |
| 46 |
int data[1]; |
| 47 |
} u; |
| 48 |
std::construct_at(u.data, 0); // { dg-message "in .constexpr. expansion" } |
| 49 |
return 0; |
| 50 |
} |
| 51 |
constexpr int x5 = test5(); // { dg-message "in .constexpr. expansion" } |
| 52 |
|
| 53 |
// { dg-error "accessing (uninitialized member|.* member instead of)" "" { target *-*-* } 0 } |