比这篇新的文章:
题目:编程计算从1到2008080808之间的整数有多少个含有数字7
比这篇旧的文章: 一个用wxPython实现有FLASH播放器(转贴)
作者: xpycc, 点击254次, 评论(0), 收藏者(0)
打分:
所有评论,共0条:( 我也来说两句)
比这篇旧的文章: 一个用wxPython实现有FLASH播放器(转贴)
template meta test
语言: C++, 标签: 无 2008/08/23发布 2个月前更新 更新记录作者: xpycc, 点击254次, 评论(0), 收藏者(0)
C++语言: template meta test
01 #include <iostream>
02 using namespace std;
03
04 struct template_false{ //false class配合template_if使用
05 static const int result=false;
06 };
07
08 template<bool flag,class T1,class T2>
09 //这里用T1,T2似乎可以使其中一个的result不被计算
10 struct template_if{ //true的情况
11 static const int result=T1::result;
12 };
13
14 template<class T1,class T2>
15 struct template_if<false,T1,T2>{ //false的情况
16 static const int result=T2::result;
17 };
18
19 template<int n,int i,bool flag=i<=n/2 >
20 struct loop{ //判断循环
21 static const int result
22 =template_if<n%i==0, template_false, loop<n,i+1> >::result;
23 //你也可以用 n%i==0?false:loop<n,i+1>::result
24 //这里使用纯的template风格
25 };
26
27 template<int n,int i>
28 struct loop<n,i,false>{ //递归出口
29 static const int result=true;
30 };
31
32 template <int N>
33 inline void fun(){ //输出函数,看起来inline应该没有用
34 static const int result=loop<N,2>::result;
35 fun<N-1>();
36 if(result) //result中存储了结果
37 cout<<N<<" ";
38 }
39
40 template <>
41 inline void fun<1>(){ //递归出口
42 static const int result=false;
43 if(result) //本人有“洁癖”,不喜欢任何Warning
44 cout<<1<<" ";
45 }
46
47 int main(){
48 fun<100>(); //注意在尖括号里传递参数,而不是小括号
49 return 0;
50 }
02 using namespace std;
03
04 struct template_false{ //false class配合template_if使用
05 static const int result=false;
06 };
07
08 template<bool flag,class T1,class T2>
09 //这里用T1,T2似乎可以使其中一个的result不被计算
10 struct template_if{ //true的情况
11 static const int result=T1::result;
12 };
13
14 template<class T1,class T2>
15 struct template_if<false,T1,T2>{ //false的情况
16 static const int result=T2::result;
17 };
18
19 template<int n,int i,bool flag=i<=n/2 >
20 struct loop{ //判断循环
21 static const int result
22 =template_if<n%i==0, template_false, loop<n,i+1> >::result;
23 //你也可以用 n%i==0?false:loop<n,i+1>::result
24 //这里使用纯的template风格
25 };
26
27 template<int n,int i>
28 struct loop<n,i,false>{ //递归出口
29 static const int result=true;
30 };
31
32 template <int N>
33 inline void fun(){ //输出函数,看起来inline应该没有用
34 static const int result=loop<N,2>::result;
35 fun<N-1>();
36 if(result) //result中存储了结果
37 cout<<N<<" ";
38 }
39
40 template <>
41 inline void fun<1>(){ //递归出口
42 static const int result=false;
43 if(result) //本人有“洁癖”,不喜欢任何Warning
44 cout<<1<<" ";
45 }
46
47 int main(){
48 fun<100>(); //注意在尖括号里传递参数,而不是小括号
49 return 0;
50 }
所有评论,共0条:( 我也来说两句)
代码