• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++17 -verify %s
2 
use_from_own_init()3 void use_from_own_init() {
4   auto [a] = a; // expected-error {{binding 'a' cannot appear in the initializer of its own decomposition declaration}}
5 }
6 
7 // As a Clang extension, _Complex can be decomposed.
decompose_complex(_Complex float cf)8 float decompose_complex(_Complex float cf) {
9   static _Complex float scf;
10   auto &[sre, sim] = scf;
11   // ok, this is references initialized by constant expressions all the way down
12   static_assert(&sre == &__real scf);
13   static_assert(&sim == &__imag scf);
14 
15   auto [re, im] = cf;
16   return re*re + im*im;
17 }
18 
19 // As a Clang extension, vector types can be decomposed.
20 typedef float vf3 __attribute__((ext_vector_type(3)));
decompose_vector(vf3 v)21 float decompose_vector(vf3 v) {
22   auto [x, y, z] = v;
23   auto *p = &x; // expected-error {{address of vector element requested}}
24   return x + y + z;
25 }
26 
27 struct S { int a, b; };
f(S s)28 constexpr int f(S s) {
29   auto &[a, b] = s;
30   return a * 10 + b;
31 }
32 static_assert(f({1, 2}) == 12);
33 
g(S && s)34 constexpr bool g(S &&s) {
35   auto &[a, b] = s;
36   return &a == &s.a && &b == &s.b && &a != &b;
37 }
38 static_assert(g({1, 2}));
39 
40 auto [outer1, outer2] = S{1, 2};
enclosing()41 void enclosing() {
42   struct S { int a = outer1; };
43   auto [n] = S(); // expected-note 2{{'n' declared here}}
44 
45   struct Q { int f() { return n; } }; // expected-error {{reference to local binding 'n' declared in enclosing function}}
46   (void) [&] { return n; }; // expected-error {{reference to local binding 'n' declared in enclosing function}}
47   (void) [n] {}; // expected-error {{'n' in capture list does not name a variable}}
48 
49   static auto [m] = S(); // expected-warning {{extension}}
50   struct R { int f() { return m; } };
51   (void) [&] { return m; };
52   (void) [m] {}; // expected-error {{'m' in capture list does not name a variable}}
53 }
54 
bitfield()55 void bitfield() {
56   struct { int a : 3, : 4, b : 5; } a;
57   auto &[x, y] = a;
58   auto &[p, q, r] = a; // expected-error {{decomposes into 2 elements, but 3 names were provided}}
59 }
60 
for_range()61 void for_range() {
62   int x = 1;
63   for (auto[a, b] : x) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}}
64     a++;
65   }
66 
67   int y[5];
68   for (auto[c] : y) { // expected-error {{cannot decompose non-class, non-array type 'int'}}
69     c++;
70   }
71 }
72 
error_recovery()73 int error_recovery() {
74   auto [foobar]; // expected-error {{requires an initializer}}
75   return foobar_; // expected-error {{undeclared identifier 'foobar_'}}
76 }
77 
78 // PR32172
dependent_foreach(T t)79 template <class T> void dependent_foreach(T t) {
80   for (auto [a,b,c] : t)
81     a,b,c;
82 }
83 
84 struct PR37352 {
85   int n;
fPR3735286   void f() { static auto [a] = *this; } // expected-warning {{C++20 extension}}
87 };
88 
89 namespace instantiate_template {
90 
91 template <typename T1, typename T2>
92 struct pair {
93   T1 a;
94   T2 b;
95 };
96 
97 const pair<int, int> &f1();
98 
f2()99 int f2() {
100   const auto &[a, b] = f1();
101   return a + b;
102 }
103 
104 } // namespace instantiate_template
105 
106 namespace lambdas {
f()107   void f() {
108     int n;
109     auto [a] =  // expected-error {{cannot decompose lambda closure type}}
110         [n] {}; // expected-note {{lambda expression}}
111   }
112 
__anon0a30f7aa0702null113   auto [] = []{}; // expected-warning {{ISO C++17 does not allow a decomposition group to be empty}}
114 
g()115   int g() {
116     int n = 0;
117     auto a = [=](auto &self) { // expected-note {{lambda expression}}
118       auto &[capture] = self; // expected-error {{cannot decompose lambda closure type}}
119       ++capture;
120       return n;
121     };
122     return a(a); // expected-note {{in instantiation of}}
123   }
124 
h()125   int h() {
126     auto x = [] {};
127     struct A : decltype(x) {
128       int n;
129     };
130     auto &&[r] = A{x, 0}; // OK (presumably), non-capturing lambda has no non-static data members
131     return r;
132   }
133 
i()134   int i() {
135     int n;
136     auto x = [n] {};
137     struct A : decltype(x) {
138       int n;
139     };
140     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}}
141     return r;
142   }
143 
j()144   void j() {
145     auto x = [] {};
146     struct A : decltype(x) {};
147     auto &&[] = A{x}; // expected-warning {{ISO C++17 does not allow a decomposition group to be empty}}
148   }
149 }
150 
151 // FIXME: by-value array copies
152