Debian Codegraph @

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

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