Debian Codegraph @

gcc/testsuite/g++.dg/DRs/dr2867-8.C

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