Debian Codegraph @

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