随笔 - 31  文章 - 115  trackbacks - 0
<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(3)

随笔分类(38)

随笔档案(31)

收藏夹(4)

College

High School

最新随笔

搜索

  •  

积分与排名

  • 积分 - 21330
  • 排名 - 115

最新评论

阅读排行榜

评论排行榜



发信人: shifan (学习浮云技术), 板面: C++
标  题: 伪typeof
发信站: 飘渺水云间 (Tue Dec 19 16:38:45 2006), 转信
 1 /*
 2 用标准C++实现typeof是不可能的
 3 这个是我写的一个approached typeof
 4 所有需要被静态反射出来的类型必须先用DECL_TYPE注册
 5 模板如果仅仅带有1个参数可以用DECL_TEMPLATE_1注册
 6 多个参数的模板还不支持。。
 7 主要是没想好编码
 8 
 9 总共能注册64个类型
10 可以通过MAX_TYPE_NUMBER设置
11 
12 支持的模板嵌套层数大约为32 / log2(MAX_TYPE_NUMBER)
13 MAX_TYPE_NUMBER必须为2的整次数幂
14 */
15 namespace my_typeof
16 {
17 
18   const int MAX_TYPE_NUMBER = 64;
19 
20   template <int N>
21   struct dummy
22   {
23     int a[N];
24   };
25 
26 
27   template <int N, typename Arg1>
28   struct select_by_number_1;
29 
30   template <int N>
31   struct select_by_number
32   {
33     typedef typename select_by_number_1<% MAX_TYPE_NUMBER, typename
34 select_by_number</ MAX_TYPE_NUMBER>::type>::type type;
35   };
36 
37 
38   template <typename T>
39   struct number_of
40   {
41     static const int v = sizeof(generic_f(*(T*)0)) / sizeof(int);
42   };
43 
44 
45 #define DECL_TYPE(T, N) \
46   namespace my_typeof{  \
47   template<>\
48   struct select_by_number<N> \
49   {\
50     typedef T type;\
51   };\
52   dummy <N> generic_f(const T&);}
53 
54 
55 #define DECL_TEMPLATE_1(T, N) \
56   namespace my_typeof{        \
57   template<typename Arg1>\
58   struct select_by_number_1<N, Arg1>\
59   {\
60     typedef T<Arg1> type;\
61   };\
62   template <typename Arg1>\
63   dummy<+ number_of<Arg1>::v * MAX_TYPE_NUMBER > generic_f(const T<Arg1>&);}
64 
65 
66 
67 #define TYPE_OF(x) my_typeof::select_by_number<sizeof(my_typeof::generic_f(x)) /
68 sizeof (int)>::type
69 
70 }
71 
72 
73 //sample
74 #include <iostream>
75 #include <vector>
76 #include <list>
77 
78 
79 DECL_TYPE(int1);
80 DECL_TEMPLATE_1(std::vector, 2);
81 DECL_TEMPLATE_1(std::list, 3);
82 DECL_TYPE(double4)
83 
84 using namespace std;
85 int main(intchar*[])
86 {
87   vector<list<vector<list<double> > > > v1;
88   TYPE_OF(v1) v2;
89   v1 = v2;
90   return 0;
91 }
92 
93 


--
You well 撒法!You well all 撒法!

※ 内容修改:·shifan 于 Dec 21 14:21:57 修改本文内容·[FROM: shifan]
※ 来源:·飘渺水云间 freecity.cn·[FROM: shifan]
posted on 2006-12-21 14:29 shifan3 阅读(1136) 评论(6)  编辑 收藏 引用 所属分类: templateC++

FeedBack:
# re: 伪typeof 2006-12-22 14:10 yayv
C++标准中有RTTI阿,为什么不考虑下?  回复  更多评论
  
# re: 伪typeof 2006-12-22 16:07 小山日志
多个参数模板可不可以参考Loki里的Typelist来做,Template编程偶不熟,只是提议。

to yayv:C++的RTTI是不能运行时创建对象的,shifan的题目改为“C++的模拟反射机制”合适些。  回复  更多评论
  
# re: 伪typeof 2007-01-12 13:58 linghuchong
寒,一开始以为是某些编译器的类似sizeof的typeof关键字,结果发现不是……

不过既然是动态的为什么要加这么多限制呢?  回复  更多评论
  
# re: 伪typeof 2007-01-12 14:09 Francis Arcanum
@linghuchong
谁说是动态的?  回复  更多评论
  
# re: [yc]伪typeof 2007-11-09 17:11 liuhs
写的不错!  回复  更多评论
  
# re: [yc]伪typeof 2008-07-22 22:51 xiaowei
baidu 到 shifan大神的blog,进来膜拜一下  回复  更多评论
  

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航: