Debian Codegraph @

gcc/testsuite/g++.dg/DRs/dr2867-3.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 foo (const int &, const int &)
58 {
59 assert (false);
60 }
61
62 inline void
63 bar ()
64 {
65 c = 1;
66 static const auto &[x, y, z, w] = foo (B {}, B {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
67 assert (c == 11); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
68 ++c;
69 d = 1;
70 static const auto &[s, t, u] = foo (C {}, C {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
71 assert (d == 4); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
72 }
73
74 template <int N>
75 inline void
76 baz ()
77 {
78 c = 1;
79 static const auto &[x, y, z, w] = foo (B {}, B {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
80 assert (c == 11); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
81 ++c;
82 d = 1;
83 static const auto &[s, t, u] = foo (C {}, C {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
84 assert (d == 4); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
85 }
86
87 template <typename T, typename U>
88 inline void
89 qux ()
90 {
91 c = 1;
92 static const auto &[x, y, z, w] = foo (T {}, T {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
93 assert (c == 11); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
94 ++c;
95 d = 1;
96 static const auto &[s, t, u] = foo (U {}, U {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
97 assert (d == 4); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
98 }
99
100 inline void
101 corge ()
102 {
103 c = 1;
104 static auto [x, y, z, w] = foo (B {}, B {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
105 assert (c == 11); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
106 ++c;
107 d = 1;
108 static auto [s, t, u] = foo (C {}, C {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
109 assert (d == 4); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
110 }
111
112 template <int N>
113 inline void
114 garply ()
115 {
116 c = 1;
117 static auto [x, y, z, w] = foo (B {}, B {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
118 assert (c == 11); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
119 ++c;
120 d = 1;
121 static auto [s, t, u] = foo (C {}, C {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
122 assert (d == 4); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
123 }
124
125 template <typename T, typename U>
126 inline void
127 freddy ()
128 {
129 c = 1;
130 static auto [x, y, z, w] = foo (T {}, T {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
131 assert (c == 11); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
132 ++c;
133 d = 1;
134 static auto [s, t, u] = foo (U {}, U {}); // { dg-warning "structured bindings only available with" "" { target c++14_down } }
135 assert (d == 4); // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 }
136 }
137
138 struct E {
139 ~E () { assert (a == 6); }
140 };
141
142 int
143 main ()
144 {
145 static E e;
146 bar ();
147 assert (c == 12);
148 baz <0> ();
149 assert (c == 12);
150 qux <B, C> ();
151 assert (c == 12);
152 corge ();
153 assert (c == 12);
154 garply <42> ();
155 assert (c == 12);
156 freddy <B, C> ();
157 assert (c == 12);
158 assert (a == 0);
159 }