BOOK / SOURCE & READING RECORD
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
David Abrahams; Aleksey Gurtovoy
1st edition, copyright 2005 (published December 2004)
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
Actually finished reading 3.1–3.7 (including exercises) of InformIT’s official Chapter 3, A Deeper Look at Metafunctions, and read the corresponding body text of the Boost MPL tutorial on dimensions, quantities, addition/subtraction, multiplication, and division. Chapters 1–2, 4–11 and the appendices were not read; Boost documentation is not the whole book, and the publisher’s purchased edition was not obtained.
Edition, actual reading range, and original sources →Sources
- Official Chapter 3 An In-Depth Look at Metafunctions in C++
All online body text of 3.1–3.7: dimensions, metafunction class, lambda/apply, lazy evaluation, and exercises.
- Boost MPL tutorial: Dimensional Analysis
Read this page and the adjacent pages on representing dimensions, quantities, and addition, subtraction, multiplication, and division; corresponds only to part of Chapter 3.
- InformIT edition and sample-chapter entry
First edition and complete table of contents; not the full book.
Let types block dimensional errors; do not overstate their power
Chapter 3 §§3.1.1–3.1.5 Dimensional Analysis; 3.2 Higher-Order Metafunctions; 3.5.4 lazy evaluation.
After attaching dimensions to numbers, whether addition is legal and what dimensions multiplication and division produce can be decided by type computation, while the numeric values are still handled at runtime. This division of labor has more practical value than “compute everything ahead of time”: callers cannot add seconds directly to a length, and library authors need not repeatedly compare tags at every site. The original book uses MPL sequences, transformations, and higher-order metafunctions to express exponent arithmetic; a modern small example can express a finite number of dimensions directly with integer template parameters, without bringing in the entire historical toolkit. However, dimensional correctness does not mean unit-scale correctness, nor that numeric precision, overflow, and division by zero have been solved. Metaprogramming should eliminate one well-defined class of errors while writing down the boundaries it does not cover.
Related fundamentals
Type traits and if constexpr compile-time branchingTemplate deduction: match parameters first, then instantiate codeConcepts and requires: Constraining Callable Interfaces