Skip to content

Commit 22caa92

Browse files
committed
book: adopt flowing-prose motivation style for structured bindings
Per feedback, switch the why-first treatment from bold question labels to flowing prose that answers the same questions naturally, keeping the italic 'since' marker and the use-case example. This is the blessed style to apply across the book.
1 parent cce628d commit 22caa92

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

book/en-us/02-usability.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ Foo foo2 {3, 4};
335335
336336
*(since C++17)*
337337
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.
338+
Functions frequently need to "return several values at once" — for example, a computed result together with a status flag. In traditional C++ this is not elegant: we either define a dedicated struct for it, 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.
339339
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:
340+
C++17's **structured bindings** exist precisely to remove that clumsiness: they 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:
341341
342342
```cpp
343343
#include <iostream>
@@ -354,9 +354,7 @@ int main() {
354354
}
355355
```
356356

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`:
357+
Compared with `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. This is especially handy when iterating an associative container: we can bind each key/value pair to meaningful names instead of writing `it->first` / `it->second`:
360358

361359
```cpp
362360
#include <iostream>

book/zh-cn/02-usability.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ Foo foo2 {3, 4};
280280
281281
*(C++17 引入)*
282282
283-
**为什么需要它?** 函数经常需要「一次返回多个值」——例如同时返回计算结果与一个状态标志。在传统 C++ 中,我们要么为此专门定义一个结构体,要么用 `std::tuple` 把它们打包返回;但把这些值再取出来却相当笨拙:用 `std::tie` 拆包时,必须**事先声明**好每一个变量,还要**准确知道**元组里有几个元素、各自是什么类型,稍有出入便会出错。
283+
函数常常需要「一次返回多个值」——例如同时返回计算结果与一个表示成败的状态。在传统 C++ 中,这件事并不优雅:我们要么为此专门定义一个结构体,要么用 `std::tuple` 把多个值打包返回,可一旦要把它们取出来就变得相当笨拙——用 `std::tie` 拆包时,必须事先声明好每一个变量,还要准确知道元组里有几个元素、各自是什么类型,稍有出入便会出错。
284284
285-
**它解决了什么问题?** C++17 的**结构化绑定 (structured bindings)** 让我们能用一行代码,把一个元组、`std::pair`、原生数组或公开数据成员的结构体「拆开」,并直接绑定到一组**有名字**的变量上,类型则交由编译器推导:
285+
C++17 的**结构化绑定 (structured bindings)** 正是为消除这种笨拙而生:它允许我们用一行代码,把一个元组、`std::pair`、原生数组或带有公开数据成员的结构体「拆开」,并直接绑定到一组有名字的变量上,类型则交由编译器推导:
286286
287287
```cpp
288288
#include <iostream>
@@ -299,9 +299,7 @@ int main() {
299299
}
300300
```
301301

302-
**相比 `std::tie` 好在哪里?** 结构化绑定无需事先声明变量、无需手写类型,并且不仅适用于元组,对原生数组和聚合结构体同样有效——这让「多返回值」的代码读起来就像其他现代语言一样自然。
303-
304-
**典型用例**:遍历关联容器时尤为优雅,可以把键值对直接绑定为有意义的名字,而不再写 `it->first` / `it->second`
302+
`std::tie` 相比,结构化绑定既不需要事先声明变量、也不需要手写类型,而且不仅适用于元组,对原生数组和聚合结构体同样有效。这一点在遍历关联容器时尤为实用:我们可以把键值对直接绑定为有意义的名字,而不必再写 `it->first` / `it->second`
305303

306304
```cpp
307305
#include <iostream>

0 commit comments

Comments
 (0)