I have a quick question about item 48 in Scott Meyers' "Effective C++". I just don't understand the code copied from the book below,
#include <iostream>
using namespace std;
template <unsigned n>
struct Factorial
{
enum { value=n*Factorial<n-1>::value };
};
template <>
struct Factorial<0>
{
enum { value=1};
};
int main()
{
cout<<Factorial<5>::value<<endl;
cout<<Factorial<10>::value<<endl;
}
Why do I have to use enum in template programming? Is there an alternative way to do this? Thanks for the help in advance.
You could use
static const intalso:This should be fine also. The result is same in both cases.
Or you could use existing class template, such as
std::integral_constant(in C++11 only) as: