《STL源碼剖析》——Array

array

array自己內容較少,平常使用也不是不少,裏面也沒有很高深的技巧c++

1 array的基本架構

瞭解array的架構須要一個額外的語法知識:架構

int a[100];

int [100]b; // error

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

__array_traits針對Nm等於0的狀況,有以下偏特化:app

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時,使用的是以下偏特化的迭代器萃取模板:dom

/// 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;
    };
相關文章
相關標籤/搜索