1// Structured binding in C++ can bind identifiers to subobjects of an object.
2//
3// There are three cases we need to test:
4// 1) arrays
5// 2) tuples like objects
6// 3) non-static data members
7//
8// They can also bind by copy, reference or rvalue reference.
9
10#include <tuple>
11
12struct A {
13 int x;
14 int y;
15};
16
17// We want to cover a mix of types and also different sizes to make sure we
18// hande the offsets correctly.
19struct MixedTypesAndSizesStruct {
20 A a;
21 char b1;
22 char b2;
23 short b3;
24 int b4;
25 char b5;
26};
27
28int main() {
29 MixedTypesAndSizesStruct b{.a: {.x: 20, .y: 30}, .b1: 'a', .b2: 'b', .b3: 50, .b4: 60, .b5: 'c'};
30
31 auto [a1, b1, c1, d1, e1, f1] = b;
32 auto &[a2, b2, c2, d2, e2, f2] = b;
33 auto &&[a3, b3, c3, d3, e3, f3] =
34 MixedTypesAndSizesStruct{.a: {.x: 20, .y: 30}, .b1: 'a', .b2: 'b', .b3: 50, .b4: 60, .b5: 'c'};
35
36 // Array with different sized types
37 char carr[]{'a', 'b', 'c'};
38 short sarr[]{11, 12, 13};
39 int iarr[]{22, 33, 44};
40
41 auto [carr_copy1, carr_copy2, carr_copy3] = carr;
42 auto [sarr_copy1, sarr_copy2, sarr_copy3] = sarr;
43 auto [iarr_copy1, iarr_copy2, iarr_copy3] = iarr;
44
45 auto &[carr_ref1, carr_ref2, carr_ref3] = carr;
46 auto &[sarr_ref1, sarr_ref2, sarr_ref3] = sarr;
47 auto &[iarr_ref1, iarr_ref2, iarr_ref3] = iarr;
48
49 auto &&[carr_rref1, carr_rref2, carr_rref3] = carr;
50 auto &&[sarr_rref1, sarr_rref2, sarr_rref3] = sarr;
51 auto &&[iarr_rref1, iarr_rref2, iarr_rref3] = iarr;
52
53 float x{4.0};
54 char y{'z'};
55 int z{10};
56
57 std::tuple<float, char, int> tpl(x, y, z);
58 auto [tx1, ty1, tz1] = tpl;
59 auto &[tx2, ty2, tz2] = tpl;
60
61 return a1.x + b1 + c1 + d1 + e1 + f1 + a2.y + b2 + c2 + d2 + e2 + f2 + a3.x +
62 b3 + c3 + d3 + e3 + f3 + carr_copy1 + carr_copy2 + carr_copy3 +
63 sarr_copy1 + sarr_copy2 + sarr_copy3 + iarr_copy1 + iarr_copy2 +
64 iarr_copy3 + carr_ref1 + carr_ref2 + carr_ref3 + sarr_ref1 +
65 sarr_ref2 + sarr_ref3 + iarr_ref1 + iarr_ref2 + iarr_ref3 +
66 carr_rref1 + carr_rref2 + carr_rref3 + sarr_rref1 + sarr_rref2 +
67 sarr_rref3 + iarr_rref1 + iarr_rref2 + iarr_rref3 + tx1 + ty1 + tz1 +
68 tx2 + ty2 + tz2; // break here
69}
70

source code of lldb/test/API/lang/cpp/structured-binding/main.cpp