File: constexpr-union4.C

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (29 lines) | stat: -rw-r--r-- 1,250 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// { dg-do compile { target c++20 } }

// from [class.union.general] p5

union A { int x; int y[4]; };
struct B { A a; };
union C { B b; int k; };
constexpr int f() {
  C c;                  // does not start lifetime of any union member
  c.b.a.y[3] = 4;       // OK, S(c.b.a.y[3]) contains c.b and c.b.a.y;
                        // creates objects to hold union members c.b and c.b.a.y
  return c.b.a.y[3];    // OK, c.b.a.y refers to newly created object (see [basic.life])
}
constexpr int a = f();

struct X { const int a; int b; };
union Y { X x; int k; };// { dg-message "does not implicitly begin its lifetime" }
constexpr int g() {
  Y y = { { 1, 2 } };   // OK, y.x is active union member ([class.mem])
  int n = y.x.a;
  y.k = 4;              // OK, ends lifetime of y.x, y.k is active member of union

  y.x.b = n;            // { dg-error "accessing .* member instead of initialized .* member" }
                        // undefined behavior: y.x.b modified outside its lifetime,
                        // S(y.x.b) is empty because X's default constructor is deleted,
                        // so union member y.x's lifetime does not implicitly start
  return 0;
}
constexpr int b = g();  // { dg-message "in .constexpr. expansion" }