aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp26/decomp9.C
blob: 5629c4c9d573c45313667794d31ec3ea19087c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// P0963R3 - Structured binding declaration as a condition
// { dg-do compile { target c++11 } }
// { dg-options "" }

int a[4];
struct S { int i, j; };
struct T { int i, j, k; explicit operator bool () const noexcept; } t;
enum E { E0, E1 };
struct U { int i, j, k, l; operator E () const noexcept; } u;
int w;
union X { int i; long j; } x;

void
foo (const S &&s)
{
  if (auto [ i, j, k, l ] = a)			// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "array initializer for structured binding declaration in condition" "" { target *-*-* } .-1 }
  if (auto & [a, b, c] = "ht")			// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "array initializer for structured binding declaration in condition" "" { target *-*-* } .-1 }
  if (auto const & [i, j] = s)			// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "could not convert '<structured bindings>' from 'const S' to 'bool'" "" { target *-*-* } .-1 }
  if (auto const & [i, j, k] = t)		// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    w = i + j + k;
  else
    w = i - j * k;
  if (auto [i, j, k, l] = u)			// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;
  if (auto [i] = x)				// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "cannot decompose union type 'X'" "" { target *-*-* } .-1 }
						// { dg-error "could not convert '<structured bindings>' from 'X' to 'bool'" "" { target *-*-* } .-2 }
  if (auto [i, j, k] = s)			// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "3 names provided for structured binding" "" { target *-*-* } .-1 }
						// { dg-error "could not convert '<structured bindings>' from 'S' to 'bool'" "" { target *-*-* } .-2 }
  if (auto [i] = s)				// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "only 1 name provided for structured binding" "" { target *-*-* } .-1 }
						// { dg-error "could not convert '<structured bindings>' from 'S' to 'bool'" "" { target *-*-* } .-2 }
  switch (auto [a, b, c] = "ht")		// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    {						// { dg-error "array initializer for structured binding declaration in condition" "" { target *-*-* } .-1 }
    default:					// { dg-error "switch quantity not an integer" "" { target *-*-* } .-2 }
      break;
    }
  switch (auto const & [i, j] = s)		// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    {						// { dg-error "switch quantity not an integer" "" { target *-*-* } .-1 }
    case 1:
    default:
      break;
    }
  switch (auto const & [i, j, k] = t)		// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    {						// { dg-error "switch quantity not an integer" "" { target *-*-* } .-1 }
    case 1:
    default:
      break;
    }
  switch (auto [i, j, k, l] = u)		// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    {
    case E0:
      ++i; ++j;
      break;
    default:
      ++k; ++l;
      break;
    }
  if (static auto [i, j, k] = t)		// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "'static' invalid in condition" "" { target *-*-* } .-1 }
						// { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-2 }
  if (constexpr auto [i, j, k] = t)		// { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
    ;						// { dg-error "structured binding declaration cannot be 'constexpr'" "" { target *-*-* } .-1 }
}