C++ template metaprogramming学习笔记一

概念:Metadata:能够在编译期被操作的东西就是元数据,分成两类:类型和非类型,这两类都可以做为模板参数。整数类型,函数的指针或引用,成员指针(这些都可以转化成整数,因此是合法的)Metafunction:类模板,它的所有参数都是类型;                            类,有公共的可访问的内嵌类;多态:允许使用不同的参数类型的性质就是多态,字面上即“多种形式的能力”;在C
摘要由CSDN通过智能技术生成
概念:
Metadata :能够在编译期被操作的东西就是元数据,分成两类:类型和非类型,这两类都可以做为模板参数。整数类型,函数的指针或引用,成员指针(这些都可以转化成整数,因此是合法的)
Metafunction :类模板,它的所有参数都是类型;
                             类,有公共的可访问的内嵌类;
多态:允许使用不同的参数类型的性质就是多态,字面上即“多种形式的能力”;
C++中,多态有两种:静态多态,即编译期多态;动态多态,即运行期多态;
编译期多态使得我们可以在编译期做出选择,而这正是我们所向往追求的。

Boosttype traits library:这是MPL的基石
以下代码抽自boost/或自己的替代实现,知其然,知其所以然,我们后面使用到的时候才可以了然于胸
1.    primary type
is_xxx
系列的metafunction具有成员value,而且我们只关心成员value的值为true/false

template <   bool  x  >   struct  bool_
{
       
static bool const value = x;        
       typedef bool_
<x> type;              
       typedef 
bool value_type;            
       
operator bool() const return x; } 
};
typedef bool_ < false >  false_;
typedef bool_
< true >  true_;

is_xxx系列派生自bool_,下面逐一给出is_xxx的实现
is_integral通过对integral类型进行特化达到

template < typename type >
struct  is_integral : false_ {} //  即默认所有类型均不是integral类型

 

//  at this moment, variadic template still has nothing to do with this condition
template < bool  arg0,  bool  arg1,  bool  arg2 = false bool  arg3 = false bool  arg4 = false bool  arg5 = false bool  arg6 = false bool  arg7 = false bool  arg8 = false bool  arg9 = false >
struct  ice_or
{
    
static const bool value = arg0+arg1+arg2+arg3+arg4+arg5+arg6+arg7+arg8+arg9;
}
;

template
< bool  arg0,  bool  arg1,  bool  arg2 = true bool  arg3 = true bool  arg4 = true bool  arg5 = true bool  arg6 = true bool  arg7 = true bool  arg8 = true bool  arg9 = true >
struct  ice_and
{
    
static const bool value = arg0*arg1*arg2*arg3*arg4*arg5*arg6*arg7*arg8*arg9;
}
;

// 然后对可枚举的integral类型进行特化,其中还有对应的const/volatile/const volatile版本

template <>   struct  is_integral < char > : true_... {} ;
template
<>   struct  is_integral < const   char > : true_... {} ;
template
<>   struct  is_integral < volatile   char > : true_... {} ;
template
<>   struct  is_integral < const   volatile   char > : true_... {} ;

 C++中的integral类型有以下:
bool / char / unsigned char / signed char / wchar_t / short / unsigned short / int / unsigned int / long / unsigned long / long long / unsigned long long
分别对以上类型进行特化,我们就可以得到完整的is_integral
is_float
的实现跟is_integral相同,c++中,float类型只有float/double/long double,还有对应的const/volatile/const volatile版本;

template < typename type >
struct is_void : false_ {} ;
template
<>   struct  is_void < void >  : true_ {} ;
template
<>   struct  is_void < const   void >  : true_ {} ;
template
<>   struct  is_void < const   volatile   void >  : true_ {} ;

Is_function使用variadic template实现,否则需要根据函数的参数个数一一进行特化,很麻烦,c++0x直接支持variadic templateconceptgcc已经实现了variadic template功能


template < typename type >
struct  is_function : false_ {} ;
template
< typename retType, typename ... argType >
struct  is_function < retType ( * )(argType...) >  : true_ {} ;
template < typename type >
struct  is_member_function : false_ {} ;
template
< typename retType, typename classType, typename ... argType >
struct  is_member_function < retType (classType:: * )(argType...) >  : true_ {} ;

template < typename type >   struct  is_array : false_... {} ;
template
< typename type,  int  N >   struct  is_array < type[N] >  : true_... {} ;
template
< typename type >   struct  is_array < type[] >  : true_... {} ;
//  然后是对应的const/volatile/const volatile版本true_{};
i s_member_pointer的实现类似于 is_function,其中, is_member_pointer分为两种: 1,类成员数据指针; 2,类成员函数指针

template < typename type >
struct  is_member_pointer :false_ {} ;
template
< typename dataType, typename classType >
struct  is_member_pointer < dataType classType:: *>  : true_ {} ;
template
< typename retType, typename classType, … argType >
struct  is_member_pointer < retype (classType:: * )(argType…) >  : true_ {} ;
// 还有对应的const/volatile/const volatile版本

template < typename type >
struct  is_pointer : false_ {} ;
template
< typename type >
struct  is_ponter < type *>  : true_ {} ;
template
< typename type >
struct  is_pointer : is_function < type > {} ;
template
< typename type >
struct  is_pointer : is_member_pointer < type > {} ;

 


template < typename type >
struct  is_reference : false_ {} ;
template
< typename type >
struct  is_reference < type &>  : true_ {} ;
//  还有对应的const/volatile/const volatile版本

 C++中的类类型包括:class/struct/union定义的类型,对于类类型,可以定义其类成员指针,包括成员数据指针及成员函数指针,boost就是利用这个性质来区分是否为类类型

template < typename type >   char  class_helper( int  type:: * );
template
< typename type >   int   class_helper(...);

template
< typename type >
struct  is_class_helper
{
    
static const bool value = sizeof(class_helper<type>(0))==sizeof(char? true : false;
}
;

template
< typename type >
struct  is_class : bool_ < is_class_helper < type > ::value > {} ;

现在,剩下is_union/is_enum未实现,这几个比较麻烦,需要编译器提供相当的支持,boost提供保守的支持,就是将is_union/is_enum都默认为false_

2.    secondary type

           template < typename type >
struct  is_arithmetic : boos_ <ice_or<is_integral<type>::value, is_float<type>::value>::value>{} ;

is_fundamental的实现:包含算术类型和void类型

template < typename type >
struct  is_fundamental: bool_ <ice_or<is_arithmetic<type>::value, is_void<type>::value>::value>{} ;


根据C++标准,复合类型包括数组/函数/指针/引用/类类型/枚举/成员指针,或者说,除了基础类型,其它的都是复合类型,而基础类型则只包括算术类型和void类型。

template < typename type >
struct  is_compound: bool_ <! is_fundamental < type > ::value > {} ;

C++中,对象是指除了函数和引用之外的所有东西

template < typename type >
struct  is_object: bool_ <!ice_or<is_function<type>::value, is_reference<type>::value>::value>{};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值