docs(cpp11): add STL real-world case, exercise mapping, and tagged-union exercise for 16-generalized-unions#57
Conversation
…ralized-unions - add section 二 真实案例 — include std::variant _Variant_storage_ and std::any _Storage_t examples, both cited from vendored msvc-stl/ - fill in 四 练习代码 — exercise descriptions, file links and d2x checker command - fix empty Code link in header table Case selection notes: Picked _Variant_storage_ over std::optional _Optional_destruct_base because the variant recursive union demonstrates both key C++11 generalized-union capabilities — non-trivial members and manual destructor management — while the optional pattern is effectively a special case of variant. std::any adds a second typical use: union as type-erased storage. Both are directly traceable in the vendored msvc-stl/, consistent with how ch00 cites xutility.
There was a problem hiding this comment.
Pull request overview
This PR updates the C++11 “广义联合体” chapter to add real-world STL examples (from the vendored MSVC STL) and to populate the exercise section with concrete links and the corresponding d2x checker command.
Changes:
- Add a new “真实案例” section illustrating generalized unions via
std::variant’s recursive union storage andstd::any’s union-based type-erased storage (with source links intomsvc-stl/). - Fill in the exercise mapping with links to
dslings/cpp11/16-generalized-unions-{0,1}.cppand add the auto-checker command. - Fix the previously empty “Code/练习代码” link in the chapter header table.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…xcept, fix 类型安全 wording
|
@Sunrisepeak 哥,练习部分我觉得可以加嵌套联合体,以及把联合体作为 unordered_map 的 key 的例子,配合自定义哈希来做区分,还是说要其他简单一点的。 |
1.如果练习里面要加, 对应的章节 md 里也要加
|
ok |
Add exercise 2 — tagged/discriminated union combining enum tag + union to implement a simplified std::variant-like Value type. Exercise progression: 0. union default member initialization (static rule) 1. non-trivial type + placement new + manual destructor 2. enum tag + union → type-safe discriminated union Design rationale: ex 2 ties ex 0 and ex 1 together by adding a tag enum to track the active member — the exact pattern used by std::variant's internal _Variant_storage_, directly echoing the STL real-world case in the chapter. Learners construct/destruct/switch members based on tag, covering all three C++11 generalized-union capabilities in one practical exercise.
练习 2 是带标签的鉴别联合体:enum tag + union + struct,跟踪活跃成员并按 tag 构造/析构/切换。直接对应书稿 STL 案例里 std::variant 的 Variant_storage 模式,把 练习 0 的静态规则和 练习 1 的手动生命周期串成一个真实场景。5 个填空。 |
|
en 对应的 章节也要同步更新一下 |
Apply the same updates to the English book chapter: - add section II Real-World Case (msvc-stl variant + any examples) - fill in section IV Exercise Code (exercise links + d2x checker command) - fix empty Code link in header table - add ex2 tagged-union exercise link
加上了 |
@Sunrisepeak 哥, 还有其他任务吗,这个项目或者其他项目,我都可以帮忙 |
docs(cpp11): add STL real-world case, exercise mapping, and tagged-union exercise for 16-generalized-unions
Book chapter (zh + en):
recursive union and std::any _Storage_t small-object optimization,
both cited from vendored msvc-stl/
and d2x checker command
New exercise 2 — tagged discriminated union:
Exercise progression:
0. union default member initialization (static rule)
Case selection notes:
Picked Variant_storage over std::optional _Optional_destruct_base
because the variant recursive union demonstrates both key C++11
generalized-union capabilities — non-trivial members and manual
destructor management — while the optional pattern is effectively
a special case of variant. std::any adds a second typical use:
union as type-erased storage. Both are directly traceable in the
vendored msvc-stl/, consistent with how ch00 cites xutility.
ex 2 ties ex 0 and ex 1 together by adding a tag enum to track
the active member — the exact pattern used by std::variant internal
Variant_storage, directly echoing the STL real-world case in the
chapter.