Skip to content

Metaprogramming Essentials (C++20-23) ​

This part picks up where Volume 1's template basics left off. Volume 1 taught the "mechanisms" — the compilation model of templates, specialization, two-phase lookup. This part is about how to use those mechanisms for compile-time computation and type deduction, and how C++20 concepts rewrite the drudgery that used to depend on SFINAE and enable_if into readable constraints.

Three threads run through it: concepts and requires expressions (C++20), classic template metaprogramming (TMP) techniques and their modernization, and compile-time strings plus C++26 reflection.

The companion runnable examples live at code/examples/vol4/vol3-metaprogramming-cpp20-23/; every file runs directly with g++ -std=c++20 xxx.cpp.

The concepts trio (01-03) lays the foundation: how to write constraints, how they participate in overloading, and the four kinds of requirements in a requires expression along with its two traps. Piece 04 steps back to the old TMP tricks (the internals of type_traits, template recursion, SFINAE, void_t, fold expressions) and the migration from SFINAE to concepts; 05 covers compile-time strings (the C++20 NTTP class type and fixed_string); 06 takes a look at C++26 static reflection (the reflection operator and splice from P2996); 07 covers template instantiation control (extern template and compile time); 08 covers templates and exception safety (move_if_noexcept and container reallocation); and finally 09 welds everything before it together with a concepts-constrained mini-STL algorithm library.

pdf-latest-4-g85128cc · 85128cc · 2026-10-05