#include <iostream>
using namespace std;
int fact(int n)
{
return n < 1 ? 1 : n * fact(n - 1);
}
template<int N>
struct Fact {
static const int value = N * Fact<N-1>::value;
};
template<>
struct Fact<1> {
static const int value = 1;
};
template<>
struct Fact<0> {
static const int value = 1;
};
int main() {
cout << fact(5) << endl; //这个是程序运行的时候确定大小,(计算出来的)
cout << Fact<5>::value << endl;//这个是编译期就确定大小的,就是程序没运行,我就知道这个函数的执行结果的
}
C++ 函数运行期和编译期运行
最新推荐文章于 2025-01-17 21:12:47 发布