博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VC6下实现remove_reference的方法。
阅读量:7086 次
发布时间:2019-06-28

本文共 2876 字,大约阅读时间需要 9 分钟。

6对模板的支持很差,有人断言VC6下不可能实现通用的remove_reference。我参考了boost,摘录其中的部分,实现了VC6下可运行的remove_reference。
 
核心代码如下:
 
InBlock.gif
//remove_reference.h 

InBlock.gif#ifndef _REMOVE_REFERENCE_H_ 

InBlock.gif#define _REMOVE_REFERENCE_H_ 

InBlock.gif 

InBlock.gif
namespace boost { 

InBlock.gif 

InBlock.gif
namespace type_traits { 

InBlock.gif     

InBlock.gif    template <
class T> 
struct wrap {}; 

InBlock.gif 

InBlock.gif    typedef 
char yes_type; 

InBlock.gif    
struct no_type 

InBlock.gif    { 

InBlock.gif      
char padding[8]; 

InBlock.gif    }; 

InBlock.gif     

InBlock.gif}
// namespace boost::type_traits 

InBlock.gif 

InBlock.gif
namespace detail { 

InBlock.gif    

InBlock.gif  
using ::boost::type_traits::yes_type; 

InBlock.gif  
using ::boost::type_traits::no_type; 

InBlock.gif  
using ::boost::type_traits::wrap; 

InBlock.gif 

InBlock.gif#define BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(trait,sp,C) \ 

InBlock.gif  template<> 
struct trait##_impl< sp > \ 

InBlock.gif  { \ 

InBlock.gif  
enum {value = (C)}; \ 

InBlock.gif  };    

InBlock.gif    

InBlock.gif  template <
class T> T&(* is_reference_helper1(wrap<T>) )(wrap<T>); 

InBlock.gif  
char is_reference_helper1(...); 

InBlock.gif    

InBlock.gif  template <
class T> no_type is_reference_helper2(T&(*)(wrap<T>)); 

InBlock.gif  yes_type is_reference_helper2(...); 

InBlock.gif    

InBlock.gif  template <typename T> 

InBlock.gif    
struct is_reference_impl 

InBlock.gif  { 

InBlock.gif    
enum

InBlock.gif      value = 
sizeof

InBlock.gif        ::boost::detail::is_reference_helper2( 

InBlock.gif        ::boost::detail::is_reference_helper1(::boost::type_traits::wrap<T>()))) == 1 

InBlock.gif    }; 

InBlock.gif    
//      BOOST_STATIC_CONSTANT( 

InBlock.gif    
//        bool, value = sizeof( 

InBlock.gif    
//                         ::boost::detail::is_reference_helper2( 

InBlock.gif    
//        ::boost::detail::is_reference_helper1(::boost::type_traits::wrap<T>()))) == 1 

InBlock.gif    
//        ); 

InBlock.gif  }; 

InBlock.gif 

InBlock.gif    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,
void,
false
// VC6用这一个就可以了,void const等也解决了 

InBlock.gif
//    #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS 

InBlock.gif
//    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,void const,false) 

InBlock.gif
//    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,void volatile,false) 

InBlock.gif
//    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,void const volatile,false) 

InBlock.gif
//    #endif 

InBlock.gif     

InBlock.gif
// namespace detail 

InBlock.gif 

InBlock.giftemplate <typename T> 

InBlock.gif
struct is_reference 

InBlock.gif

InBlock.gif  
enum

InBlock.gif    value = detail::is_reference_impl<T>::value 

InBlock.gif  }; 

InBlock.gif}; 

InBlock.gif 

InBlock.gif 

InBlock.gif
namespace detail { 

InBlock.gif  template<typename ID> 

InBlock.gif                
struct msvc_extract_type 

InBlock.gif  { 

InBlock.gif    
struct id2type; 

InBlock.gif  }; 

InBlock.gif    

InBlock.gif  template<typename T, typename ID> 

InBlock.gif                
struct msvc_register_type : msvc_extract_type<ID> 

InBlock.gif  { 

InBlock.gif    typedef msvc_extract_type<ID> base_type; 

InBlock.gif    
struct base_type::id2type 
// This uses nice VC6.5 and VC7.1 bugfeature 

InBlock.gif    { 

InBlock.gif      typedef T type; 

InBlock.gif    }; 

InBlock.gif        }; 

InBlock.gif    

InBlock.gif  template<
bool IsReference> 

InBlock.gif                
struct remove_reference_impl_typeof { 

InBlock.gif    template<typename T,typename ID> 

InBlock.gif                        
struct inner { 

InBlock.gif      typedef T type; 

InBlock.gif    }; 

InBlock.gif  }; 

InBlock.gif  template<> 

InBlock.gif                
struct remove_reference_impl_typeof<
true> { 

InBlock.gif    template<typename T,typename ID> 

InBlock.gif                        
struct inner { 

InBlock.gif      template<typename U> 

InBlock.gif                                
static msvc_register_type<U,ID> test(U&(*)()); 

InBlock.gif      
static msvc_register_type<T,ID> test(...); 

InBlock.gif      
//BOOST_STATIC_CONSTANT(unsigned,register_test=sizeof(test( (T(*)())(NULL) ) )); 

InBlock.gif      
enum {register_test=
sizeof(test( (T(*)())(NULL) ) )}; 

InBlock.gif      typedef typename msvc_extract_type<ID>::id2type::type type; 

InBlock.gif    }; 

InBlock.gif  }; 

InBlock.gif
//namespace detail 

InBlock.gif 

InBlock.giftemplate<typename T> 

InBlock.gif
struct remove_reference { 

InBlock.gif  typedef typename detail::remove_reference_impl_typeof< 

InBlock.gif    boost::is_reference<T>::value 

InBlock.gif                >::template inner<T,remove_reference<T> >::type type; 

InBlock.gif  
//BOOST_MPL_AUX_LAMBDA_SUPPORT(1,remove_reference,T) 

InBlock.gif        }; 

InBlock.gif 

InBlock.gif}    

InBlock.gif 

InBlock.gif#endif
本文转sinojelly51CTO博客,原文链接:http://blog.51cto.com/sinojelly/100307
,如需转载请自行联系原作者
你可能感兴趣的文章
JSR与MR的区别
查看>>
华为存储不是昙花一现
查看>>
沟通是一种感知
查看>>
学会这二十个正则表达式,能让你少些1000行代码!
查看>>
关于Cocos Creator脚本执行顺序的几点补充
查看>>
Powershell-Exchange:设置分层通讯薄中通讯组的优先级
查看>>
开启好用的Lync联系人即时模糊搜索功能
查看>>
Microsoft Hyper-V Server 2012开启虚拟化-SMB 3.0
查看>>
Powershell管理系列(十二)Exchange新启用的邮箱禁用OWA及Activesync的访问
查看>>
Windows 8上安装本地回环网卡
查看>>
Exchange Server 2013系列十二:邮箱的基本管理
查看>>
[C#进阶系列]专题二:你知道Dictionary查找速度为什么快吗?
查看>>
并发连接数、请求数、并发用户数
查看>>
SDA报告给各国网络空间安全防卫水平进行评级
查看>>
去小机化思维(二)--【软件和信息服务】2015.03
查看>>
【翻译】Sencha Cmd中脚本压缩方法之比较
查看>>
最新.NET 5.0 C#6 MVC6 WCF5 NoSQL Azure开发120课视频
查看>>
爱因斯坦计划最新进展(201710)
查看>>
传统HA系统的终结者-【软件和信息服务】2013.11
查看>>
Spread for Windows Forms快速入门(15)---使用 Spread 设计器
查看>>