Debian Codegraph @

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

raw
1 // CWG2867 - Order of initialization for structured bindings.
2 // { dg-do run { target c++11 } }
3 // { dg-options "" }
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 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 A
41 foo (const B &, const B &)
42 {
43 A a;
44 assert (c == 4);
45 ++c;
46 return a;
47 }
48
49 int
50 bar (int &x, int y)
51 {
52 x = y;
53 return y;
54 }
55
56 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 E e;
68 int c1 = bar (c, 1);
69 // First B::B () is invoked twice, then foo called, which invokes A::A ().
70 // e is reference bound to the A::A () constructed temporary.
71 // Then 4 times (in increasing I):
72 // C::C () is invoked, get is called, D::D () is invoked, C::~C () is
73 // invoked.
74 // After that B::~B () is invoked twice.
75 // At exit time D::~D () is invoked 4 times, then A::~A ().
76 const auto &[x, y, z, w] = foo (B {}, B {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
77 int c2 = baz (c, 23);
78
79 int
80 main ()
81 {
82 assert (a == 0);
83 }