it
template<> class Factorial<1> { public: enum {fact = 1}; };
it is actually a template for the full specialization or explicit specialization of a template of the Factorial class. There is also something called private specialization. Both are forms of specialization pattern .
Template specialization is a special case when when creating a template with the parameter specified by the template specialization, a special template specification is used instead of the original template.
In your code, the source class Factorial
template <int N> class Factorial { public: enum {fact = N * Factorial<N-1>::fact}; };
used when you create an instance, for example, the following:
Factorial<3>Factorial<5>Factorial<42>
But when you create / use
Factorial<1>
instead, the Factorial<1> template specification is used. In other words, this is a special case that is used when you add 1 as a template parameter.
One notable example of template specialization is std::vector<bool> , although you should be careful to use it or not .
Also an example . This shows the minimal use of specialized templates for class templates and function templates.
source share