| 1 |
// P0963R3 - Structured binding declaration as a condition |
| 2 |
// { dg-do run { target c++11 } } |
| 3 |
// { dg-options "" } |
| 4 |
|
| 5 |
namespace std { |
| 6 |
template<typename T> struct tuple_size; |
| 7 |
template<int, typename> struct tuple_element; |
| 8 |
} |
| 9 |
|
| 10 |
struct S { |
| 11 |
int a, b; |
| 12 |
explicit operator bool () const noexcept { return a == b; } |
| 13 |
}; |
| 14 |
|
| 15 |
struct T { |
| 16 |
int a, b, c; |
| 17 |
static int d; |
| 18 |
explicit operator bool () const noexcept { d = 42; return a == b; } |
| 19 |
template <int I> int &get () { if (d != 42) __builtin_abort (); return I ? a : b; } |
| 20 |
}; |
| 21 |
int T::d = 0; |
| 22 |
|
| 23 |
template<> struct std::tuple_size<T> { static const int value = 2; }; |
| 24 |
template<int I> struct std::tuple_element<I,T> { using type = int; }; |
| 25 |
|
| 26 |
void |
| 27 |
foo (T t) |
| 28 |
{ |
| 29 |
if (auto [ i, j ] = S { 1, 1 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 30 |
{ |
| 31 |
if (i != 1 || j != 1) |
| 32 |
__builtin_abort (); |
| 33 |
} |
| 34 |
else |
| 35 |
{ |
| 36 |
++i; |
| 37 |
++j; |
| 38 |
__builtin_abort (); |
| 39 |
} |
| 40 |
T::d = 0; |
| 41 |
if (int m = 78; auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 42 |
{ // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 } |
| 43 |
if (i != 42 || j != 42 || T::d != 42 || m != 78) |
| 44 |
__builtin_abort (); |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
++i; |
| 49 |
++j; |
| 50 |
++m; |
| 51 |
__builtin_abort (); |
| 52 |
} |
| 53 |
if (auto m = 15; auto [ i, j ] = S { -1, 1 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 54 |
{ // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 } |
| 55 |
++i; |
| 56 |
++j; |
| 57 |
++m; |
| 58 |
__builtin_abort (); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
if (i != -1 || j != 1 || m != 15) |
| 63 |
__builtin_abort (); |
| 64 |
} |
| 65 |
t.a = -42; |
| 66 |
T::d = 0; |
| 67 |
if (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 68 |
{ |
| 69 |
++i; |
| 70 |
++j; |
| 71 |
__builtin_abort (); |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
if (i != 42 || j != -42 || T::d != 42) |
| 76 |
__builtin_abort (); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
bar (T t) |
| 82 |
{ |
| 83 |
int cnt = 0; |
| 84 |
while (auto [ i, j ] = S { 7, 7 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 85 |
{ |
| 86 |
if (i != 7 || j != 7) |
| 87 |
__builtin_abort (); |
| 88 |
if (++cnt == 5) |
| 89 |
break; |
| 90 |
} |
| 91 |
if (cnt != 5) |
| 92 |
__builtin_abort (); |
| 93 |
T::d = 0; |
| 94 |
while (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 95 |
{ |
| 96 |
if (i != 31 || j != 31 || T::d != 42) |
| 97 |
__builtin_abort (); |
| 98 |
T::d = 0; |
| 99 |
if (++cnt == 10) |
| 100 |
break; |
| 101 |
} |
| 102 |
if (cnt != 10) |
| 103 |
__builtin_abort (); |
| 104 |
while (auto [ i, j ] = S { 7, -7 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 105 |
{ |
| 106 |
++i; |
| 107 |
++j; |
| 108 |
__builtin_abort (); |
| 109 |
} |
| 110 |
t.a = -31; |
| 111 |
T::d = 0; |
| 112 |
while (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 113 |
{ |
| 114 |
++i; |
| 115 |
++j; |
| 116 |
__builtin_abort (); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
baz (T t) |
| 122 |
{ |
| 123 |
int cntc = 0; |
| 124 |
for (int cnt = 0; auto [ i, j ] = S { 12, 12 }; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 125 |
{ |
| 126 |
if (i != 12 || j != 12) |
| 127 |
__builtin_abort (); |
| 128 |
cntc = cnt; |
| 129 |
if (cnt == 5) |
| 130 |
break; |
| 131 |
} |
| 132 |
if (cntc != 5) |
| 133 |
__builtin_abort (); |
| 134 |
T::d = 0; |
| 135 |
for (int cnt = 5; auto & [ i, j ] = t; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 136 |
{ |
| 137 |
if (i != -15 || j != -15 || T::d != 42) |
| 138 |
__builtin_abort (); |
| 139 |
T::d = 0; |
| 140 |
cntc = cnt; |
| 141 |
if (cnt == 10) |
| 142 |
break; |
| 143 |
} |
| 144 |
if (cntc != 10) |
| 145 |
__builtin_abort (); |
| 146 |
for (int cnt = 0; auto [ i, j ] = S { -27, 27 }; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 147 |
{ |
| 148 |
++i; |
| 149 |
++j; |
| 150 |
__builtin_abort (); |
| 151 |
} |
| 152 |
t.a = 15; |
| 153 |
T::d = 0; |
| 154 |
for (int cnt = 0; auto & [ i, j ] = t; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 155 |
{ |
| 156 |
++i; |
| 157 |
++j; |
| 158 |
__builtin_abort (); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
int |
| 163 |
main () |
| 164 |
{ |
| 165 |
foo ({ 42, 42, 0 }); |
| 166 |
bar ({ 31, 31, 7 }); |
| 167 |
baz ({ -15, -15, 6 }); |
| 168 |
} |