Debian Codegraph @

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