January 2020
Intermediate to advanced
454 pages
11h 25m
English
Each time the compiler sees the use of a template class with a given type, it creates a version of that type implicitly. This, however, can happen multiple times, reducing the speed of the compiler. If, however, the types that are expected to be used are known upfront, this issue can be solved using explicit template specialization. Take a look at this example:
#include <iostream>template<typename T>class the_answer{public: the_answer(T t) { std::cout << "The answer is: " << t << '\n'; }};
Earlier, we created a simple structure that outputs to stdout during construction. Normally, this class would be created by the compiler once the first specialization of the class is seen. We can, however, perform the following:
template ...