
TMP is a purely sub-language. Because the compiler cannot "change" a value once it is defined during a build, you don't use loops or variables. Instead, you use: Recursion: To mimic loops.
This is a fundamental rule used to "sift" through template overloads. If a template fails to compile during the substitution of types, the compiler doesn't crash—it simply ignores that specific overload and looks for another that works. This is the backbone of and library-level introspection. 4. Type Traits
The primary goal of TMP is . By moving logic to the compilation phase, the final executable is smaller and faster because the work has already been done by the compiler before the user ever runs the program. C Template Metaprogramming Concepts
Uses a using alias or typedef to return a new type (e.g., removing a const qualifier). 3. SFINAE (Substitution Failure Is Not An Error)
Concepts are the modern evolution of TMP. Instead of relying on complex SFINAE "hacks" to restrict templates, allow you to explicitly define requirements for template arguments using the requires keyword. This makes error messages much more readable and the code intent clearer. 6. Variable Templates and constexpr TMP is a purely sub-language
C++ (TMP) is essentially a "program within a program." It allows you to execute logic during compilation rather than at runtime, using the compiler as an interpreter to generate optimized code. 1. The Core Mechanism: Functional Programming
In standard C++, a function takes values and returns a value. In TMP, a takes types or constants and "returns" a new type or constant. This is a fundamental rule used to "sift"
Passing types ( int , std::vector ) into templates as if they were data. 2. Metafunctions