VC编程之VC6下实现remove_reference的方法
小标 2019-05-07 来源 : 阅读 1632 评论 0

摘要:本文主要向大家介绍了VC编程之VC6下实现remove_reference的方法,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

本文主要向大家介绍了VC编程之VC6下实现remove_reference的方法。,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

VC编程之VC6下实现remove_reference的方法

VC6对模板的支持很差,有人断言VC6下不可能实现通用的remove_reference。我参考了boost,摘录其中的部分,实现了VC6下可运行的remove_reference。


核心代码如下:


//remove_reference.h
#ifndef _REMOVE_REFERENCE_H_
#define _REMOVE_REFERENCE_H_

namespace boost {

namespace type_traits {
    
    template <class T> struct wrap {};

    typedef char yes_type;
    struct no_type
    {
      char padding[8];
    };
    
}// namespace boost::type_traits

namespace detail {
    
  using ::boost::type_traits::yes_type;
  using ::boost::type_traits::no_type;
  using ::boost::type_traits::wrap;

#define BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(trait,sp,C) \
  template<> struct trait##_impl< sp > \
  { \
  enum {value = (C)}; \
  };    
    
  template <class T> T&(* is_reference_helper1(wrap<T>) )(wrap<T>);
  char is_reference_helper1(...);
    
  template <class T> no_type is_reference_helper2(T&(*)(wrap<T>));
  yes_type is_reference_helper2(...);
    
  template <typename T>
    struct is_reference_impl
  {
    enum{
      value = sizeof(
        ::boost::detail::is_reference_helper2(
        ::boost::detail::is_reference_helper1(::boost::type_traits::wrap<T>()))) == 1
    };
    //      BOOST_STATIC_CONSTANT(
    //        bool, value = sizeof(
    //                         ::boost::detail::is_reference_helper2(
    //        ::boost::detail::is_reference_helper1(::boost::type_traits::wrap<T>()))) == 1
    //        );
  };

    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,void,false) // VC6用这一个就可以了,void const等也解决了
//    #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
//    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,void const,false)
//    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,void volatile,false)
//    BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_reference,void const volatile,false)
//    #endif
    
} // namespace detail

template <typename T>
struct is_reference
{
  enum{
    value = detail::is_reference_impl<T>::value
  };
};


namespace detail {
  template<typename ID>
                struct msvc_extract_type
  {
    struct id2type;
  };
    
  template<typename T, typename ID>
                struct msvc_register_type : msvc_extract_type<ID>
  {
    typedef msvc_extract_type<ID> base_type;
    struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature
    {
      typedef T type;
    };
        };
    
  template<bool IsReference>
                struct remove_reference_impl_typeof {
    template<typename T,typename ID>
                        struct inner {
      typedef T type;
    };
  };
  template<>
                struct remove_reference_impl_typeof<true> {
    template<typename T,typename ID>
                        struct inner {
      template<typename U>
                                static msvc_register_type<U,ID> test(U&(*)());
      static msvc_register_type<T,ID> test(...);
      //BOOST_STATIC_CONSTANT(unsigned,register_test=sizeof(test( (T(*)())(NULL) ) ));
      enum {register_test=sizeof(test( (T(*)())(NULL) ) )};
      typedef typename msvc_extract_type<ID>::id2type::type type;
    };
  };
} //namespace detail

template<typename T>
struct remove_reference {
  typedef typename detail::remove_reference_impl_typeof<
    boost::is_reference<T>::value
                >::template inner<T,remove_reference<T> >::type type;
  //BOOST_MPL_AUX_LAMBDA_SUPPORT(1,remove_reference,T)
        };

}    

#endif

   

以上就介绍了VC/MFC的学习,希望对VC/MFC有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言VC/MFC频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程