傳統版:
unsigned int Factorial (unsigned int n)
{
{
if ( n<=1 ) return 1 ;
else return n*Factorial(n-1);
else return n*Factorial(n-1);
}
int main()
{
{
cout << Factorial (10) << endl ;
return 0 ;
return 0 ;
}
meta-programming 版:
template<unsigned n>
struct Factorial
{
struct Factorial
{
enum { value = n * Factorial<n-1>::value } ;
};
template<>
struct Factorial<0>
{
struct Factorial<0>
{
enum { value = 1 } ;
};
int main()
{
{
cout << Factorial<10>::value << endl ;
return 0 ;
return 0 ;
}
沒有留言:
張貼留言