Passing types ( int , std::vector ) into templates as if they were data. 2. Metafunctions
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
Modern C++ (C++14/17/20) has shifted much of the "heavy lifting" from pure template syntax to constexpr and consteval functions. These allow you to write logic that looks like normal C++ but is guaranteed to run at compile time, significantly reducing the complexity of traditional template syntax. Why Use It?
To handle "base cases" (the exit condition for recursion). Passing types ( int , std::vector ) into
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.
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. If a template fails to compile during the
Introduced heavily in , these are standard metafunctions used to query properties of types. They allow your code to ask questions like: "Is this type a pointer?" ( std::is_pointer ) "Are these two types the same?" ( std::is_same ) "Can I add these two types together?" 5. Concepts (C++20)