Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ small = min({x, y, z, k}); // life is easy
```

## JavaScript like Destructuring using Structured Binding in C++

### Binding by value:
```cpp
pair<int, int> cur = {1, 2};
auto [x, y] = cur;
Expand All @@ -36,8 +38,22 @@ auto [x, y] = cur;
array<int, 3> arr = {1, 0, -1};
auto [a, b, c] = arr;
// a is now 1, b is now 0, c is now -1
a++;
// a is now 2. arr[0] is unaffected, remains 1.
```

### Binding by reference:
```cpp
array<int, 3> arr = {1, 0, -1};
auto& [a, b, c] = arr;
// a is now 1, b is now 0, c is now -1
a++;
// a is now 2, arr[0] is also 2.
```

**Note:** Structured binding cannot be done with vectors since vectors are dynamic.



----------------

Expand Down