从网上偶然看来的 #include <iostream> using namespace std; template<int n> class fac{ public: enum{var = n * fac<n - 1>::var}; }; template<> class fac<1>{ public: enum{var = 1}; }; int main() { cout<<fac<10>::var<<endl; system("pause"); return 0; } #include <iostream> using namespace std; template <bool condition, class Then, class Else> struct IF { typedef Then RET; }; template <class Then, class Else> struct IF <false, Then, Else> { typedef Else RET; }; struct Stop { enum { RET = 1 }; }; template <int n> struct Factorial { typedef typename IF <n==0, Stop, Factorial <n-1> >::RET PrevFact; enum { RET = (n==0) ? (PrevFact::RET) : (PrevFact::RET) * n }; }; int main() { cout<<Factorial <10>::PrevFact::RET <<endl; system("pause"); return 0; }