233 lines · cpp
1// RUN: %clang_cc1 -std=c++17 -Wc++20-extensions -verify=expected %s2// RUN: %clang_cc1 -std=c++20 -Wpre-c++20-compat -verify=expected %s3// RUN: %clang_cc1 -std=c++20 -Wpre-c++20-compat -fexperimental-new-constant-interpreter -verify=expected %s4 5void use_from_own_init() {6 auto [a] = a; // expected-error {{binding 'a' cannot appear in the initializer of its own structured binding declaration}}7}8 9void num_elems() {10 struct A0 {} a0;11 int a1[1], a2[2];12 13 auto [] = a0; // expected-warning {{does not allow a structured binding group to be empty}}14 auto [v1] = a0; // expected-error {{type 'struct A0' binds to 0 elements, but 1 name was provided}}15 auto [] = a1; // expected-error {{type 'int[1]' binds to 1 element, but no names were provided}} expected-warning {{empty}}16 auto [v2] = a1;17 auto [v3, v4] = a1; // expected-error {{type 'int[1]' binds to 1 element, but 2 names were provided}}18 auto [] = a2; // expected-error {{type 'int[2]' binds to 2 elements, but no names were provided}} expected-warning {{empty}}19 auto [v5] = a2; // expected-error {{type 'int[2]' binds to 2 elements, but only 1 name was provided}}20 auto [v6, v7] = a2;21 auto [v8, v9, v10] = a2; // expected-error {{type 'int[2]' binds to 2 elements, but 3 names were provided}}22}23 24// As a Clang extension, _Complex can be decomposed.25float decompose_complex(_Complex float cf) {26 static _Complex float scf;27 auto &[sre, sim] = scf;28 // ok, this is references initialized by constant expressions all the way down29 static_assert(&sre == &__real scf);30 static_assert(&sim == &__imag scf);31 32 auto [re, im] = cf;33 return re*re + im*im;34}35 36// As a Clang extension, vector types can be decomposed.37typedef float vf3 __attribute__((ext_vector_type(3)));38float decompose_vector(vf3 v) {39 auto [x, y, z] = v;40 auto *p = &x; // expected-error {{address of vector element requested}}41 return x + y + z;42}43 44struct S { int a, b; };45constexpr int f(S s) {46 auto &[a, b] = s;47 return a * 10 + b;48}49static_assert(f({1, 2}) == 12);50 51constexpr bool g(S &&s) {52 auto &[a, b] = s;53 return &a == &s.a && &b == &s.b && &a != &b;54}55static_assert(g({1, 2}));56 57struct S1 {58 int a, b;59};60struct S2 {61 int a : 1; // expected-note 2{{bit-field is declared here}}62 int b;63};64 65auto [outer1, outer2] = S1{1, 2};66auto [outerbit1, outerbit2] = S1{1, 2}; // expected-note {{declared here}}67 68void enclosing() {69 struct S { int a = outer1; };70 auto [n] = S(); // expected-note 3{{'n' declared here}}71 72 struct Q {73 int f() { return n; } // expected-error {{reference to local binding 'n' declared in enclosing function 'enclosing'}}74 };75 76 (void)[&] { return n; }; // expected-warning {{C++20}}77 (void)[n] { return n; }; // expected-warning {{C++20}}78 79 static auto [m] = S(); // expected-note {{'m' declared here}} \80 // expected-warning {{C++20}}81 82 struct R { int f() { return m; } };83 (void) [&] { return m; };84 (void)[m]{}; // expected-error {{'m' cannot be captured because it does not have automatic storage duration}}85 86 (void)[outerbit1]{}; // expected-error {{'outerbit1' cannot be captured because it does not have automatic storage duration}}87 88 auto [bit, var] = S2{-1, 1}; // expected-note 2{{'bit' declared here}}89 90 (void)[&bit] { // expected-error {{non-const reference cannot bind to bit-field 'a'}} \91 // expected-warning {{C++20}}92 return bit;93 };94 95 union { // expected-note {{declared here}}96 int u;97 };98 99 (void)[&] { return bit + u; } // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}} \100 // expected-error {{non-const reference cannot bind to bit-field 'a'}} \101 // expected-warning {{C++20}}102 ();103}104 105void bitfield() {106 struct { int a : 3, : 4, b : 5; } a;107 auto &[x, y] = a;108 auto &[p, q, r] = a; // expected-error-re {{type 'struct (unnamed struct at {{.*}})' binds to 2 elements, but 3 names were provided}}109}110 111void for_range() {112 int x = 1;113 for (auto[a, b] : x) { // expected-error {{invalid range expression of type 'int'; no viable 'begin' function available}}114 a++;115 }116 117 int y[5];118 for (auto[c] : y) { // expected-error {{cannot bind non-class, non-array type 'int'}}119 c++;120 }121}122 123int error_recovery() {124 auto [foobar]; // expected-error {{requires an initializer}} \125 expected-note {{'foobar' declared here}}126 return foobar_; // expected-error {{undeclared identifier 'foobar_'}}127}128 129// PR32172130template <class T> void dependent_foreach(T t) {131 for (auto [a,b,c] : t)132 a,b,c;133}134 135struct PR37352 {136 int n;137 void f() { static auto [a] = *this; } // expected-warning {{C++20}}138};139 140namespace instantiate_template {141 142template <typename T1, typename T2>143struct pair {144 T1 a;145 T2 b;146};147 148const pair<int, int> &f1();149 150int f2() {151 const auto &[a, b] = f1();152 return a + b;153}154 155} // namespace instantiate_template156 157namespace lambdas {158 void f() {159 int n;160 auto [a] = // expected-error {{cannot bind lambda closure type}}161 [n] {}; // expected-note {{lambda expression}}162 }163 164 auto [] = []{}; // expected-warning {{ISO C++17 does not allow a structured binding group to be empty}}165 166 int g() {167 int n = 0;168 auto a = [=](auto &self) { // expected-note {{lambda expression}}169 auto &[capture] = self; // expected-error {{cannot bind lambda closure type}}170 ++capture;171 return n;172 };173 return a(a); // expected-note {{in instantiation of}}174 }175 176 int h() {177 auto x = [] {};178 struct A : decltype(x) {179 int n;180 };181 auto &&[r] = A{x, 0}; // OK (presumably), non-capturing lambda has no non-static data members182 return r;183 }184 185 int i() {186 int n;187 auto x = [n] {};188 struct A : decltype(x) {189 int n;190 };191 auto &&[r] = A{x, 0}; // expected-error-re {{cannot bind class type 'A': both it and its base class 'decltype(x)' (aka '(lambda {{.*}})') have non-static data members}}192 return r;193 }194 195 void j() {196 auto x = [] {};197 struct A : decltype(x) {};198 auto &&[] = A{x}; // expected-warning {{ISO C++17 does not allow a structured binding group to be empty}}199 }200}201 202namespace by_value_array_copy {203 struct explicit_copy {204 explicit_copy() = default; // expected-note {{candidate constructor not viable: requires 0 arguments, but 1 was provided}}205 explicit explicit_copy(const explicit_copy&) = default; // expected-note {{explicit constructor is not a candidate}}206 };207 208 constexpr int simple_array_elements() {209 int arr[2]{1, 2};210 211 auto [a1, a2] = arr;212 auto [b1, b2](arr);213 auto [c1, c2]{arr}; // GH31813214 215 arr[0] = 0;216 return arr[0] + a1 + a2 + b1 + b2 + c1 + c2;217 }218 static_assert(simple_array_elements() == 9);219 220 void explicit_copy_ctor_array_elements() {221 explicit_copy ec_arr[1];222 223 auto [a] = ec_arr; // expected-error {{no matching constructor for initialization of 'explicit_copy[1]'}}224 auto [b](ec_arr);225 auto [c]{ec_arr};226 227 // Test prvalue228 using T = explicit_copy[1];229 auto [d] = T{};230 }231 232} // namespace by_value_array_copy233