| 1 | // RUN: %clang_cc1 -std=c++1z -verify %s |
| 2 | |
| 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. |
| 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))); |
| 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; }; |
| 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 | |
| 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 | void enclosing() { |
| 41 | struct S { int a; }; |
| 42 | auto [n] = S(); // expected-note 2{{'n' declared here}} |
| 43 | |
| 44 | struct Q { int f() { return n; } }; // expected-error {{reference to local binding 'n' declared in enclosing function}} |
| 45 | // FIXME: This is probably supposed to be valid, but we do not have clear rules on how it's supposed to work. |
| 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 | |
| 50 | void bitfield() { |
| 51 | struct { int a : 3, : 4, b : 5; } a; |
| 52 | auto &[x, y] = a; |
| 53 | auto &[p, q, r] = a; // expected-error {{decomposes into 2 elements, but 3 names were provided}} |
| 54 | } |
| 55 | |
| 56 | void for_range() { |
| 57 | int x = 1; |
| 58 | for (auto[a, b] : x) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}} |
| 59 | a++; |
| 60 | } |
| 61 | |
| 62 | int y[5]; |
| 63 | for (auto[c] : y) { // expected-error {{cannot decompose non-class, non-array type 'int'}} |
| 64 | c++; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | int error_recovery() { |
| 69 | auto [foobar]; // expected-error {{requires an initializer}} |
| 70 | return foobar_; // expected-error {{undeclared identifier 'foobar_'}} |
| 71 | } |
| 72 | |
| 73 | // PR32172 |
| 74 | template <class T> void dependent_foreach(T t) { |
| 75 | for (auto [a,b,c] : t) |
| 76 | a,b,c; |
| 77 | } |
| 78 | |
| 79 | struct PR37352 { |
| 80 | int n; |
| 81 | void f() { static auto [a] = *this; } // expected-error {{cannot be declared 'static'}} |
| 82 | }; |
| 83 | |
| 84 | namespace instantiate_template { |
| 85 | |
| 86 | template <typename T1, typename T2> |
| 87 | struct pair { |
| 88 | T1 a; |
| 89 | T2 b; |
| 90 | }; |
| 91 | |
| 92 | const pair<int, int> &f1(); |
| 93 | |
| 94 | int f2() { |
| 95 | const auto &[a, b] = f1(); |
| 96 | return a + b; |
| 97 | } |
| 98 | |
| 99 | } // namespace instantiate_template |
| 100 | |
| 101 | // FIXME: by-value array copies |
| 102 | |