Skip to content

docs(cpp11): add STL real-world case, exercise mapping, and tagged-union exercise for 16-generalized-unions#57

Merged
Sunrisepeak merged 6 commits into
mcpp-community:mainfrom
lczllx:docs/cpp11-16-generalized-unions-book
Jun 13, 2026
Merged

docs(cpp11): add STL real-world case, exercise mapping, and tagged-union exercise for 16-generalized-unions#57
Sunrisepeak merged 6 commits into
mcpp-community:mainfrom
lczllx:docs/cpp11-16-generalized-unions-book

Conversation

@lczllx

@lczllx lczllx commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

docs(cpp11): add STL real-world case, exercise mapping, and tagged-union exercise for 16-generalized-unions

Book chapter (zh + en):

  • add STL real-world case section — std::variant Variant_storage
    recursive union and std::any _Storage_t small-object optimization,
    both cited from vendored msvc-stl/
  • fill in exercise code section — exercise descriptions, file links
    and d2x checker command
  • fix empty Code link in header table

New exercise 2 — tagged discriminated union:

  • dslings/cpp11/16-generalized-unions-2.cpp (zh)
  • dslings/en/cpp11/16-generalized-unions-2.cpp (en)
  • solutions/cpp11/16-generalized-unions-2.cpp
  • register targets in dslings/cpp11, dslings/en/cpp11, solutions/cpp11

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

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.

lczllx added 2 commits June 11, 2026 21:50
…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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and std::any’s union-based type-erased storage (with source links into msvc-stl/).
  • Fill in the exercise mapping with links to dslings/cpp11/16-generalized-unions-{0,1}.cpp and 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.

Comment thread book/src/cpp11/16-generalized-unions.md Outdated
Comment thread book/src/cpp11/16-generalized-unions.md Outdated
lczllx and others added 2 commits June 12, 2026 08:41
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lczllx

lczllx commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

@Sunrisepeak 哥,练习部分我觉得可以加嵌套联合体,以及把联合体作为 unordered_map 的 key 的例子,配合自定义哈希来做区分,还是说要其他简单一点的。
另外想问下,PR 里允许用 --force 更新分支吗?

@Sunrisepeak

Copy link
Copy Markdown
Member

@Sunrisepeak 哥,练习部分我觉得可以加嵌套联合体,以及把联合体作为 unordered_map 的 key 的例子,配合自定义哈希来做区分,还是说要其他简单一点的。 另外想问下,PR 里允许用 --force 更新分支吗?

1.如果练习里面要加, 对应的章节 md 里也要加

  • 你可以从教学的视角, 最好是一类真实场景, 且能即传达场景用法, 而又不会过于复杂
    2.PR对应的分支是你fork的分支应该是可以force的, 但有commit记录没有问题 后面合入的时候会 squash

@lczllx

lczllx commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

哥,练习部分我觉得可以加嵌套联合体,以及把联合体作为 unordered_map 的 key 的例子,配合自定义哈希来做区分,还是说要其他简单一点的。另外想问下,PR 里允许用 --force 更新分支吗?

1.如果练习里面要加, 对应的章节 md 里也要加

  • 你可以从教学的视角,最好是一类真实场景,且能即传达场景用法, 而又不会过于复杂 2.PR 对应的分支是你fork的分支应该是可以force的, 但有commit记录没有问题 后面合入的时候会 squash

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.
@lczllx lczllx changed the title docs(cpp11): add STL real-world case and exercise mapping for 16-generalized-unions docs(cpp11): add STL real-world case, exercise mapping, and tagged-union exercise for 16-generalized-unions Jun 12, 2026
@lczllx

lczllx commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

哥,练习部分我觉得可以加嵌套联合体,以及把联合体作为 unordered_map 的 key 的例子,配合自定义哈希来做区分,还是说要其他简单一点的。另外想问下,PR 里允许用 --force 更新分支吗?

1.如果练习里面要加, 对应的章节 md 里也要加

  • 你可以从教学的视角,最好是一类真实场景,且能即传达场景用法,而又不会过于复杂 2.PR 对应的分支是你fork的分支应该是可以force的,但有commit记录没有问题 后面合入的时候会 squash

好的

练习 2 是带标签的鉴别联合体:enum tag + union + struct,跟踪活跃成员并按 tag 构造/析构/切换。直接对应书稿 STL 案例里 std::variant 的 Variant_storage 模式,把 练习 0 的静态规则和 练习 1 的手动生命周期串成一个真实场景。5 个填空。

@Sunrisepeak

Copy link
Copy Markdown
Member

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
@lczllx

lczllx commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

en 对应的 章节也要同步更新一下

加上了

@lczllx

lczllx commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

en 对应的 章节也要同步更新一下

加上了

en 对应的 章节也要同步更新一下

加上了

@Sunrisepeak 哥, 还有其他任务吗,这个项目或者其他项目,我都可以帮忙

@Sunrisepeak

Copy link
Copy Markdown
Member

en 对应的 章节也要同步更新一下

加上了

en 对应的 章节也要同步更新一下

加上了

@Sunrisepeak 哥, 还有其他任务吗,这个项目或者其他项目,我都可以帮忙

可以研究研究社区下面的这个项目

@Sunrisepeak Sunrisepeak merged commit 173f145 into mcpp-community:main Jun 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants