VC6下实现remove_reference的方法。

VC6对模板的支持很差,有人断言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.gifnamespace boost {
InBlock.gif
InBlock.gifnamespace 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.gifnamespace 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.gifstruct 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.gifnamespace 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.gifstruct 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值