1 // RUN: %clang_cc1 -std=c++17 -verify %s
3 void use_from_own_init() {
4 auto [a
] = a
; // expected-error {{binding 'a' cannot appear in the initializer of its own decomposition declaration}}
11 auto [] = a0
; // expected-warning {{does not allow a decomposition group to be empty}}
12 auto [v1
] = a0
; // expected-error {{type 'struct A0' decomposes into 0 elements, but 1 name was provided}}
13 auto [] = a1
; // expected-error {{type 'int[1]' decomposes into 1 element, but no names were provided}} expected-warning {{empty}}
15 auto [v3
, v4
] = a1
; // expected-error {{type 'int[1]' decomposes into 1 element, but 2 names were provided}}
16 auto [] = a2
; // expected-error {{type 'int[2]' decomposes into 2 elements, but no names were provided}} expected-warning {{empty}}
17 auto [v5
] = a2
; // expected-error {{type 'int[2]' decomposes into 2 elements, but only 1 name was provided}}
19 auto [v8
, v9
, v10
] = a2
; // expected-error {{type 'int[2]' decomposes into 2 elements, but 3 names were provided}}
22 // As a Clang extension, _Complex can be decomposed.
23 float decompose_complex(_Complex
float cf
) {
24 static _Complex
float scf
;
25 auto &[sre
, sim
] = scf
;
26 // ok, this is references initialized by constant expressions all the way down
27 static_assert(&sre
== &__real scf
);
28 static_assert(&sim
== &__imag scf
);
34 // As a Clang extension, vector types can be decomposed.
35 typedef float vf3
__attribute__((ext_vector_type(3)));
36 float decompose_vector(vf3 v
) {
38 auto *p
= &x
; // expected-error {{address of vector element requested}}
42 struct S
{ int a
, b
; };
43 constexpr int f(S s
) {
47 static_assert(f({1, 2}) == 12);
49 constexpr bool g(S
&&s
) {
51 return &a
== &s
.a
&& &b
== &s
.b
&& &a
!= &b
;
53 static_assert(g({1, 2}));
55 auto [outer1
, outer2
] = S
{1, 2};
57 struct S
{ int a
= outer1
; };
58 auto [n
] = S(); // expected-note 2{{'n' declared here}}
60 struct Q
{ int f() { return n
; } }; // expected-error {{reference to local binding 'n' declared in enclosing function}}
61 (void) [&] { return n
; }; // expected-error {{reference to local binding 'n' declared in enclosing function}}
62 (void) [n
] {}; // expected-error {{'n' in capture list does not name a variable}}
64 static auto [m
] = S(); // expected-warning {{extension}}
65 struct R
{ int f() { return m
; } };
66 (void) [&] { return m
; };
67 (void) [m
] {}; // expected-error {{'m' in capture list does not name a variable}}
71 struct { int a
: 3, : 4, b
: 5; } a
;
73 auto &[p
, q
, r
] = a
; // expected-error-re {{type 'struct (unnamed struct at {{.*}})' decomposes into 2 elements, but 3 names were provided}}
78 for (auto[a
, b
] : x
) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}}
83 for (auto[c
] : y
) { // expected-error {{cannot decompose non-class, non-array type 'int'}}
88 int error_recovery() {
89 auto [foobar
]; // expected-error {{requires an initializer}}
90 return foobar_
; // expected-error {{undeclared identifier 'foobar_'}}
94 template <class T
> void dependent_foreach(T t
) {
95 for (auto [a
,b
,c
] : t
)
101 void f() { static auto [a
] = *this; } // expected-warning {{C++20 extension}}
104 namespace instantiate_template
{
106 template <typename T1
, typename T2
>
112 const pair
<int, int> &f1();
115 const auto &[a
, b
] = f1();
119 } // namespace instantiate_template
124 auto [a
] = // expected-error {{cannot decompose lambda closure type}}
125 [n
] {}; // expected-note {{lambda expression}}
128 auto [] = []{}; // expected-warning {{ISO C++17 does not allow a decomposition group to be empty}}
132 auto a
= [=](auto &self
) { // expected-note {{lambda expression}}
133 auto &[capture
] = self
; // expected-error {{cannot decompose lambda closure type}}
137 return a(a
); // expected-note {{in instantiation of}}
142 struct A
: decltype(x
) {
145 auto &&[r
] = A
{x
, 0}; // OK (presumably), non-capturing lambda has no non-static data members
152 struct A
: decltype(x
) {
155 auto &&[r
] = A
{x
, 0}; // expected-error-re {{cannot decompose class type 'A': both it and its base class 'decltype(x)' (aka '(lambda {{.*}})') have non-static data members}}
161 struct A
: decltype(x
) {};
162 auto &&[] = A
{x
}; // expected-warning {{ISO C++17 does not allow a decomposition group to be empty}}
166 // FIXME: by-value array copies