Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/constexpr-union4.C

raw
1 // { dg-do compile { target c++20 } }
2
3 // from [class.union.general] p5
4
5 union A { int x; int y[4]; };
6 struct B { A a; };
7 union C { B b; int k; };
8 constexpr int f() {
9 C c; // does not start lifetime of any union member
10 c.b.a.y[3] = 4; // OK, S(c.b.a.y[3]) contains c.b and c.b.a.y;
11 // creates objects to hold union members c.b and c.b.a.y
12 return c.b.a.y[3]; // OK, c.b.a.y refers to newly created object (see [basic.life])
13 }
14 constexpr int a = f();
15
16 struct X { const int a; int b; };
17 union Y { X x; int k; };// { dg-message "does not implicitly begin its lifetime" }
18 constexpr int g() {
19 Y y = { { 1, 2 } }; // OK, y.x is active union member ([class.mem])
20 int n = y.x.a;
21 y.k = 4; // OK, ends lifetime of y.x, y.k is active member of union
22
23 y.x.b = n; // { dg-error "accessing .* member instead of initialized .* member" }
24 // undefined behavior: y.x.b modified outside its lifetime,
25 // S(y.x.b) is empty because X's default constructor is deleted,
26 // so union member y.x's lifetime does not implicitly start
27 return 0;
28 }
29 constexpr int b = g(); // { dg-message "in .constexpr. expansion" }