docs(cpp14): add 00-generic-lambdas book chapter, exercises, solutions, and build wiring#71
Open
lczllx wants to merge 4 commits into
Open
docs(cpp14): add 00-generic-lambdas book chapter, exercises, solutions, and build wiring#71lczllx wants to merge 4 commits into
lczllx wants to merge 4 commits into
Conversation
Add the first C++14 chapter covering generic lambdas — lambda parameters
using auto, which the compiler expands into a functor with a templated
operator().
Book structure (zh + en):
- 一/II: Basic usage — identity, multi-param, STL algorithms, captures,
returning lambda (factory pattern)
- 二/II: Notes — closure type uniqueness, perfect forwarding, non-variadic
- 三/III: Exercise topics and d2x checker command
- 四/IV: External resources
Build wiring:
- register cpp14 in dslings/xmake.lua, dslings/en/xmake.lua,
solutions/xmake.lua
- add cpp14 entry to zh/en SUMMARY.md and book/README.md
- remove stale dslings/cpp14/README.md TODO placeholder
Files: book/src/cpp14/, book/en/src/cpp14/, book/src/SUMMARY.md,
book/en/src/SUMMARY.md, book/README.md,
dslings/xmake.lua, dslings/en/xmake.lua, solutions/xmake.lua,
dslings/cpp14/README.md (deleted)
Exercise progression:
0. Basic generic lambda — auto parameter identity/comparison/type-size
exercises. Learner fills auto param type, return expression, and
sizeof argument. 4 D2X_YOUR_ANSWER.
1. Generic lambda with STL algorithms — same lambda reused across
vector<int> and vector<double> for sort/find_if, plus a factory
pattern returning a lambda. 5 D2X_YOUR_ANSWER.
Design rationale:
Generic lambdas have three layers worth teaching independently:
(1) syntax — auto in lambda parameters generates a templated operator();
(2) reuse — a single lambda serves multiple container types, the
primary motivation for the feature;
(3) interaction — generic lambda returns + captures work naturally
without extra syntax.
ex 0 isolates layer (1). identity forces the learner to write auto
in a parameter position. greater verifies the return expression is
type-checked per instantiation. get_type_size confirms that sizeof
on a generic parameter works correctly, reinforcing the "compiler
stamps out per-type copies" mental model. Everything is self-contained
and runnable without headers beyond <string>.
ex 1 layers (2) and (3) on top. The desc lambda applied to both
vector<int> and vector<double> makes the payoff tangible — contrast
with the C++11 approach of duplicating the comparator. The find_if
exercise adds a capture without complicating the generic parameter.
The make_multiplier factory previews generic-lambda-returning-lambda,
which is a pattern that only becomes practical with C++14 return type
deduction.
We deliberately limited this chapter to 2 exercises. A third exercise
covering perfect forwarding + auto&& + decltype(auto) is better placed
in the decltype(auto) chapter where the forwarding semantics are the
primary topic, not a side note.
Files: dslings/cpp14/00-generic-lambdas-{0,1}.cpp (zh),
dslings/en/cpp14/00-generic-lambdas-{0,1}.cpp (en),
dslings/cpp14/xmake.lua, dslings/en/cpp14/xmake.lua,
solutions/cpp14/00-generic-lambdas-{0,1}.cpp,
solutions/cpp14/xmake.lua
Sunrisepeak
requested changes
Jun 16, 2026
…c-lambdas - add transparent functor real-world case (std::greater<>) cited from vendored msvc-stl/stl/inc/functional - add collapsible d2x setup block in exercise section - sync en book chapter
Contributor
There was a problem hiding this comment.
Pull request overview
Adds the first C++14 chapter (generic lambdas) with accompanying exercises, reference solutions, and xmake build wiring so the new material can be built/checked alongside existing chapters.
Changes:
- Added new C++14 “00 - generic lambdas” chapter in both zh/en books and registered it in both SUMMARY.md files.
- Introduced two new exercise files (zh + en) plus corresponding reference solutions.
- Wired cpp14 targets into existing xmake build structure for dslings and solutions; removed the stale cpp14 README placeholder.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| solutions/xmake.lua | Includes cpp14 solutions build script. |
| solutions/cpp14/xmake.lua | Defines xmake targets for cpp14 reference solution binaries. |
| solutions/cpp14/00-generic-lambdas-0.cpp | Reference solution for exercise 0. |
| solutions/cpp14/00-generic-lambdas-1.cpp | Reference solution for exercise 1. |
| dslings/xmake.lua | Includes cpp14 exercises in zh build. |
| dslings/en/xmake.lua | Includes cpp14 exercises in en build. |
| dslings/cpp14/xmake.lua | Defines zh cpp14 exercise targets. |
| dslings/en/cpp14/xmake.lua | Defines en cpp14 exercise targets. |
| dslings/cpp14/README.md | Removes the old TODO placeholder file. |
| dslings/cpp14/00-generic-lambdas-0.cpp | New zh exercise 0 scaffold with D2X placeholders. |
| dslings/cpp14/00-generic-lambdas-1.cpp | New zh exercise 1 scaffold with D2X placeholders. |
| dslings/en/cpp14/00-generic-lambdas-0.cpp | New en exercise 0 scaffold with D2X placeholders. |
| dslings/en/cpp14/00-generic-lambdas-1.cpp | New en exercise 1 scaffold with D2X placeholders. |
| book/src/SUMMARY.md | Registers the new zh cpp14 chapter in the book TOC. |
| book/src/cpp14/00-generic-lambdas.md | Adds the zh generic lambdas chapter content. |
| book/en/src/SUMMARY.md | Registers the new en cpp14 chapter in the book TOC. |
| book/en/src/cpp14/00-generic-lambdas.md | Adds the en generic lambdas chapter content. |
| book/README.md | Adds cpp14 cppreference links to the book README. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+187
to
+189
| ### 泛型 lambda 不是 variadic | ||
|
|
||
| 泛型 lambda 的参数个数仍然是固定的 — `[](auto a, auto b)` 接受恰好两个参数。如果要变参, 仍然需要可变参数模板 (C++20 后才支持 lambda 中使用 `...` 参数包) |
- fix C++11 example using auto (C++14 feature) in EN Why-introduced - fix variadic claim: generic lambdas can be variadic in C++14 - fix STL case: use greater<void> from xutility instead of greater_equal - fix is_transparent description: it signals associative containers, not sort algorithms
bd74a99 to
de16c08
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
docs(cpp14): add 00-generic-lambdas book chapter, exercises, solutions, and build wiring
Add the first C++14 chapter covering generic lambdas.
Book chapter (zh + en):
captures, returning lambda (factory pattern)
non-variadic
Exercise progression:
0. Basic generic lambda — auto parameter identity/comparison/type-size
exercises. Learner fills auto param type, return expression, and
sizeof argument. 4 D2X_YOUR_ANSWER.
vector and vector for sort/find_if, plus a factory
pattern returning a lambda. 5 D2X_YOUR_ANSWER.
Design rationale:
Generic lambdas have three layers worth teaching independently:
(1) syntax — auto in lambda parameters generates a templated operator();
(2) reuse — a single lambda serves multiple container types, the
primary motivation for the feature;
(3) interaction — generic lambda returns + captures work naturally
without extra syntax.
ex 0 isolates layer (1). identity forces the learner to write auto
in a parameter position. greater verifies the return expression is
type-checked per instantiation. get_type_size confirms that sizeof
on a generic parameter works correctly, reinforcing the "compiler
stamps out per-type copies" mental model.
ex 1 layers (2) and (3) on top. The desc lambda applied to both
vector and vector makes the payoff tangible. The find_if
exercise adds a capture without complicating the generic parameter.
The make_multiplier factory previews generic-lambda-returning-lambda,
which is a pattern that only becomes practical with C++14 return type
deduction.
We deliberately limited this chapter to 2 exercises. A third exercise
covering perfect forwarding + auto&& + decltype(auto) is better placed
in the decltype(auto) chapter where the forwarding semantics are the
primary topic, not a side note.
Build wiring:
solutions/xmake.lua
Closes #60
CLA: I have read the CLA and by submitting this PR agree to its terms.