zoukankan      html  css  js  c++  java
  • boost之 remove_cv

    从remove_cv名字中,可以看出这个traits与const, volatile有关,具体什么作用,还是看看boost的代码吧
    注意boost\type_traits中的定义
    BOOST_TT_AUX_TYPE_TRAIT_DEF1(remove_cv,T,typename boost::detail::rvalue_ref_filter_rem_cv<T>::type)
    BOOST_TT_AUX_TYPE_TRAIT_PARTIAL_SPEC1_1(typename T,remove_cv,T&,T&)
    #if !defined(BOOST_NO_ARRAY_TYPE_SPECIALIZATIONS)
    BOOST_TT_AUX_TYPE_TRAIT_PARTIAL_SPEC1_2(typename T,std::size_t N,remove_cv,T const[N],T type[N])
    BOOST_TT_AUX_TYPE_TRAIT_PARTIAL_SPEC1_2(typename T,std::size_t N,remove_cv,T volatile[N],T type[N])
    BOOST_TT_AUX_TYPE_TRAIT_PARTIAL_SPEC1_2(typename T,std::size_t N,remove_cv,T const volatile[N],T type[N])
    #endif
    把宏展开,得到以下结果
    //BOOST_TT_AUX_TYPE_TRAIT_DEF1(remove_cv,T,typename boost::detail::rvalue_ref_filter_rem_cv<T>::type)
    //1
    template< typename T > struct remove_cv
    {
    public:
    typedef typename boost::detail::rvalue_ref_filter_rem_cv<T>::type type;
    BOOST_MPL_AUX_LAMBDA_SUPPORT(1,remove_cv,(T))
    };

    BOOST_TT_AUX_TEMPLATE_ARITY_SPEC(1,remove_cv)

    //#define BOOST_TT_AUX_TYPE_TRAIT_PARTIAL_SPEC1_1(typename T, remove_cv,T&,T&)
    //2
    template< typename T> struct remove_cv<T&>
    {
    public:
    typedef T&type;
    typedef int& TAG;
    };
    /**/

    //#define BOOST_TT_AUX_TYPE_TRAIT_PARTIAL_SPEC1_2(param1,param2,trait, T const[N], result)
    //3
    template< typename T, std::size_t N> struct remove_cv<T const[N]>
    {
    public:
    typedef T type[N];
    };
    //4
    template< typename T, std::size_t N> struct remove_cv<T volatile[N]>
    {
    public:
    typedef T type[N];
    };
    //5
    template< typename T, std::size_t N> struct remove_cv<const volatile[N]>
    {
    public:
    typedef T type[N];
    };
    可知,1是remove_cv的范化版本,2-5分别是对remove_cv的当T为T&, T const[N], T volatile[N], const Volatile[N]的特化版本,实际上,从上面可以看出,如果T是一个引用,则remove_cv<T>::type 还是一个引用,没有变,至于const 数组,volatile数组,则remove_cv<T>::type是T[N]
    最后,来看看remove_cv范型版本,BOOST_MPL_AUX_LAMBDA_SUPPORT(1,remove_cv,(T)) 和BOOST_TT_AUX_TEMPLATE_ARITY_SPEC(1,remove_cv) 忽略,rvalue_ref_filter_rem_cv代码如下
    template <class T>
    struct rvalue_ref_filter_rem_cv
    {
    typedef typename boost::detail::cv_traits_imp<T*>::unqualified_type type;
    };
    template <typename T> struct cv_traits_imp {};

    template <typename T>
    struct cv_traits_imp<T*>
    {
    BOOST_STATIC_CONSTANT(bool, is_const = false);
    BOOST_STATIC_CONSTANT(bool, is_volatile = false);
    typedef T unqualified_type;
    };

    template <typename T>
    struct cv_traits_imp<const T*>
    {
    BOOST_STATIC_CONSTANT(bool, is_const = true);
    BOOST_STATIC_CONSTANT(bool, is_volatile = false);
    typedef T unqualified_type;
    };

    template <typename T>
    struct cv_traits_imp<volatile T*>
    {
    BOOST_STATIC_CONSTANT(bool, is_const = false);
    BOOST_STATIC_CONSTANT(bool, is_volatile = true);
    typedef T unqualified_type;
    };

    template <typename T>
    struct cv_traits_imp<const volatile T*>
    {
    BOOST_STATIC_CONSTANT(bool, is_const = true);
    BOOST_STATIC_CONSTANT(bool, is_volatile = true);
    typedef T unqualified_type;
    };
    所以remove_cv<T>::type 就是去掉const, volatile修饰符后的类型

  • 相关阅读:
    python 元组及操作
    python 字典所有操作
    python 列表操作
    python 运算符
    python while循环
    python 分支语句 等值判断 逻辑运算符
    GDI+_从Bitmap里得到的Color数组值分解
    服务器设计策略
    服务器设计策略
    IOCP的缓冲区
  • 原文地址:https://www.cnblogs.com/wenlove/p/3085663.html
Copyright © 2011-2022 走看看