You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Demonstrate the proposed motivation-driven rewrite style on one feature:
lead with why the reader needs it and the problem it solves, explain the
mechanism, contrast with the prior approach (std::tie), and show a
concrete use case (map iteration). Also trials a per-section 'since
C++NN' marker. This is a style sample for sign-off before applying the
pattern across the book.
Copy file name to clipboardExpand all lines: book/en-us/02-usability.md
+19-11Lines changed: 19 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -333,18 +333,11 @@ Foo foo2 {3, 4};
333
333
334
334
### Structured binding
335
335
336
-
Structured bindings provide functionality similar to the multiple return values
337
-
provided in other languages. In the chapter on containers,
338
-
we will learn that C++11 has added a `std::tuple` container for
339
-
constructing a tuple that encloses multiple return values. But the flaw
340
-
is that C++11/14 does not provide a simple way to get and define
341
-
the elements in the tuple from the tuple,
342
-
although we can unpack the tuple using `std::tie`
343
-
But we still have to be very clear about how many objects this tuple contains,
344
-
what type of each object is, very troublesome.
336
+
*(since C++17)*
345
337
346
-
C++17 completes this setting,
347
-
and the structured bindings let us write code like this:
338
+
**Why do we need it?** Functions frequently need to "return several values at once" — for example, a computed result together with a status flag. In traditional C++ we either define a dedicated struct for this, or pack the values into a `std::tuple` and return that; but getting the values back out is clumsy. Unpacking with `std::tie` forces us to **declare every variable in advance** and to **know exactly** how many elements the tuple holds and the type of each — and any mismatch is an error.
339
+
340
+
**What problem does it solve?** C++17's **structured bindings** let us, in a single line, "unpack" a tuple, a `std::pair`, a raw array, or a struct with public data members, and bind the pieces directly to a set of **named** variables, with the types deduced by the compiler:
348
341
349
342
```cpp
350
343
#include <iostream>
@@ -361,6 +354,21 @@ int main() {
361
354
}
362
355
```
363
356
357
+
**Why is this better than `std::tie`?** Structured bindings need no prior declaration and no spelled-out types, and they work not only on tuples but also on raw arrays and aggregate structs — making "multiple return values" read as naturally as in other modern languages.
358
+
359
+
**A typical use case**: iterating an associative container becomes especially clean, binding each key/value pair to meaningful names instead of writing `it->first` / `it->second`:
0 commit comments