Skip to content

Commit 8fe948b

Browse files
feat: optimize vol2 passages, in ch05 and ch00 (#115)
1 parent de395f4 commit 8fe948b

9 files changed

Lines changed: 963 additions & 658 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// 13_init_statements.cpp
2+
// if/switch 初始化器:把变量的生存范围卡在它真正有用的那几行
3+
4+
#include <iostream>
5+
#include <map>
6+
#include <mutex>
7+
#include <string>
8+
9+
struct Command {
10+
int type;
11+
int arg;
12+
};
13+
14+
Command read_command(int raw) {
15+
return {raw / 10, raw % 10};
16+
}
17+
18+
int main() {
19+
std::cout << "=== if/switch 初始化器演示 ===\n\n";
20+
21+
std::map<int, std::string> cache = {{1, "one"}, {2, "two"}};
22+
23+
// 1. map 查找:迭代器被关在 if/else 内,出了块就不可见
24+
std::cout << "1. map 查找 (if init):\n";
25+
if (auto it = cache.find(2); it != cache.end()) {
26+
std::cout << " 找到: " << it->second << "\n";
27+
} else {
28+
std::cout << " 没找到\n";
29+
}
30+
std::cout << "\n";
31+
32+
// 2. insert + 结构化绑定:声明、判断、使用一行搞定
33+
std::cout << "2. insert + 结构化绑定:\n";
34+
if (auto [it, ok] = cache.insert({3, "three"}); ok) {
35+
std::cout << " 插入成功: " << it->second << "\n";
36+
} else {
37+
std::cout << " 已存在: " << it->second << "\n";
38+
}
39+
std::cout << "\n";
40+
41+
// 3. 锁守卫:锁的持有范围精确匹配条件块(CTAD 省去模板参数)
42+
std::cout << "3. 锁守卫:\n";
43+
std::mutex mtx;
44+
bool ready = true;
45+
if (std::lock_guard lock(mtx); ready) {
46+
std::cout << " 持锁状态下执行\n";
47+
}
48+
std::cout << "\n";
49+
50+
// 4. switch 初始化器:init 里准备数据,switch 里分发
51+
std::cout << "4. switch 初始化器:\n";
52+
for (int raw : {12, 25, 31}) {
53+
switch (auto cmd = read_command(raw); cmd.type) {
54+
case 1:
55+
std::cout << " cmd " << raw << " -> start(" << cmd.arg << ")\n";
56+
break;
57+
case 2:
58+
std::cout << " cmd " << raw << " -> stop(" << cmd.arg << ")\n";
59+
break;
60+
default:
61+
std::cout << " cmd " << raw << " -> unknown\n";
62+
break;
63+
}
64+
}
65+
66+
return 0;
67+
}

documents/en/vol2-modern-features/ch00-move-semantics/03-rvo-nrvo.md

Lines changed: 378 additions & 238 deletions
Large diffs are not rendered by default.

documents/en/vol2-modern-features/ch00-move-semantics/04-perfect-forwarding.md

Lines changed: 85 additions & 91 deletions
Large diffs are not rendered by default.

documents/en/vol2-modern-features/ch05-structured-bindings/01-structured-bindings.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,6 @@ Run the structured-binding examples online and see unpacking for `pair`, `tuple`
392392
allow-run
393393
/>
394394
395-
## Wrapping up
396-
397-
That's the full coverage of types structured binding handles: `pair`, `tuple`, native arrays, structs with public members, plus custom types that implement the tuple-like protocol. The semantics are entirely decided by the modifier in front of `auto`—`auto` copies, `auto&` references, `const auto&` is read-only, `auto&&` forwards.
398-
399-
What I actually use day to day is range-based for over a map (`for (const auto& [k, v] : m)`) and catching multi-return functions. Pair it with the if/switch initializers in the next chapter and your code shrinks another size.
400-
401395
## References
402396
403397
- [cppreference: Structured binding declaration](https://en.cppreference.com/w/cpp/language/structured_binding)

0 commit comments

Comments
 (0)