c++ - What does template <unsigned int N> mean? -


While declaring a template, I am used to do this type of code:

  Template & lt; Class T & gt;  

However, they used it:

  template & lt; Unsigned int N & gt;  

I checked that it is compiled but what does it mean? Is this a non-type parameter? And if so, how can we have a template without any type of parameter?

This template is completely possible instead of a type of class on an integer. We can specify templated values ​​in any variable, or else it can be literally added to any other integer:

  unsigned int x = N;  

In fact, we can create algorithms that evaluate (at) the time of compilation:

  template & lt; Int N & gt; Factorial structure {enum {value = N * factorial & lt; N-1 & gt; :: value}; }; Template & lt; & Gt; Structure Factorial & lt; 0 & gt; {Enum {value = 1}; }; // Factorial & lt; 4 & gt; :: value == 24 // factorial & lt; 0 & gt; :: value == 1 zero foo () {int x = factorial & lt; 4 & gt; :: Values; // == 24 int y = factorial & lt; 0 & gt; :: Values; // == 1}  

Comments