《STL源码剖析》笔记:traits技术

  • 例子1

    template <typename T>
    struct my_is_void
    {
        static const bool value = false;
    };
      
    template <>
    struct my_is_void<void>
    {
        static const bool value = true;
    };
      
    cout << my_is_void<int>::value << endl;
    cout << my_is_void<void>::value << endl;
    

    上面这个例子就是利用模板偏特化以及类静态变量实现对类型判断是否谓空类型的例子。

  • 例子2

    template <typename Iterator>
    void func(Iterator iter)
    {
        // 我们想用迭代器所指的类型定义一个变量该怎么办?
        *iter var // 这样是不能通过编译的。
    }
      
    // 转换思路
    template <typename Iterator>
    void func(Iterator iter)
    {
         __func(&*iter);
    }
    
    template <typename T>
    void __func(T* ) 
    {
        T var;
    }
    
  • 例子3:type_traits
    在C++中有的比如int, bool, char等类型没有必要有构造,拷贝构造等函数,我们称之为trivial_default_constructor,trivial_copy_constructor,trivial_assignment_operator,trivial_destructor,我们称之为POD_type。反之称之为NO_POD_type。这个定义可能不严谨,但不是今天这篇文的重点。

    在SGI STL底层的有这样一个函数。目的是析构迭代器范围[ first, last )的元素

    template <typename ForwardIterator>
    inline void destory(ForwardIterator first, ForwardIterator last) 
    {
        if (is_POD_type(*first))
            ;  //假设是int等POD_type类型,不需要析构。
        if (is_no_POD_type(*first))
            for (; first != last; first++) 
                first->~(*first)()    // 调用析构函数,(*first)不能通过编译。
    }  
    

    但是怎么表现 is_POD_type 和 is_no_POD_type 这2个意思呢?
    这个时候我们可以用内嵌型别偏特化来实现。

    // 标明类型
    struct _true_type  {  };
    struct _false_type  {  };
      
    // 用于萃取是否为POD_type
    template <typename T>
    struct _type_traits
    {  
        typedef _false_type has_trivial_default_constructor;
        typedef _false_type has_trivial_copy_constructor;
        typedef _false_type has_trivial_assignment_operator;
        typedef _false_type has_trivial_destructor;
        typedef _false_type is_POD_type;
    };
      
    // char偏特化
    template<>
    struct _type_traits<char> 
    {
        typedef _true_type has_trivial_default_constructor;
        typedef _true_type has_trivial_copy_constructor;
        typedef _true_type has_trivial_assignment_operator;
        typedef _true_type has_trivial_destructor;
        typedef _true_type is_POD_type;
    };
      
    // int偏特化
    template<>
    struct _type_traits<int> 
    {
        typedef _true_type has_trivial_default_constructor;
        typedef _true_type has_trivial_copy_constructor;
        typedef _true_type has_trivial_assignment_operator;
        typedef _true_type has_trivial_destructor;
        typedef _true_type is_POD_type;
    };
      
    ......其他POD类型定义偏特化版本。
    
    // 判断元素是否为non trivaial destructor。激活重载决议
    template <typename ForwardIterator>
    inline void destory(ForwardIterator first, ForwardIterator last) 
    {
        /*  利用_type_traits类嵌型别萃取 */
        typedef typename _type_traits<ForwardIterator>::is_POD_type is_POD_type;
        __destory_aux(first, last, is_POD_type());
    }
    
    // 元素是trivial destructor, 则什么都不用做
    template <typename ForwardIterator>
    inline void __destory_aux
    (ForwardIterator first, ForwardIterator last, _true_type) 
    {
    }
     
    // 元素是non trivial destrouctor,调用destroy
    template <typename ForwardIterator>
    inline void __destory_aux(ForwardIterator first, ForwardIterator last,    
    _false_type) 
    {
        for (; first != last; ++first)
            destory(&*first);
    }
    
    // destroy中调用析构函数
    template <typename T>
    inline void destory(T* pointer) 
    {
        pointer->~T();
    }
    
  • 例子4:迭代器中的iterator_traits
    见下一篇文章迭代器中对迭代器类型萃取部分。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容