I have tried all source available on net and books not able to understand the concepts and syntax for the template.
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
int x = Factorial<4>::value; // == 24
int y = Factorial<0>::value; // == 1
}
what is the "value" which associate with "Factorial<4>::value" why we need enum here. what is the difference between struct Factorial<0> and struct Factorial, why if i am declaring like struct Factorial<4>, why its giving error
Aucun commentaire:
Enregistrer un commentaire