C++ / a working model

BOOK / SOURCE & READING RECORD

C++ Templates: The Complete Guide

David Vandevoorde; Nicolai M. Josuttis; Douglas Gregor

2nd edition, 2017

READING EVIDENCE / Partial text read

C++ Templates: The Complete Guide

Sequentially read the readable main text and textual code of chapters 1–28 and appendices A–E of the second edition, and also cross-read official chapters 4 and 23. Inspected all 12 PDF pages corresponding to the 13 substantive figures: Figures 18.1–18.5 and D.1 are visible and have been read; Figures 13.1, 21.1–21.4, 27.1, and B.1 remain only as titles or links in the obtained PDF, with the graphics missing (7 figures in total), so coverage is still listed as partial and titles are not treated as having read the images. The graphic for Figure 18.4 is on the page before the title and has been verified. The actual edition is the second edition published in 2017 with copyright 2018; this is not presented as having read the first edition. Table of contents, bibliography, and index are not counted as main text. Concepts in the book are a pre-finalization C++20 design; the old-style -> bool syntax must not be treated as final C++20.

Edition, actual reading range, and original sources →

Sources

Parameter packs have algebra first, then syntax

Chapter 4, 4.2 Fold Expressions, 4.3 Variadic Template Applications; Chapter 23, 23.1, 23.3, 23.5.

A parameter pack turns a family of interfaces into one template; it does not automatically define empty-input, evaluation-order, or failure semantics. Fold expressions are first of all parenthesized structure: left folds and right folds differ for subtraction. If logical-and is used to compose checkers, the result of an empty set should be “no constraint was violated,” and not continuing to check after a runtime failure can avoid wasted work; but that short-circuit cannot replace compile-time validity checking of expressions. Chapter 23 further notes that compile time is also a computation that needs a budget; recursive type construction increases instantiation and diagnostic cost. For modern value computation prefer constexpr; keep template machinery only where type transformation is necessary, and do not treat complex template syntax itself as a performance gain.

Instantiable is not semantically correct; zero overhead is not zero cost

19.2, 19.4.4, 19.7.1; 20.3.3, 20.5; 21.1; 22.4–22.6; 25.3.4–25.5; 26.4.3; 27.3; 28.3–28.5; Appendix E.4.

Chapters 19–20 separate type facts, substitutable policies, and overload-participation conditions: detecting that an expression is valid does not mean it satisfies the order relation or unit semantics the algorithm needs. Chapters 21–26 show that layout, construction order, forwarding, type erasure, and a variant’s active-member state constrain one another; generalizing an interface cannot ignore object lifetime. Chapter 27’s expression templates can fuse loops, but the cross-element aliasing in x=A*x may still require a temporary. Chapter 28’s minimal-capability archetypes are used to expose extra operations an algorithm secretly depends on; a tracer can only prove the operation trace of the measured inputs, not substitute for a correctness argument. In C++20 prefer reusing standard traits, tuple, variant, and concepts with clear semantics; when customization is needed, weigh compile cost, runtime cost, and maintenance cost separately.

Related fundamentals

Parameter packs and fold expressions: handle empty packs firstWhat constexpr, consteval, and constinit each controlTemplate deduction: match parameters first, then instantiate code

Original practice →