Debian Codegraph @

gcc/testsuite/g++.dg/modules/dr2867-4_a.H

raw
1 // CWG2867 - Order of initialization for structured bindings.
2 // { dg-require-effective-target c++20 }
3 // { dg-additional-options -fmodule-header }
4 // { dg-module-cmi {} }
5 // { dg-add-options tls }
6 // { dg-require-effective-target tls_runtime }
7
8 #define assert(X) do { if (!(X)) __builtin_abort(); } while (0)
9
10 namespace std {
11 template<typename T> struct tuple_size;
12 template<int, typename> struct tuple_element;
13 }
14
15 extern int a, c;
16
17 struct C {
18 C () { assert (c >= 5 && c <= 17 && (c - 5) % 4 == 0); ++c; }
19 ~C () { assert (c >= 8 && c <= 20 && c % 4 == 0); ++c; }
20 };
21
22 struct D {
23 D () { assert (c >= 7 && c <= 19 && (c - 7) % 4 == 0); ++c; }
24 ~D () { assert (a % 5 != 4); ++a; }
25 };
26
27 struct A {
28 A () { assert (c == 3); ++c; }
29 ~A () { assert (a % 5 == 4); ++a; }
30 template <int I> D get (const C & = C{}) const { assert (c == 6 + 4 * I); ++c; return D {}; }
31 };
32
33 template <> struct std::tuple_size <A> { static const int value = 4; };
34 template <int I> struct std::tuple_element <I, A> { using type = D; };
35 template <> struct std::tuple_size <const A> { static const int value = 4; };
36 template <int I> struct std::tuple_element <I, const A> { using type = D; };
37
38 struct B {
39 B () { assert (c >= 1 && c <= 2); ++c; }
40 ~B () { assert (c >= 21 && c <= 22); ++c; }
41 };
42
43 inline A
44 foo (const B &, const B &)
45 {
46 A a;
47 assert (c == 4);
48 ++c;
49 return a;
50 }
51
52 inline int
53 bar (int &x, int y)
54 {
55 x = y;
56 return y;
57 }
58
59 inline int
60 baz (int &x, int y)
61 {
62 assert (x == y);
63 return y;
64 }
65
66 struct E {
67 ~E () { assert (a == 5); }
68 };
69
70 namespace {
71 thread_local E e;
72 thread_local int c1 = bar (c, 1);
73 // First B::B () is invoked twice, then foo called, which invokes A::A ().
74 // e is reference bound to the A::A () constructed temporary.
75 // Then 4 times (in increasing I):
76 // C::C () is invoked, get is called, D::D () is invoked, C::~C () is
77 // invoked.
78 // After that B::~B () is invoked twice.
79 // At exit time D::~D () is invoked 4 times, then A::~A ().
80 thread_local const auto &[x, y, z, w] = foo (B {}, B {});
81 thread_local int c2 = baz (c, 23);
82 }