#include <iostream>
using namespace std;
struct template_false{ //false class配合template_if使用
static const int result=false;
};
template<bool flag,class T1,class T2>
//这里用T1,T2似乎可以使其中一个的result不被计算
struct template_if{ //true的情况
static const int result=T1::result;
};
template<class T1,class T2>
struct template_if<false,T1,T2>{ //false的情况
static const int result=T2::result;
};
template<int n,int i,bool flag=i<=n/2 >
struct loop{ //判断循环
static const int result
=template_if<n%i==0, template_false, loop<n,i+1> >::result;
//你也可以用 n%i==0?false:loop<n,i+1>::result
//这里使用纯的template风格
};
template<int n,int i>
struct loop<n,i,false>{ //递归出口
static const int result=true;
};
template <int N>
inline void fun(){ //输出函数,看起来inline应该没有用
static const int result=loop<N,2>::result;
fun<N-1>();
if(result) //result中存储了结果
cout<<N<<" ";
}
template <>
inline void fun<1>(){ //递归出口
static const int result=false;
if(result) //本人有“洁癖”,不喜欢任何Warning
cout<<1<<" ";
}
int main(){
fun<100>(); //注意在尖括号里传递参数,而不是小括号
return 0;
}