d2mcpp icon indicating copy to clipboard operation
d2mcpp copied to clipboard

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

Open lczllx opened this issue 5 months ago • 0 comments

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 avatar Jun 15 '26 13:06 lczllx