sgi stl 特性萃取器

来源:互联网 发布:vscode css3前缀 编辑:程序博客网 时间:2024/06/08 06:08

1 迭代器相关类型的萃取(iterator_traits)

许多算法,如查找,指针移动等都传递的是迭代器,而不同迭代器的型别决定了其算法操作过程的差异

template <class Iterator>struct iterator_traits {  typedef typename Iterator::iterator_category iterator_category;  typedef typename Iterator::value_type        value_type;  typedef typename Iterator::difference_type   difference_type;  typedef typename Iterator::pointer           pointer;  typedef typename Iterator::reference         reference;};
针对原生指针类型,常量指针类型的的迭代器的萃取偏化版本,注意指针类型的迭代器是随机访问的迭代器
template <class T>struct iterator_traits<T*> {  typedef random_access_iterator_tag iterator_category;  typedef T                          value_type;  typedef ptrdiff_t                  difference_type;  typedef T*                         pointer;  typedef T&                         reference;};template <class T>struct iterator_traits<const T*> {  typedef random_access_iterator_tag iterator_category;  typedef T                          value_type;  typedef ptrdiff_t                  difference_type;  typedef const T*                   pointer;  typedef const T&                   reference;};
萃取函数(注意版本函数调用时不需要模板说明)
萃取迭代器本身类型
inline typename iterator_traits<Iterator>::iterator_categoryiterator_category(const Iterator&) {  typedef typename iterator_traits<Iterator>::iterator_category category;  return category();}
注意萃取操作是领用 int()创建临时对象的方法
萃取迭代器间距型别:指针类型
template <class Iterator>inline typename iterator_traits<Iterator>::difference_type*distance_type(const Iterator&) {  return static_cast<typename iterator_traits<Iterator>::difference_type*>(0);}
萃取迭代器所指类型:以指针进行标识
template <class Iterator>inline typename iterator_traits<Iterator>::value_type*value_type(const Iterator&) {  return static_cast<typename iterator_traits<Iterator>::value_type*>(0);}
2__type_traits数据型别的特性萃取器
意在萃取数据类型是否是POD数据型别,是否拥有trivial构造函数,拷贝函数等,赋值=,析构

基本类型,保证最低级别的设置
template <class type>struct __type_traits {   typedef __true_type     this_dummy_member_must_be_first;                   /* Do not remove this member. It informs a compiler which                      automatically specializes __type_traits that this                      __type_traits template is special. It just makes sure that                      things work if an implementation is using a template                      called __type_traits for something unrelated. */   /* The following restrictions should be observed for the sake of      compilers which automatically produce type specific specializations      of this class:          - You may reorder the members below if you wish          - You may remove any of the members below if you wish          - You must not rename members without making the corresponding            name change in the compiler          - Members you add will be treated like regular members unless            you add the appropriate support in the compiler. */   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;};
其后为偏化版本,包括了全部的built-in 类型,与原生指针类型
template <class T>struct __type_traits<T*> {   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;};

实例:给定迭代器的范围,首先萃取出迭代器所指即容器元素类型,其后根据类型判断类别是否有必要调用构造函数进行对象的析构过程

// 利用 value_type 萃取容器元素类型,注意类型以指针标识template <class ForwardIterator>inline void__destroy_aux(ForwardIterator first, ForwardIterator last, __false_type) {  for ( ; first < last; ++first)    destroy(&*first);}利用 __type_traits 萃取类型是否有必要的析构函数template <class ForwardIterator> inline void __destroy_aux(ForwardIterator, ForwardIterator, __true_type) {}// 有必要的析构函数, 对每一个元素进析构template <class ForwardIterator, class T>inline void __destroy(ForwardIterator first, ForwardIterator last, T*) {  typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;  __destroy_aux(first, last, trivial_destructor());}// 没有必要的析构函数不用处理templatetemplate <class ForwardIterator>inline void destroy(ForwardIterator first, ForwardIterator last) {  __destroy(first, last, value_type(first));}

REF:STL源码剖析

0 0