Skip to content

docs(cpp14): add 00-generic-lambdas book chapter, exercises, solutions, and build wiring#71

Open
lczllx wants to merge 4 commits into
mcpp-community:mainfrom
lczllx:feat/cpp14-00-generic-lambdas
Open

docs(cpp14): add 00-generic-lambdas book chapter, exercises, solutions, and build wiring#71
lczllx wants to merge 4 commits into
mcpp-community:mainfrom
lczllx:feat/cpp14-00-generic-lambdas

Conversation

@lczllx

@lczllx lczllx commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

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):

  • section 一/I: Basic usage — identity, multi-param, STL algorithms,
    captures, returning lambda (factory pattern)
  • section 二/II: Notes — closure type uniqueness, perfect forwarding,
    non-variadic
  • section 三/III: Exercise topics and d2x checker command
  • section 四/IV: External resources
  • register chapter in zh/en SUMMARY.md and book/README.md

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 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:

  • register cpp14 in dslings/xmake.lua, dslings/en/xmake.lua,
    solutions/xmake.lua
  • new xmake.lua per directory with set_languages("cxx14")
  • remove stale dslings/cpp14/README.md TODO placeholder

Closes #60

CLA: I have read the CLA and by submitting this PR agree to its terms.

lczllx added 2 commits June 15, 2026 20:21
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
Comment thread book/src/cpp14/00-generic-lambdas.md Outdated
Comment thread book/src/cpp14/00-generic-lambdas.md
…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

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

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 thread book/en/src/cpp14/00-generic-lambdas.md
Comment thread book/src/cpp14/00-generic-lambdas.md Outdated
Comment on lines +187 to +189
### 泛型 lambda 不是 variadic

泛型 lambda 的参数个数仍然是固定的 — `[](auto a, auto b)` 接受恰好两个参数。如果要变参, 仍然需要可变参数模板 (C++20 后才支持 lambda 中使用 `...` 参数包)
Comment thread book/en/src/cpp14/00-generic-lambdas.md Outdated
Comment thread book/src/cpp14/00-generic-lambdas.md
Comment thread book/en/src/cpp14/00-generic-lambdas.md Outdated
- 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
@lczllx lczllx force-pushed the feat/cpp14-00-generic-lambdas branch from bd74a99 to de16c08 Compare June 17, 2026 03:01
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.

SubTask: C++14 - 00 - generic lambdas

3 participants