Templates – More than Generic Programming
Templates can also deliver much more than just generic programming (a cookie cutter with types). In the case of generic programming, the template operates as a blueprint that cannot be changed and delivers the compiled version of the template for the specified type or types.
Templates can be written to provide specialization of a function or algorithm based on the types involved. This is known as template specialization and is not generic programming in the sense we previously used it. It can only be called generic programming when it makes certain types behave as we would expect them to do in a given context. It cannot be called generic programming when the algorithm that's used for all types is modified.
Examine the following sample of specialization code:
#include <iostream>
#include <type_traits>
template <typename T, std::enable_if_t<sizeof(T) == 1, int> = 0>
void print(T val)
{
printf("...