1#ifndef __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H
2#define __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H
3
4#pragma GCC system_header
5
6namespace std
7{
8
9 //定义了5种迭代器类型,但是都不含有任何成员,只用来标记迭代器类型。
10 struct input_iterator_tag {};
11 struct output_iterator_tag {};
12 struct forward_iterator_tag : public input_iterator_tag {};
13 struct bidirectional_iterator_tag : public forward_iterator_tag {};
14 struct random_access_iterator_tag : public bidirectional_iterator_tag {};
15
16
17 //stl提供了一个iteraotr class,供每个新设计的迭代器继承它、。
18 //在这个迭代器里只是定义了iterator的五种常用型别,不含任何成员,
19 //所以继承它不会有任何额外负担,而且后三个参数已经有了默认值,故
20 //只需要提供前两个参数就可以了。
21 template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
22 typename _Pointer = _Tp*, typename _Reference = _Tp&>
23 struct iterator
24 {
25 typedef _Category iterator_category;
26 typedef _Tp value_type;
27 typedef _Distance difference_type;
28 typedef _Pointer pointer;
29 typedef _Reference reference;
30 };
31
32 //针对含有non-trivial constructor,non-trivial destructor,non-trivial copy constructor,
33 //non-trivial assigment construstor的类型使用的traits版本。定义了5种常用型别
34 template<typename _Iterator>
35 struct iterator_traits {
36 typedef typename _Iterator::iterator_category iterator_category;
37 typedef typename _Iterator::value_type value_type;
38 typedef typename _Iterator::difference_type difference_type;
39 typedef typename _Iterator::pointer pointer;
40 typedef typename _Iterator::reference reference;
41 };
42
43 //针对原生指针的traits偏特化版本
44 template<typename _Tp>
45 struct iterator_traits<_Tp*> {
46 typedef random_access_iterator_tag iterator_category;
47 typedef _Tp value_type;
48 typedef ptrdiff_t difference_type;
49 typedef _Tp* pointer;
50 typedef _Tp& reference;
51 };
52 //针对原生指针之pointer-to-const版本的traits偏特化版本
53 template<typename _Tp>
54 struct iterator_traits<const _Tp*> {
55 typedef random_access_iterator_tag iterator_category;
56 typedef _Tp value_type;
57 typedef ptrdiff_t difference_type;
58 typedef const _Tp* pointer;
59 typedef const _Tp& reference;
60 };
61
62
63 //返回迭代器类型
64 template<typename _Iter>
65 inline typename iterator_traits<_Iter>::iterator_category
66 __iterator_category(const _Iter&)
67 { return typename iterator_traits<_Iter>::iterator_category(); }
68
69} // namespace std
70
71#endif /* __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H */
72
2#define __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H
3
4#pragma GCC system_header
5
6namespace std
7{
8
9 //定义了5种迭代器类型,但是都不含有任何成员,只用来标记迭代器类型。
10 struct input_iterator_tag {};
11 struct output_iterator_tag {};
12 struct forward_iterator_tag : public input_iterator_tag {};
13 struct bidirectional_iterator_tag : public forward_iterator_tag {};
14 struct random_access_iterator_tag : public bidirectional_iterator_tag {};
15
16
17 //stl提供了一个iteraotr class,供每个新设计的迭代器继承它、。
18 //在这个迭代器里只是定义了iterator的五种常用型别,不含任何成员,
19 //所以继承它不会有任何额外负担,而且后三个参数已经有了默认值,故
20 //只需要提供前两个参数就可以了。
21 template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
22 typename _Pointer = _Tp*, typename _Reference = _Tp&>
23 struct iterator
24 {
25 typedef _Category iterator_category;
26 typedef _Tp value_type;
27 typedef _Distance difference_type;
28 typedef _Pointer pointer;
29 typedef _Reference reference;
30 };
31
32 //针对含有non-trivial constructor,non-trivial destructor,non-trivial copy constructor,
33 //non-trivial assigment construstor的类型使用的traits版本。定义了5种常用型别
34 template<typename _Iterator>
35 struct iterator_traits {
36 typedef typename _Iterator::iterator_category iterator_category;
37 typedef typename _Iterator::value_type value_type;
38 typedef typename _Iterator::difference_type difference_type;
39 typedef typename _Iterator::pointer pointer;
40 typedef typename _Iterator::reference reference;
41 };
42
43 //针对原生指针的traits偏特化版本
44 template<typename _Tp>
45 struct iterator_traits<_Tp*> {
46 typedef random_access_iterator_tag iterator_category;
47 typedef _Tp value_type;
48 typedef ptrdiff_t difference_type;
49 typedef _Tp* pointer;
50 typedef _Tp& reference;
51 };
52 //针对原生指针之pointer-to-const版本的traits偏特化版本
53 template<typename _Tp>
54 struct iterator_traits<const _Tp*> {
55 typedef random_access_iterator_tag iterator_category;
56 typedef _Tp value_type;
57 typedef ptrdiff_t difference_type;
58 typedef const _Tp* pointer;
59 typedef const _Tp& reference;
60 };
61
62
63 //返回迭代器类型
64 template<typename _Iter>
65 inline typename iterator_traits<_Iter>::iterator_category
66 __iterator_category(const _Iter&)
67 { return typename iterator_traits<_Iter>::iterator_category(); }
68
69} // namespace std
70
71#endif /* __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H */
72