From Jason Turner

[iterator.primitives]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp4ep4lvqb/{from.md → to.md} +45 -66
tmp/tmp4ep4lvqb/{from.md → to.md} RENAMED
@@ -35,52 +35,56 @@ iterator_traits<Iterator>::reference
35
  iterator_traits<Iterator>::pointer
36
  ```
37
 
38
  may be defined as `void`.
39
 
40
- The template `iterator_traits<Iterator>` is defined as
 
 
 
41
 
42
  ``` cpp
43
- namespace std {
44
- template<class Iterator> struct iterator_traits {
45
- typedef typename Iterator::difference_type difference_type;
46
- typedef typename Iterator::value_type value_type;
47
- typedef typename Iterator::pointer pointer;
48
- typedef typename Iterator::reference reference;
49
- typedef typename Iterator::iterator_category iterator_category;
50
- };
51
- }
52
  ```
53
 
 
 
 
54
  It is specialized for pointers as
55
 
56
  ``` cpp
57
  namespace std {
58
  template<class T> struct iterator_traits<T*> {
59
- typedef ptrdiff_t difference_type;
60
- typedef T value_type;
61
- typedef T* pointer;
62
- typedef T& reference;
63
- typedef random_access_iterator_tag iterator_category;
64
  };
65
  }
66
  ```
67
 
68
  and for pointers to const as
69
 
70
  ``` cpp
71
  namespace std {
72
  template<class T> struct iterator_traits<const T*> {
73
- typedef ptrdiff_t difference_type;
74
- typedef T value_type;
75
- typedef const T* pointer;
76
- typedef const T& reference;
77
- typedef random_access_iterator_tag iterator_category;
78
  };
79
  }
80
  ```
81
 
 
 
82
  To implement a generic `reverse` function, a C++program can do the
83
  following:
84
 
85
  ``` cpp
86
  template <class BidirectionalIterator>
@@ -96,28 +100,11 @@ void reverse(BidirectionalIterator first, BidirectionalIterator last) {
96
  n -= 2;
97
  }
98
  }
99
  ```
100
 
101
- ### Basic iterator <a id="iterator.basic">[[iterator.basic]]</a>
102
-
103
- The `iterator` template may be used as a base class to ease the
104
- definition of required types for new iterators.
105
-
106
- ``` cpp
107
- namespace std {
108
- template<class Category, class T, class Distance = ptrdiff_t,
109
- class Pointer = T*, class Reference = T&>
110
- struct iterator {
111
- typedef T value_type;
112
- typedef Distance difference_type;
113
- typedef Pointer pointer;
114
- typedef Reference reference;
115
- typedef Category iterator_category;
116
- };
117
- }
118
- ```
119
 
120
  ### Standard iterator tags <a id="std.iterator.tags">[[std.iterator.tags]]</a>
121
 
122
  It is often desirable for a function template specialization to find out
123
  what is the most specific category of its iterator argument, so that the
@@ -138,26 +125,29 @@ namespace std {
138
  struct bidirectional_iterator_tag: public forward_iterator_tag { };
139
  struct random_access_iterator_tag: public bidirectional_iterator_tag { };
140
  }
141
  ```
142
 
 
 
143
  For a program-defined iterator `BinaryTreeIterator`, it could be
144
  included into the bidirectional iterator category by specializing the
145
  `iterator_traits` template:
146
 
147
  ``` cpp
148
  template<class T> struct iterator_traits<BinaryTreeIterator<T>> {
149
- typedef std::ptrdiff_t difference_type;
150
- typedef T value_type;
151
- typedef T* pointer;
152
- typedef T& reference;
153
- typedef bidirectional_iterator_tag iterator_category;
154
  };
155
  ```
156
 
157
- Typically, however, it would be easier to derive `BinaryTreeIterator<T>`
158
- from `iterator<bidirectional_iterator_tag,T,ptrdiff_t,T*,T&>`.
 
159
 
160
  If `evolve()` is well defined for bidirectional iterators, but can be
161
  implemented more efficiently for random access iterators, then the
162
  implementation is as follows:
163
 
@@ -180,22 +170,11 @@ void evolve(RandomAccessIterator first, RandomAccessIterator last,
180
  random_access_iterator_tag) {
181
  // more efficient, but less generic algorithm
182
  }
183
  ```
184
 
185
- If a C++program wants to define a bidirectional iterator for some data
186
- structure containing `double` and such that it works on a large memory
187
- model of the implementation, it can do so with:
188
-
189
- ``` cpp
190
- class MyIterator :
191
- public iterator<bidirectional_iterator_tag, double, long, T*, T&> {
192
- // code implementing ++, etc.
193
- };
194
- ```
195
-
196
- Then there is no need to specialize the `iterator_traits` template.
197
 
198
  ### Iterator operations <a id="iterator.operations">[[iterator.operations]]</a>
199
 
200
  Since only random access iterators provide `+` and `-` operators, the
201
  library provides two function templates `advance` and `distance`. These
@@ -203,22 +182,22 @@ function templates use `+` and `-` for random access iterators (and are,
203
  therefore, constant time for them); for input, forward and bidirectional
204
  iterators they use `++` to provide linear time implementations.
205
 
206
  ``` cpp
207
  template <class InputIterator, class Distance>
208
- void advance(InputIterator& i, Distance n);
209
  ```
210
 
211
  *Requires:* `n` shall be negative only for bidirectional and random
212
  access iterators.
213
 
214
  *Effects:* Increments (or decrements for negative `n`) iterator
215
  reference `i` by `n`.
216
 
217
  ``` cpp
218
  template <class InputIterator>
219
- typename iterator_traits<InputIterator>::difference_type
220
  distance(InputIterator first, InputIterator last);
221
  ```
222
 
223
  *Effects:* If `InputIterator` meets the requirements of random access
224
  iterator, returns `(last - first)`; otherwise, returns the number of
@@ -228,20 +207,20 @@ increments needed to get from `first` to `last`.
228
  iterator, `last` shall be reachable from `first` or `first` shall be
229
  reachable from `last`; otherwise, `last` shall be reachable from
230
  `first`.
231
 
232
  ``` cpp
233
- template <class ForwardIterator>
234
- ForwardIterator next(ForwardIterator x,
235
- typename std::iterator_traits<ForwardIterator>::difference_type n = 1);
236
  ```
237
 
238
- *Effects:* Equivalent to `advance(x, n); return x;`
239
 
240
  ``` cpp
241
  template <class BidirectionalIterator>
242
- BidirectionalIterator prev(BidirectionalIterator x,
243
- typename std::iterator_traits<BidirectionalIterator>::difference_type n = 1);
244
  ```
245
 
246
- *Effects:* Equivalent to `advance(x, -n); return x;`
247
 
 
35
  iterator_traits<Iterator>::pointer
36
  ```
37
 
38
  may be defined as `void`.
39
 
40
+ If `Iterator` has valid ([[temp.deduct]]) member types
41
+ `difference_type`, `value_type`, `pointer`, `reference`, and
42
+ `iterator_category`, `iterator_traits<Iterator>` shall have the
43
+ following as publicly accessible members:
44
 
45
  ``` cpp
46
+ using difference_type = typename Iterator::difference_type;
47
+ using value_type = typename Iterator::value_type;
48
+ using pointer = typename Iterator::pointer;
49
+ using reference = typename Iterator::reference;
50
+ using iterator_category = typename Iterator::iterator_category;
 
 
 
 
51
  ```
52
 
53
+ Otherwise, `iterator_traits<Iterator>` shall have no members by any of
54
+ the above names.
55
+
56
  It is specialized for pointers as
57
 
58
  ``` cpp
59
  namespace std {
60
  template<class T> struct iterator_traits<T*> {
61
+ using difference_type = ptrdiff_t;
62
+ using value_type = T;
63
+ using pointer = T*;
64
+ using reference = T&;
65
+ using iterator_category = random_access_iterator_tag;
66
  };
67
  }
68
  ```
69
 
70
  and for pointers to const as
71
 
72
  ``` cpp
73
  namespace std {
74
  template<class T> struct iterator_traits<const T*> {
75
+ using difference_type = ptrdiff_t;
76
+ using value_type = T;
77
+ using pointer = const T*;
78
+ using reference = const T&;
79
+ using iterator_category = random_access_iterator_tag;
80
  };
81
  }
82
  ```
83
 
84
+ [*Example 1*:
85
+
86
  To implement a generic `reverse` function, a C++program can do the
87
  following:
88
 
89
  ``` cpp
90
  template <class BidirectionalIterator>
 
100
  n -= 2;
101
  }
102
  }
103
  ```
104
 
105
+ *end example*]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  ### Standard iterator tags <a id="std.iterator.tags">[[std.iterator.tags]]</a>
108
 
109
  It is often desirable for a function template specialization to find out
110
  what is the most specific category of its iterator argument, so that the
 
125
  struct bidirectional_iterator_tag: public forward_iterator_tag { };
126
  struct random_access_iterator_tag: public bidirectional_iterator_tag { };
127
  }
128
  ```
129
 
130
+ [*Example 1*:
131
+
132
  For a program-defined iterator `BinaryTreeIterator`, it could be
133
  included into the bidirectional iterator category by specializing the
134
  `iterator_traits` template:
135
 
136
  ``` cpp
137
  template<class T> struct iterator_traits<BinaryTreeIterator<T>> {
138
+ using iterator_category = bidirectional_iterator_tag;
139
+ using difference_type = ptrdiff_t;
140
+ using value_type = T;
141
+ using pointer = T*;
142
+ using reference = T&;
143
  };
144
  ```
145
 
146
+ *end example*]
147
+
148
+ [*Example 2*:
149
 
150
  If `evolve()` is well defined for bidirectional iterators, but can be
151
  implemented more efficiently for random access iterators, then the
152
  implementation is as follows:
153
 
 
170
  random_access_iterator_tag) {
171
  // more efficient, but less generic algorithm
172
  }
173
  ```
174
 
175
+ *end example*]
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  ### Iterator operations <a id="iterator.operations">[[iterator.operations]]</a>
178
 
179
  Since only random access iterators provide `+` and `-` operators, the
180
  library provides two function templates `advance` and `distance`. These
 
182
  therefore, constant time for them); for input, forward and bidirectional
183
  iterators they use `++` to provide linear time implementations.
184
 
185
  ``` cpp
186
  template <class InputIterator, class Distance>
187
+ constexpr void advance(InputIterator& i, Distance n);
188
  ```
189
 
190
  *Requires:* `n` shall be negative only for bidirectional and random
191
  access iterators.
192
 
193
  *Effects:* Increments (or decrements for negative `n`) iterator
194
  reference `i` by `n`.
195
 
196
  ``` cpp
197
  template <class InputIterator>
198
+ constexpr typename iterator_traits<InputIterator>::difference_type
199
  distance(InputIterator first, InputIterator last);
200
  ```
201
 
202
  *Effects:* If `InputIterator` meets the requirements of random access
203
  iterator, returns `(last - first)`; otherwise, returns the number of
 
207
  iterator, `last` shall be reachable from `first` or `first` shall be
208
  reachable from `last`; otherwise, `last` shall be reachable from
209
  `first`.
210
 
211
  ``` cpp
212
+ template <class InputIterator>
213
+ constexpr InputIterator next(InputIterator x,
214
+ typename iterator_traits<InputIterator>::difference_type n = 1);
215
  ```
216
 
217
+ *Effects:* Equivalent to: `advance(x, n); return x;`
218
 
219
  ``` cpp
220
  template <class BidirectionalIterator>
221
+ constexpr BidirectionalIterator prev(BidirectionalIterator x,
222
+ typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
223
  ```
224
 
225
+ *Effects:* Equivalent to: `advance(x, -n); return x;`
226