通用C++ typeid实现(不支持RTTI) v0.2

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

介绍一个通用c++ typeid实现.

主要功能:
   通用的typeid实现,可以在VS60/VS2005以及mingw32下保证相同的类型名输出.
   程序使用boost::type_traits来实现,没有使用内置的typeid.不支持RTTI操作.
 
局限:
   目前版本v0.2仅实现一层function_ptr和member_function_ptr解析操作.

版本历史:
   v0.2 不再使用std::string.去除了std_name成员函数
   将 vfc::type_id_t<T>::name() 更改为 vfc::type_id_t<T>().name()

项目地址:
    http://code.google.com/p/typeid

主要类介绍:
   template<typename T>  vfc::type_id_t<>  获取类型的typeid名
   template<typename T>  vfc::type_id(const T & t) 获取变量的类型的type_id_t类型.

主要函数:
   type_id_t<int>().name() 输出 const char * 类型的typename
   type_id(100).name()     输出 const char * 类型的typename

定义类型:
   VFC_TYPE_ID(type,id) 实现类型的定义,type为类型,id为类型自定义数值标识
例: VFC_TYPE_ID(std::string,0x2001)

下面给出一个完整的例子:

 1  #include  < stdio.h >
 2  #include  " vfc_type.h "
 3 
 4  struct  a
 5  {
 6       const   int   *  test( int , int )
 7      {
 8           return  NULL;
 9      }
10 
11       void  test1()  const
12      {
13      }
14       const   char   *  test2( int int volatile
15      {
16           return  NULL;
17      }
18       const   int   &  test3( int   * const   char   * const   volatile
19      {
20           static   int  i  =   100 ;
21           return  i;
22      }
23  };
24 
25  void  test( const   char   * int  k)
26  {
27  };
28 
29  VFC_TYPE_ID(a, 0x2000 );
30 
31 
32  int  main( int  argc,  char *  argv[])
33  {
34      typedef  void  ( * T1)( void );
35      typedef  int  ( * T2)( void );
36      typedef  int  ( * T3)( int , int , int , int , int , int , int , int , int );
37      typedef  const   char   *  ( * T4)( int   & , int   * , char   ** , char   & , const   int   & );
38 
39      typedef  int  U1;
40      typedef  int  U2[ 5 ];
41      typedef  const   int   &  U3;
42      typedef  char  U4[ 5 ][ 6 ][ 7 ];
43 
44      printf( " T1 type %s/n " ,vfc::type_id_t < T1 > ().name());
45      printf( " T2 type %s/n " ,vfc::type_id_t < T2 > ().name());
46      printf( " T3 type %s/n " ,vfc::type_id_t < T3 > ().name());
47      printf( " T4 type %s/n " ,vfc::type_id_t < T4 > ().name());
48 
49      printf( " U1 type %s/n " ,vfc::type_id_t < U1 > ().name());
50      printf( " U2 type %s/n " ,vfc::type_id_t < U2 > ().name());
51      printf( " U3 type %s/n " ,vfc::type_id_t < U3 > ().name());
52      printf( " U4 type %s/n " ,vfc::type_id_t < U4 > ().name());
53 
54      printf( " /'d/' type %s/n " ,vfc::type_id( ' d ' ).name());
55      printf( " 100 type %s/n " ,vfc::type_id( 100 ).name());
56      printf( " 100.0 type %s/n " ,vfc::type_id( 100.0 ).name());
57      printf( " / " str/ "  type %s/n " ,vfc::type_id( " str " ).name());
58 
59       const   char   *  str  =   " ok " ;
60      printf( " const char * str type %s/n " ,vfc::type_id(str).name());
61 
62      a  *  pa;
63      printf( " a type %s/n " ,vfc::type_id_t < a > ().name());
64      printf( " pa type %s/n " ,vfc::type_id(pa).name());
65      printf( " a::test type %s/n " ,vfc::type_id( & a::test).name());
66      printf( " a::test type %s/n " ,vfc::type_id( & a::test1).name());
67      printf( " a::test type %s/n " ,vfc::type_id( & a::test2).name());
68      printf( " a::test type %s/n " ,vfc::type_id( & a::test3).name());
69      printf( " test %s/n " ,vfc::type_id( & test).name());
70      
71      typedef  int  (a:: * TT)( const   int   * , char const   volatile ;
72       const  TT  *  tt;
73      printf( " TT type %s/n " ,vfc::type_id_t < TT > ().name());
74      printf( " const TT * type %s/n " ,vfc::type_id(tt).name());
75 
76       return   0 ;
77  }
78

在VC60/ VC2003/VC2005/MinGW32下输出相同运行结果

T1 type  void  ( * )()
T2 type 
int  ( * )()
T3 type 
int  ( * )( int , int , int , int , int , int , int , int , int )
T4 type 
char   const   *  ( * )( int   & , int   * , char   *   * , char   & , int   const   & )
U1 type 
int
U2 type 
int  [ 5 ]
U3 type 
int   const   &
U4 type 
char  [ 7 ] [ 6 ] [ 5 ]
' d '  type  char
100  type  int
100.0  type  double
" str "  type  char  [ 4 ]
const   char   *  str type  char   const   *
a type a
pa type a 
*
a::test type 
int   const   *  (a:: * )( int , int )
a::test type 
void  (a:: * )() const
a::test type 
char   const   *  (a:: * )( int , int ) volatile
a::test type 
int   const   &  (a:: * )( int   * , char   const   * ) const   volatile
test 
void  ( * )( char   const   * , int )
TT type 
int  (a:: * )( int   const   * , char ) const   volatile
const  TT  *  type  int  (a:: * const   * )( int   const   * , char ) const   volatile
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值