Template metaprogramming

“Generic programming is why we can’t have nice things” – Andrei Alexandrescu

Template metaprogramming (TMP) is a programming technique in which templates are used to generate program code at compile time. It is a powerful tool that enables you to write code that is both highly efficient and highly generic. TMP relies on the C++ template system to generate code at compile time, based on a set of parameters. This allows the programmer to create code that is both concise and highly efficient. TMP is used to implement advanced software design patterns such as the Singleton pattern, as well as to implement complex algorithms such as sorting. It is also used to create highly optimized code for specialized applications, such as graph algorithms. TMP is a powerful tool for experienced C++ programmers, but it does require a solid understanding of the C++ language and its template system.

Template classes and functions are a powerful feature of C++ that allow the programmer to create generic types and functions that can be used for many different data types. Templates are used extensively throughout the C++ Standard Library to provide a wide range of useful functionality.

Templates are used to define container classes such as vector, list, and map, which are used to store and manipulate data. Template algorithms such as sort, copy, and find are used to manipulate data in the containers. Template functions such as min and max are used to compare and search for values in containers.

Templates are also used to create generic programming utilities such as iterators, functors, and type traits. These are used to abstract away implementation details and provide a consistent interface for manipulating data.

Finally, templates are used to define type traits such as is_reference, is_pointer, and is_const. These type traits are used to determine properties of types at compile time and allow for type-safe programming.


SFINAE stands for Substitution Failure Is Not An Error. It is a compiler technique used to detect errors in template function specializations. SFINAE allows the compiler to ignore invalid template specializations and continue compilation, rather than aborting with an error. This technique is used to provide certain compile-time features, such as type traits and expression SFINAE.