Debian Codegraph @

gcc/testsuite/g++.dg/modules/dr2867-3_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, d, i;
16
17 struct A {
18 A () { assert (c == 3); ++c; }
19 ~A () { ++a; }
20 template <int I> int &get () const { assert (c == 5 + I); ++c; return i; }
21 };
22
23 template <> struct std::tuple_size <A> { static const int value = 4; };
24 template <int I> struct std::tuple_element <I, A> { using type = int; };
25 template <> struct std::tuple_size <const A> { static const int value = 4; };
26 template <int I> struct std::tuple_element <I, const A> { using type = int; };
27
28 struct B {
29 B () { assert (c >= 1 && c <= 2); ++c; }
30 ~B () { assert (c >= 9 && c <= 10); ++c; }
31 };
32
33 struct C {
34 constexpr C () {}
35 constexpr C (const C &) {}
36 template <int I> int &get () const { assert (d == 1 + I); ++d; return i; }
37 };
38
39 template <> struct std::tuple_size <C> { static const int value = 3; };
40 template <int I> struct std::tuple_element <I, C> { using type = int; };
41 template <> struct std::tuple_size <const C> { static const int value = 3; };
42 template <int I> struct std::tuple_element <I, const C> { using type = int; };
43
44 inline A
45 foo (const B &, const B &)
46 {
47 A a;
48 assert (c == 4);
49 ++c;
50 return a;
51 }
52
53 constexpr C
54 foo (const C &, const C &)
55 {
56 return C {};
57 }
58
59 inline int
60 bar (int &x, int y)
61 {
62 x = y;
63 return y;
64 }
65
66 inline int
67 baz (int &x, int y)
68 {
69 assert (x == y);
70 return y;
71 }
72
73 struct E {
74 ~E () { assert (a == 2); }
75 };
76
77 namespace {
78 thread_local E e;
79 thread_local int c1 = bar (c, 1);
80 thread_local const auto &[x, y, z, w] = foo (B {}, B {});
81 thread_local int c2 = baz (c, 11);
82 thread_local int d1 = bar (d, 1);
83 thread_local const auto &[s, t, u] = foo (C {}, C {});
84 thread_local int d2 = baz (d, 4);
85 thread_local int c3 = bar (c, 1);
86 thread_local auto [x2, y2, z2, w2] = foo (B {}, B {});
87 thread_local int c4 = baz (c, 11);
88 thread_local int d3 = bar (d, 1);
89 thread_local auto [s2, t2, u2] = foo (C {}, C {});
90 thread_local int d4 = baz (d, 4);
91 }