15,440
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
const unsigned P = 10;
template <unsigned P> struct test
{
static void func()
{
cout << "func: " << P << endl;
test<P - 1>().func();
}
};
struct test<0>
{
static void func()
{
cout << "func0" << endl;
}
};
int main()
{
test<P>::func();
return 0;
}
//前两天看到一个题目:不能使用乘法计算N的阶乘
//模板元实现,挺有意思的,跟大家分享一下
#include<iostream>
using namespace std;
template<int SUM, int N>
struct Mul
{
enum{val = SUM + Mul<SUM, N - 1>::val};
};
template<int SUM>
struct Mul<SUM, 1>
{
enum{val = SUM};
};
template<int N>
struct Fact
{
enum{val = Mul<Fact<N - 1>::val, N>::val};
};
template<>
struct Fact<1>
{
enum{val = 1};
};
int main()
{
cout<<Fact<10>::val<<endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
const unsigned P = 10;
template <unsigned P>
struct test
{
static void func()
{
cout << "func: " << P << endl;
test<P - 1>::func();//这里直接使用::操作符调用就行了,没必要使用对象调用func静态成员函数了
}
};
template<>//这里应该是特化
struct test<0>
{
static void func()
{
cout << "func0" << endl;
}
};
int main()
{
test<P>::func();
return 0;
}