Boost库中的Traits(is_float, is_class)

 对以判断是不是某个基本类型(整数, 浮点, bool)可以参考下面代码:

3. is_float

[cpp]  view plain copy
  1. template<typename T>  
  2. struct is_float : bool_type<false>{};  
  3.   
  4. #define IS_FLOAT(T)  template<> struct is_float<float> : bool_type<true>{};/  
  5.     template<> struct is_float<const float> : bool_type<true>{};/  
  6.     template<> struct is_float<volatile float> : bool_type<true>{};/  
  7.     template<> struct is_float<const volatile float> : bool_type<true>{}  
  8.   
  9. IS_FLOAT(float);  
  10. IS_FLOAT(double);  
  11. IS_FLOAT(long double);  

 

4. is_calss

判断一个类型T是不是类可以判断是不是存在 void(T::*)(void)类型的成员函数, 当然这里并不需要真存在。

由于对struct, class这些都是满足的, 所以并不能吧struct和class中区分出来。

[cpp]  view plain copy
  1. typedef char yes_type;  
  2. typedef int no_type;  
  3.   
  4. template<typename T>  
  5. struct is_class_imp{  
  6.     template <typename U> static yes_type is_calss_tester(void(U::*)(void));  
  7.     template <typename U> static no_type is_calss_tester(...);  
  8.   
  9.     static const bool value = (sizeof(is_calss_tester<T>(0)) == sizeof(yes_type));  
  10. };  
  11.   
  12. template<typename T>  
  13. struct is_class : is_class_imp<T>{};  
  14.   
  15.   
  16. struct A{};  
  17. class B{};  
  18. enum C{};  
  19.   
  20. int main()  
  21. {  
  22.     bool is_1 = is_class<int>::value;  //false  
  23.     bool is_2 = is_class<A>::value;  
  24.     bool is_3 = is_class<B>::value;  
  25.     bool is_4 = is_class<C>::value;     //false  
  26.     bool is_6 = is_class<const A>::value;  
  27. }  

 

后面回接着讲述

is_function

is_member_function_pointer

is_member_object_pointer

is_member_pointer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值