Debian Codegraph @

gcc/testsuite/g++.dg/DRs/dr2867-4.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 foo (const int &, const int &)
51 {
52 assert (false);
53 }
54
55 inline void
56 bar ()
57 {
58 c = 1;
59 // First B::B () is invoked twice, then foo called, which invokes A::A ().
60 // e is reference bound to the A::A () constructed temporary.
61 // Then 4 times (in increasing I):
62 // C::C () is invoked, get is called, D::D () is invoked, C::~C () is
63 // invoked.
64 // After that B::~B () is invoked twice, then the following 2 user
65 // statements.
66 // At exit time D::~D () is invoked 4 times, then A::~A (), repeated 3
67 // times.
68 static const auto &[x, y, z, w] = foo (B {}, B {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
69 assert (c == 23); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
70 ++c;
71 }
72
73 template <int N>
74 inline void
75 baz ()
76 {
77 c = 1;
78 static const auto &[x, y, z, w] = foo (B {}, B {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
79 assert (c == 23); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
80 ++c;
81 }
82
83 template <typename T>
84 inline void
85 qux ()
86 {
87 c = 1;
88 static const auto &[x, y, z, w] = foo (T {}, T {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
89 assert (c == 23); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
90 ++c;
91 }
92
93 struct E {
94 ~E () { assert (a == 15); }
95 };
96
97 int
98 main ()
99 {
100 static E e;
101 bar ();
102 assert (c == 24);
103 baz <42> ();
104 assert (c == 24);
105 qux <B> ();
106 assert (c == 24);
107 assert (a == 0);
108 }