Template metaprogramming
Template metaprogramming involves using templates to write code that generates different functions, types, and constants at compile time based on the types used in the template arguments. Template metaprogramming is an advanced technique heavily utilized in modern C++ libraries. It may be overwhelming, so it is perfectly fine if it appears to be hard to understand. Take this as merely an introduction and an exploration of this interesting topic.
Let us go back to the example of the add template function. Is there something we can do if we want to enforce that this template function is used only for arithmetic types such as integers and floats?
<type_traits> header from metaprogramming library provides us with the std::enable_if template type, which accepts two parameters, a Boolean and a type. If a Boolean is true, the resulting type will have a public typedef member, type. Let’s take a look at the following example:
#include <type_traits...