Debian Codegraph @

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