Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added structured binding by reference #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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