| 1 |
// CWG2867 - Order of initialization for structured bindings. |
| 2 |
// { dg-additional-options -fmodule-header } |
| 3 |
// { dg-module-cmi {} } |
| 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 |
extern 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 |
inline 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 |
inline int |
| 57 |
bar (int &x, int y) |
| 58 |
{ |
| 59 |
x = y; |
| 60 |
return y; |
| 61 |
} |
| 62 |
|
| 63 |
inline 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 |
namespace { |
| 75 |
E e; |
| 76 |
int c1 = bar (c, 1); |
| 77 |
const auto &[x, y, z, w] = foo (B {}, B {}); |
| 78 |
int c2 = baz (c, 11); |
| 79 |
int d1 = bar (d, 1); |
| 80 |
const auto &[s, t, u] = foo (C {}, C {}); |
| 81 |
int d2 = baz (d, 4); |
| 82 |
int c3 = bar (c, 1); |
| 83 |
auto [x2, y2, z2, w2] = foo (B {}, B {}); |
| 84 |
int c4 = baz (c, 11); |
| 85 |
int d3 = bar (d, 1); |
| 86 |
auto [s2, t2, u2] = foo (C {}, C {}); |
| 87 |
int d4 = baz (d, 4); |
| 88 |
} |