9

What does a template class without arguments mean? For example, let's take a template class who calculate factorial, whose template arguments in N - N!.

basically, this is the class:

template <int N> class Factorial
{
public:
    enum {fact = N * Factorial<N-1>::fact};
};

But, I found that the this class have an "extention class",

template<> class Factorial<1>
{
public:
    enum {fact = 1};
};

And here my question is: what does a template without arguments, template<> mean?

Thanks in advance.

Billie
  • 8,938
  • 12
  • 37
  • 67

1 Answers1

16

This

template<> class Factorial<1>
{
public:
    enum {fact = 1};
};

is actually a template full specialization or explicit specialization of class template Factorial. There is also something called template partial specialization. Both are forms of template specialization.

Template specializations are special cases wherein when you instantiate a template with the parameter indicated by the template specialization, that specific template specialization is used, instead of the original template.

In your code, the original Factorial template class

template <int N> class Factorial
{
public:
    enum {fact = N * Factorial<N-1>::fact};
};

is used when you instantiate, for example, the following:

  • Factorial<3>
  • Factorial<5>
  • Factorial<42>

But when you instantiate/use

Factorial<1>

the template specialization Factorial<1> is used instead. In other words, it is a special case that is used whenever you supply 1 as the template parameter.

One notable example of a template specialization is std::vector<bool>, though you must be careful whether to use it or not.

Also an example. That show's some minimal usage of template specializations for both class templates and function templates.

Community
  • 1
  • 1
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
  • What do you mean by "template specialization"? – Billie Aug 01 '13 at 03:55
  • Amm , Ok , I've got your edit. Now everything make sense. very thank you! – Billie Aug 01 '13 at 03:58
  • @aaronman: technically it is *"template **full** specialization"*. I also edited the answer. and .... +1 – Nawaz Aug 01 '13 at 04:20
  • @Nawaz both partial template specialization and template full specialization are forms of template specialization – aaronman Aug 01 '13 at 04:22
  • @Nawaz That's better, but saying *"template specialization"* is still correct, as a full specialization is a template specialization. – Mark Garcia Aug 01 '13 at 04:23
  • @aaronman: Yes, but "template full specialization" is more specific, technically. Note that *template full specialization* is **not** a template anymore! – Nawaz Aug 01 '13 at 04:23
  • @Nawaz being unspecific isn't incorrect, anyway I should've answered I knew this was gonna be a big rep gainer – aaronman Aug 01 '13 at 04:25
  • @aaronman: I didn't say it is incorrect. It is just "technically more specific and precise". – Nawaz Aug 01 '13 at 04:26
  • Also, "template full specialization" is also called **explicit specialization**. It is better to use more specific terminologies. It helps avoiding/removing confusion. – Nawaz Aug 01 '13 at 04:28