模板元编程:求N的阶乘

求N的阶乘,要求不使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句?:

template <int SUM,int N>
struct Mul;

template <int N>
struct Fact
{
	enum {sum = Mul<Fact<N-1>::sum,N>::sum};
};

template <>
struct Fact<1>
{
	enum {sum = 1};
};


template <int SUM,int N>
struct Mul
{
	enum {sum = SUM+Mul<SUM,N-1>::sum};
};

template <int SUM>
struct Mul<SUM,1>
{
	enum {sum = SUM};
};


posted on 2010-11-15 13:13  SammyLan  阅读(3087)  评论(0编辑  收藏  举报

导航