Debian Codegraph @

gcc/testsuite/g++.dg/cpp26/decomp9.C

raw
1 // P0963R3 - Structured binding declaration as a condition
2 // { dg-do compile { target c++11 } }
3 // { dg-options "" }
4
5 int a[4];
6 struct S { int i, j; };
7 struct T { int i, j, k; explicit operator bool () const noexcept; } t;
8 enum E { E0, E1 };
9 struct U { int i, j, k, l; operator E () const noexcept; } u;
10 int w;
11 union X { int i; long j; } x;
12
13 void
14 foo (const S &&s)
15 {
16 if (auto [ i, j, k, l ] = a) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
17 ; // { dg-error "array initializer for structured binding declaration in condition" "" { target *-*-* } .-1 }
18 if (auto & [a, b, c] = "ht") // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
19 ; // { dg-error "array initializer for structured binding declaration in condition" "" { target *-*-* } .-1 }
20 if (auto const & [i, j] = s) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
21 ; // { dg-error "could not convert '<structured bindings>' from 'const S' to 'bool'" "" { target *-*-* } .-1 }
22 if (auto const & [i, j, k] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
23 w = i + j + k;
24 else
25 w = i - j * k;
26 if (auto [i, j, k, l] = u) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
27 ;
28 if (auto [i] = x) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
29 ; // { dg-error "cannot decompose union type 'X'" "" { target *-*-* } .-1 }
30 // { dg-error "could not convert '<structured bindings>' from 'X' to 'bool'" "" { target *-*-* } .-2 }
31 if (auto [i, j, k] = s) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
32 ; // { dg-error "3 names provided for structured binding" "" { target *-*-* } .-1 }
33 // { dg-error "could not convert '<structured bindings>' from 'S' to 'bool'" "" { target *-*-* } .-2 }
34 if (auto [i] = s) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
35 ; // { dg-error "only 1 name provided for structured binding" "" { target *-*-* } .-1 }
36 // { dg-error "could not convert '<structured bindings>' from 'S' to 'bool'" "" { target *-*-* } .-2 }
37 switch (auto [a, b, c] = "ht") // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
38 { // { dg-error "array initializer for structured binding declaration in condition" "" { target *-*-* } .-1 }
39 default: // { dg-error "switch quantity not an integer" "" { target *-*-* } .-2 }
40 break;
41 }
42 switch (auto const & [i, j] = s) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
43 { // { dg-error "switch quantity not an integer" "" { target *-*-* } .-1 }
44 case 1:
45 default:
46 break;
47 }
48 switch (auto const & [i, j, k] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
49 { // { dg-error "switch quantity not an integer" "" { target *-*-* } .-1 }
50 case 1:
51 default:
52 break;
53 }
54 switch (auto [i, j, k, l] = u) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
55 {
56 case E0:
57 ++i; ++j;
58 break;
59 default:
60 ++k; ++l;
61 break;
62 }
63 if (static auto [i, j, k] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
64 ; // { dg-error "'static' invalid in condition" "" { target *-*-* } .-1 }
65 // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-2 }
66 if (constexpr auto [i, j, k] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
67 ; // { dg-error "structured binding declaration cannot be 'constexpr'" "" { target *-*-* } .-1 }
68 }