From Jason Turner

[iterator.primitives]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpb485e88s/{from.md → to.md} +29 -17
tmp/tmpb485e88s/{from.md → to.md} RENAMED
@@ -1,7 +1,9 @@
1
  ## Iterator primitives <a id="iterator.primitives">[[iterator.primitives]]</a>
2
 
 
 
3
  To simplify the use of iterators, the library provides several classes
4
  and functions.
5
 
6
  ### Standard iterator tags <a id="std.iterator.tags">[[std.iterator.tags]]</a>
7
 
@@ -29,13 +31,13 @@ namespace std {
29
  }
30
  ```
31
 
32
  [*Example 1*:
33
 
34
- For a program-defined iterator `BinaryTreeIterator`, it could be
35
- included into the bidirectional iterator category by specializing the
36
- `iterator_traits` template:
37
 
38
  ``` cpp
39
  template<class T> struct iterator_traits<BinaryTreeIterator<T>> {
40
  using iterator_category = bidirectional_iterator_tag;
41
  using difference_type = ptrdiff_t;
@@ -103,12 +105,12 @@ template<class InputIterator>
103
  *Preconditions:* `last` is reachable from `first`, or `InputIterator`
104
  meets the *Cpp17RandomAccessIterator* requirements and `first` is
105
  reachable from `last`.
106
 
107
  *Effects:* If `InputIterator` meets the *Cpp17RandomAccessIterator*
108
- requirements, returns `(last - first)`; otherwise, returns the number of
109
- increments needed to get from `first` to `last`.
110
 
111
  ``` cpp
112
  template<class InputIterator>
113
  constexpr InputIterator next(InputIterator x,
114
  typename iterator_traits<InputIterator>::difference_type n = 1);
@@ -124,10 +126,12 @@ template<class BidirectionalIterator>
124
 
125
  *Effects:* Equivalent to: `advance(x, -n); return x;`
126
 
127
  ### Range iterator operations <a id="range.iter.ops">[[range.iter.ops]]</a>
128
 
 
 
129
  The library includes the function templates `ranges::advance`,
130
  `ranges::distance`, `ranges::next`, and `ranges::prev` to manipulate
131
  iterators. These operations adapt to the set of operators provided by
132
  each iterator category to provide the most efficient implementation
133
  possible for a concrete iterator type.
@@ -136,11 +140,11 @@ possible for a concrete iterator type.
136
  `random_access_iterator` forward `n` steps in constant time. For an
137
  iterator type that does not model `random_access_iterator`,
138
  `ranges::advance` instead performs `n` individual increments with the
139
  `++` operator. — *end example*]
140
 
141
- The function templates defined in this subclause are not found by
142
  argument-dependent name lookup [[basic.lookup.argdep]]. When found by
143
  unqualified [[basic.lookup.unqual]] name lookup for the
144
  *postfix-expression* in a function call [[expr.call]], they inhibit
145
  argument-dependent name lookup.
146
 
@@ -155,18 +159,18 @@ void foo() {
155
  ```
156
 
157
  The function call expression at `#1` invokes `std::ranges::distance`,
158
  not `std::distance`, despite that (a) the iterator type returned from
159
  `begin(vec)` and `end(vec)` may be associated with namespace `std` and
160
- (b) `std::distance` is more specialized ([[temp.func.order]]) than
161
  `std::ranges::distance` since the former requires its first two
162
  parameters to have the same type.
163
 
164
  — *end example*]
165
 
166
  The number and order of deducible template parameters for the function
167
- templates defined in this subclause is unspecified, except where
168
  explicitly stated otherwise.
169
 
170
  #### `ranges::advance` <a id="range.iter.op.advance">[[range.iter.op.advance]]</a>
171
 
172
  ``` cpp
@@ -186,11 +190,13 @@ not negative.
186
  ``` cpp
187
  template<input_or_output_iterator I, sentinel_for<I> S>
188
  constexpr void ranges::advance(I& i, S bound);
189
  ```
190
 
191
- *Preconditions:* \[`i`, `bound`) denotes a range.
 
 
192
 
193
  *Effects:*
194
 
195
  - If `I` and `S` model `assignable_from<I&, S>`, equivalent to
196
  `i = std::move(bound)`.
@@ -223,21 +229,27 @@ template<input_or_output_iterator I, sentinel_for<I> S>
223
  starting positions of `i`.
224
 
225
  #### `ranges::distance` <a id="range.iter.op.distance">[[range.iter.op.distance]]</a>
226
 
227
  ``` cpp
228
- template<input_or_output_iterator I, sentinel_for<I> S>
 
229
  constexpr iter_difference_t<I> ranges::distance(I first, S last);
230
  ```
231
 
232
- *Preconditions:* \[`first`, `last`) denotes a range, or \[`last`,
233
- `first`) denotes a range and `S` and `I` model
234
- `same_as<S, I> && sized_sentinel_for<S, I>`.
235
 
236
- *Effects:* If `S` and `I` model `sized_sentinel_for<S, I>`, returns
237
- `(last - first)`; otherwise, returns the number of increments needed to
238
- get from `first` to `last`.
 
 
 
 
 
 
 
239
 
240
  ``` cpp
241
  template<range R>
242
  constexpr range_difference_t<R> ranges::distance(R&& r);
243
  ```
@@ -289,11 +301,11 @@ template<input_or_output_iterator I, sentinel_for<I> S>
289
  ``` cpp
290
  template<bidirectional_iterator I>
291
  constexpr I ranges::prev(I x);
292
  ```
293
 
294
- *Effects:* Equivalent to: `-``-``x; return x;`
295
 
296
  ``` cpp
297
  template<bidirectional_iterator I>
298
  constexpr I ranges::prev(I x, iter_difference_t<I> n);
299
  ```
 
1
  ## Iterator primitives <a id="iterator.primitives">[[iterator.primitives]]</a>
2
 
3
+ ### General <a id="iterator.primitives.general">[[iterator.primitives.general]]</a>
4
+
5
  To simplify the use of iterators, the library provides several classes
6
  and functions.
7
 
8
  ### Standard iterator tags <a id="std.iterator.tags">[[std.iterator.tags]]</a>
9
 
 
31
  }
32
  ```
33
 
34
  [*Example 1*:
35
 
36
+ A program-defined iterator `BinaryTreeIterator` can be included into the
37
+ bidirectional iterator category by specializing the `iterator_traits`
38
+ template:
39
 
40
  ``` cpp
41
  template<class T> struct iterator_traits<BinaryTreeIterator<T>> {
42
  using iterator_category = bidirectional_iterator_tag;
43
  using difference_type = ptrdiff_t;
 
105
  *Preconditions:* `last` is reachable from `first`, or `InputIterator`
106
  meets the *Cpp17RandomAccessIterator* requirements and `first` is
107
  reachable from `last`.
108
 
109
  *Effects:* If `InputIterator` meets the *Cpp17RandomAccessIterator*
110
+ requirements, returns `(last - first)`; otherwise, increments `first`
111
+ until `last` is reached and returns the number of increments.
112
 
113
  ``` cpp
114
  template<class InputIterator>
115
  constexpr InputIterator next(InputIterator x,
116
  typename iterator_traits<InputIterator>::difference_type n = 1);
 
126
 
127
  *Effects:* Equivalent to: `advance(x, -n); return x;`
128
 
129
  ### Range iterator operations <a id="range.iter.ops">[[range.iter.ops]]</a>
130
 
131
+ #### General <a id="range.iter.ops.general">[[range.iter.ops.general]]</a>
132
+
133
  The library includes the function templates `ranges::advance`,
134
  `ranges::distance`, `ranges::next`, and `ranges::prev` to manipulate
135
  iterators. These operations adapt to the set of operators provided by
136
  each iterator category to provide the most efficient implementation
137
  possible for a concrete iterator type.
 
140
  `random_access_iterator` forward `n` steps in constant time. For an
141
  iterator type that does not model `random_access_iterator`,
142
  `ranges::advance` instead performs `n` individual increments with the
143
  `++` operator. — *end example*]
144
 
145
+ The function templates defined in [[range.iter.ops]] are not found by
146
  argument-dependent name lookup [[basic.lookup.argdep]]. When found by
147
  unqualified [[basic.lookup.unqual]] name lookup for the
148
  *postfix-expression* in a function call [[expr.call]], they inhibit
149
  argument-dependent name lookup.
150
 
 
159
  ```
160
 
161
  The function call expression at `#1` invokes `std::ranges::distance`,
162
  not `std::distance`, despite that (a) the iterator type returned from
163
  `begin(vec)` and `end(vec)` may be associated with namespace `std` and
164
+ (b) `std::distance` is more specialized [[temp.func.order]] than
165
  `std::ranges::distance` since the former requires its first two
166
  parameters to have the same type.
167
 
168
  — *end example*]
169
 
170
  The number and order of deducible template parameters for the function
171
+ templates defined in [[range.iter.ops]] is unspecified, except where
172
  explicitly stated otherwise.
173
 
174
  #### `ranges::advance` <a id="range.iter.op.advance">[[range.iter.op.advance]]</a>
175
 
176
  ``` cpp
 
190
  ``` cpp
191
  template<input_or_output_iterator I, sentinel_for<I> S>
192
  constexpr void ranges::advance(I& i, S bound);
193
  ```
194
 
195
+ *Preconditions:* Either
196
+ `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or
197
+ \[`i`, `bound`) denotes a range.
198
 
199
  *Effects:*
200
 
201
  - If `I` and `S` model `assignable_from<I&, S>`, equivalent to
202
  `i = std::move(bound)`.
 
229
  starting positions of `i`.
230
 
231
  #### `ranges::distance` <a id="range.iter.op.distance">[[range.iter.op.distance]]</a>
232
 
233
  ``` cpp
234
+ template<class I, sentinel_for<I> S>
235
+ requires (!sized_sentinel_for<S, I>)
236
  constexpr iter_difference_t<I> ranges::distance(I first, S last);
237
  ```
238
 
239
+ *Preconditions:* \[`first`, `last`) denotes a range.
 
 
240
 
241
+ *Effects:* Increments `first` until `last` is reached and returns the
242
+ number of increments.
243
+
244
+ ``` cpp
245
+ template<class I, sized_sentinel_for<decay_t<I>> S>
246
+ constexpr iter_difference_t<decay_t<I>> ranges::distance(I&& first, S last);
247
+ ```
248
+
249
+ *Effects:* Equivalent to:
250
+ `return last - static_cast<const decay_t<I>&>(first);`
251
 
252
  ``` cpp
253
  template<range R>
254
  constexpr range_difference_t<R> ranges::distance(R&& r);
255
  ```
 
301
  ``` cpp
302
  template<bidirectional_iterator I>
303
  constexpr I ranges::prev(I x);
304
  ```
305
 
306
+ *Effects:* Equivalent to: `x; return x;`
307
 
308
  ``` cpp
309
  template<bidirectional_iterator I>
310
  constexpr I ranges::prev(I x, iter_difference_t<I> n);
311
  ```