| 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 |
operator int () const noexcept { return a * 2; } |
| 13 |
}; |
| 14 |
|
| 15 |
struct T { |
| 16 |
int a, b, c; |
| 17 |
static int d; |
| 18 |
operator long long () const noexcept { d = 42; return a * 4; } |
| 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 |
template <typename S, typename T> |
| 27 |
void |
| 28 |
foo (T t) |
| 29 |
{ |
| 30 |
switch (auto [ i, j ] = S { 53, 62 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 31 |
{ |
| 32 |
case 2 * 53: |
| 33 |
if (i != 53 || j != 62) |
| 34 |
__builtin_abort (); |
| 35 |
break; |
| 36 |
default: |
| 37 |
__builtin_abort (); |
| 38 |
} |
| 39 |
T::d = 0; |
| 40 |
switch (int m = 78; auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 41 |
{ // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 } |
| 42 |
case 4 * 42LL: |
| 43 |
if (i != 43 || j != 42 || T::d != 42 || m != 78) |
| 44 |
__builtin_abort (); |
| 45 |
break; |
| 46 |
default: |
| 47 |
break; |
| 48 |
} |
| 49 |
switch (auto m = 15; auto [ i, j ] = S { -1, 1 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 50 |
{ // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 } |
| 51 |
case 2 * -1: |
| 52 |
if (i != -1 || j != 1 || m != 15) |
| 53 |
__builtin_abort (); |
| 54 |
break; |
| 55 |
default: |
| 56 |
__builtin_abort (); |
| 57 |
} |
| 58 |
t.a = -42; |
| 59 |
T::d = 0; |
| 60 |
switch (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } |
| 61 |
{ |
| 62 |
case 4LL * -42: |
| 63 |
if (i != 43 || j != -42 || T::d != 42) |
| 64 |
__builtin_abort (); |
| 65 |
break; |
| 66 |
default: |
| 67 |
__builtin_abort (); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
int |
| 72 |
main () |
| 73 |
{ |
| 74 |
foo<S, T> ({ 42, 43, 0 }); |
| 75 |
} |