《STL源码剖析》——Array

array

array本身内容较少,日常使用也不是很多,里面也没有很高深的技巧

1 array的基本架构

了解array的架构需要一个额外的语法知识:

int a[100];

int [100]b; // error

typedef int T[100];
T c;  // success

__array_traits针对Nm等于0的情况,有如下偏特化:

    template<typename _Tp>
    struct __array_traits<_Tp, 0> {
     struct _Type { };
     typedef true_type _Is_swappable;
     typedef true_type _Is_nothrow_swappable;
    
     static constexpr _Tp&
     _S_ref(const _Type&, std::size_t) noexcept { return *static_cast<_Tp*>(nullptr); }
    
     static constexpr _Tp*
     _S_ptr(const _Type&) noexcept { return nullptr; }
    };

2 array的iterator

array的iterator是Tp*类型的,在萃取机制作用于该种类型的iterator时,使用的是如下偏特化的迭代器萃取模板:

    /// Partial specialization for pointer types.
    template<typename _Tp>
    struct iterator_traits<_Tp*> {
      typedef random_access_iterator_tag iterator_category;
      typedef _Tp                         value_type;
      typedef ptrdiff_t                   difference_type;
      typedef _Tp*                        pointer;
      typedef _Tp&                        reference;
    };

猜你喜欢

转载自www.cnblogs.com/xhb19960928/p/11839707.html