Template metaprogramming is programming at compile-time. However, what does template metaprogramming have in common with the type-traits library? As it turns out, quite a lot! The type-traits library is pure template metaprogramming enclosed in a library.
- Programming at compile-time.
- Programming with types such as
intand not with values such as 5.5 or 5.
- The compiler instantiates the templates and generates C++ code.
The type traits library requires the following header:
Looking carefully, we can see that type traits have a big optimization potential. In the first step, the type traits analyze the code at compile-time, and in the second step, they optimize the code based on that analysis. How is that possible? Depending on the type of the variable, a faster variant of an algorithm will be chosen.
- An optimized version of
std::equalis used so the algorithms can work on memory blocks.
- Type checks will be performed at compile-time.
- Together with
static_assert, the type information defines the requirements for the code.