From Jason Turner

[iterator.requirements]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpjapicmag/{from.md → to.md} +1389 -125
tmp/tmpjapicmag/{from.md → to.md} RENAMED
@@ -1,50 +1,62 @@
1
  ## Iterator requirements <a id="iterator.requirements">[[iterator.requirements]]</a>
2
 
3
  ### In general <a id="iterator.requirements.general">[[iterator.requirements.general]]</a>
4
 
5
  Iterators are a generalization of pointers that allow a C++ program to
6
- work with different data structures (containers) in a uniform manner. To
7
- be able to construct template algorithms that work correctly and
8
- efficiently on different types of data structures, the library
9
- formalizes not just the interfaces but also the semantics and complexity
10
- assumptions of iterators. An input iterator `i` supports the expression
11
- `*i`, resulting in a value of some object type `T`, called the *value
12
- type* of the iterator. An output iterator `i` has a non-empty set of
13
- types that are *writable* to the iterator; for each such type `T`, the
14
- expression `*i = o` is valid where `o` is a value of type `T`. An
15
- iterator `i` for which the expression `(*i).m` is well-defined supports
16
- the expression `i->m` with the same semantics as `(*i).m`. For every
17
- iterator type `X` for which equality is defined, there is a
18
- corresponding signed integer type called the *difference type* of the
19
- iterator.
20
 
21
- Since iterators are an abstraction of pointers, their semantics is a
22
  generalization of most of the semantics of pointers in C++. This ensures
23
  that every function template that takes iterators works as well with
24
- regular pointers. This International Standard defines five categories of
25
- iterators, according to the operations defined on them: *input
26
- iterators*, *output iterators*, *forward iterators*, *bidirectional
27
- iterators* and *random access iterators*, as shown in Table 
28
- [[tab:iterators.relations]].
29
 
30
- **Table: Relations among iterator categories** <a id="tab:iterators.relations">[tab:iterators.relations]</a>
31
 
32
- | | | | |
33
- | ----------------- | ------------------------------- | ------------------------- | ------------------------ |
34
- | **Random Access** | $\rightarrow$ **Bidirectional** | $\rightarrow$ **Forward** | $\rightarrow$ **Input** |
35
- | | | | $\rightarrow$ **Output** |
36
 
37
 
38
- Forward iterators satisfy all the requirements of input iterators and
39
- can be used whenever an input iterator is specified; Bidirectional
40
- iterators also satisfy all the requirements of forward iterators and can
41
- be used whenever a forward iterator is specified; Random access
42
- iterators also satisfy all the requirements of bidirectional iterators
43
- and can be used whenever a bidirectional iterator is specified.
44
 
45
- Iterators that further satisfy the requirements of output iterators are
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  called *mutable iterators*. Nonmutable iterators are referred to as
47
  *constant iterators*.
48
 
49
  In addition to the requirements in this subclause, the nested
50
  *typedef-name*s specified in [[iterator.traits]] shall be provided for
@@ -52,24 +64,14 @@ the iterator type.
52
 
53
  [*Note 1*: Either the iterator type must provide the *typedef-name*s
54
  directly (in which case `iterator_traits` pick them up automatically),
55
  or an `iterator_traits` specialization must provide them. — *end note*]
56
 
57
- Iterators that further satisfy the requirement that, for integral values
58
- `n` and dereferenceable iterator values `a` and `(a + n)`, `*(a + n)` is
59
- equivalent to `*(addressof(*a) + n)`, are called *contiguous iterators*.
60
-
61
- [*Note 2*: For example, the type “pointer to `int`” is a contiguous
62
- iterator, but `reverse_iterator<int *>` is not. For a valid iterator
63
- range [`a`,`b`) with dereferenceable `a`, the corresponding range
64
- denoted by pointers is [`addressof(*a)`,`addressof(*a) + (b - a)`); `b`
65
- might not be dereferenceable. — *end note*]
66
-
67
  Just as a regular pointer to an array guarantees that there is a pointer
68
  value pointing past the last element of the array, so for any iterator
69
  type there is an iterator value that points past the last element of a
70
- corresponding sequence. These values are called *past-the-end* values.
71
  Values of an iterator `i` for which the expression `*i` is defined are
72
  called *dereferenceable*. The library never assumes that past-the-end
73
  values are dereferenceable. Iterators can also have singular values that
74
  are not associated with any sequence.
75
 
@@ -78,135 +80,1123 @@ with `int* x;`), `x` must always be assumed to have a singular value of
78
  a pointer. — *end example*]
79
 
80
  Results of most expressions are undefined for singular values; the only
81
  exceptions are destroying an iterator that holds a singular value, the
82
  assignment of a non-singular value to an iterator that holds a singular
83
- value, and, for iterators that satisfy the `DefaultConstructible`
84
  requirements, using a value-initialized iterator as the source of a copy
85
  or move operation.
86
 
87
- [*Note 3*: This guarantee is not offered for default-initialization,
88
  although the distinction only matters for types with trivial default
89
  constructors such as pointers or aggregates holding
90
  pointers. — *end note*]
91
 
92
  In these cases the singular value is overwritten the same way as any
93
  other value. Dereferenceable values are always non-singular.
94
 
95
- An iterator `j` is called *reachable* from an iterator `i` if and only
96
- if there is a finite sequence of applications of the expression `++i`
97
- that makes `i == j`. If `j` is reachable from `i`, they refer to
98
- elements of the same sequence.
99
-
100
  Most of the library’s algorithmic templates that operate on data
101
- structures have interfaces that use ranges. A *range* is a pair of
102
- iterators that designate the beginning and end of the computation. A
103
- range \[`i`, `i`) is an empty range; in general, a range \[`i`, `j`)
104
- refers to the elements in the data structure starting with the element
105
- pointed to by `i` and up to but not including the element pointed to by
106
- `j`. Range \[`i`, `j`) is valid if and only if `j` is reachable from
107
- `i`. The result of the application of functions in the library to
108
- invalid ranges is undefined.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  All the categories of iterators require only those functions that are
111
  realizable for a given category in constant time (amortized). Therefore,
112
- requirement tables for the iterators do not have a complexity column.
 
113
 
114
- Destruction of an iterator may invalidate pointers and references
115
- previously obtained from that iterator.
116
 
117
- An *invalid* iterator is an iterator that may be singular.[^1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  In the following sections, `a` and `b` denote values of type `X` or
120
  `const X`, `difference_type` and `reference` refer to the types
121
  `iterator_traits<X>::difference_type` and
122
  `iterator_traits<X>::reference`, respectively, `n` denotes a value of
123
  `difference_type`, `u`, `tmp`, and `m` denote identifiers, `r` denotes a
124
  value of `X&`, `t` denotes a value of value type `T`, `o` denotes a
125
  value of some type that is writable to the output iterator.
126
 
127
- [*Note 4*: For an iterator type `X` there must be an instantiation of
128
- `iterator_traits<X>` ([[iterator.traits]]). — *end note*]
129
 
130
- ### Iterator <a id="iterator.iterators">[[iterator.iterators]]</a>
131
 
132
- The `Iterator` requirements form the basis of the iterator concept
133
- taxonomy; every iterator satisfies the `Iterator` requirements. This set
134
- of requirements specifies operations for dereferencing and incrementing
135
- an iterator. Most algorithms will require additional operations to
136
- read ([[input.iterators]]) or write ([[output.iterators]]) values, or
137
- to provide a richer set of iterator movements ([[forward.iterators]],
138
- [[bidirectional.iterators]], [[random.access.iterators]]).
 
139
 
140
- A type `X` satisfies the `Iterator` requirements if:
141
 
142
- - `X` satisfies the `CopyConstructible`, `CopyAssignable`, and
143
- `Destructible` requirements ([[utility.arg.requirements]]) and
144
- lvalues of type `X` are swappable ([[swappable.requirements]]), and
145
- - the expressions in Table  [[tab:iterator.requirements]] are valid and
146
- have the indicated semantics.
147
-
148
- ### Input iterators <a id="input.iterators">[[input.iterators]]</a>
149
-
150
- A class or pointer type `X` satisfies the requirements of an input
151
- iterator for the value type `T` if `X` satisfies the `Iterator` (
152
- [[iterator.iterators]]) and `EqualityComparable` (Table 
153
- [[tab:equalitycomparable]]) requirements and the expressions in Table 
154
- [[tab:iterator.input.requirements]] are valid and have the indicated
155
  semantics.
156
 
157
- In Table  [[tab:iterator.input.requirements]], the term *the domain of
158
- `==`* is used in the ordinary mathematical sense to denote the set of
159
- values over which `==` is (required to be) defined. This set can change
160
- over time. Each algorithm places additional requirements on the domain
161
- of `==` for the iterator values it uses. These requirements can be
162
- inferred from the uses that algorithm makes of `==` and `!=`.
 
 
 
 
 
 
 
 
163
 
164
  [*Example 1*: The call `find(a,b,x)` is defined only if the value of
165
  `a` has the property *p* defined as follows: `b` has property *p* and a
166
  value `i` has property *p* if (`*i==x`) or if (`*i!=x` and `++i` has
167
  property *p*). — *end example*]
168
 
169
  [*Note 1*: For input iterators, `a == b` does not imply `++a == ++b`.
170
  (Equality does not guarantee the substitution property or referential
171
  transparency.) Algorithms on input iterators should never attempt to
172
  pass through the same iterator twice. They should be *single pass*
173
- algorithms. Value type `T` is not required to be a `CopyAssignable` type
174
- (Table  [[tab:copyassignable]]). These algorithms can be used with
175
  istreams as the source of the input data through the `istream_iterator`
176
  class template. — *end note*]
177
 
178
- ### Output iterators <a id="output.iterators">[[output.iterators]]</a>
179
 
180
- A class or pointer type `X` satisfies the requirements of an output
181
- iterator if `X` satisfies the `Iterator` requirements (
182
- [[iterator.iterators]]) and the expressions in Table 
183
- [[tab:iterator.output.requirements]] are valid and have the indicated
184
  semantics.
185
 
186
  [*Note 1*: The only valid use of an `operator*` is on the left side of
187
- the assignment statement. *Assignment through the same value of the
188
- iterator happens only once.* Algorithms on output iterators should never
189
- attempt to pass through the same iterator twice. They should be *single
190
- pass* algorithms. Equality and inequality might not be defined.
191
- Algorithms that take output iterators can be used with ostreams as the
192
- destination for placing data through the `ostream_iterator` class as
193
- well as with insert iterators and insert pointers. — *end note*]
194
 
195
- ### Forward iterators <a id="forward.iterators">[[forward.iterators]]</a>
196
 
197
- A class or pointer type `X` satisfies the requirements of a forward
198
- iterator if
199
 
200
- - `X` satisfies the requirements of an input iterator (
201
- [[input.iterators]]),
202
- - `X` satisfies the `DefaultConstructible` requirements (
203
- [[utility.arg.requirements]]),
204
  - if `X` is a mutable iterator, `reference` is a reference to `T`; if
205
  `X` is a constant iterator, `reference` is a reference to `const T`,
206
- - the expressions in Table  [[tab:iterator.forward.requirements]] are
207
- valid and have the indicated semantics, and
208
  - objects of type `X` offer the multi-pass guarantee, described below.
209
 
210
  The domain of `==` for forward iterators is that of iterators over the
211
  same underlying sequence. However, value-initialized iterators may be
212
  compared and shall compare equal to other value-initialized iterators of
@@ -232,22 +1222,296 @@ If `a` and `b` are equal, then either `a` and `b` are both
232
  dereferenceable or else neither is dereferenceable.
233
 
234
  If `a` and `b` are both dereferenceable, then `a == b` if and only if
235
  `*a` and `*b` are bound to the same object.
236
 
237
- ### Bidirectional iterators <a id="bidirectional.iterators">[[bidirectional.iterators]]</a>
238
 
239
- A class or pointer type `X` satisfies the requirements of a
240
- bidirectional iterator if, in addition to satisfying the requirements
241
- for forward iterators, the following expressions are valid as shown in
242
- Table  [[tab:iterator.bidirectional.requirements]].
243
 
244
  [*Note 1*: Bidirectional iterators allow algorithms to move iterators
245
  backward as well as forward. — *end note*]
246
 
247
- ### Random access iterators <a id="random.access.iterators">[[random.access.iterators]]</a>
248
 
249
- A class or pointer type `X` satisfies the requirements of a random
250
- access iterator if, in addition to satisfying the requirements for
251
- bidirectional iterators, the following expressions are valid as shown in
252
- Table  [[tab:iterator.random.access.requirements]].
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
 
1
  ## Iterator requirements <a id="iterator.requirements">[[iterator.requirements]]</a>
2
 
3
  ### In general <a id="iterator.requirements.general">[[iterator.requirements.general]]</a>
4
 
5
  Iterators are a generalization of pointers that allow a C++ program to
6
+ work with different data structures (for example, containers and ranges)
7
+ in a uniform manner. To be able to construct template algorithms that
8
+ work correctly and efficiently on different types of data structures,
9
+ the library formalizes not just the interfaces but also the semantics
10
+ and complexity assumptions of iterators. An input iterator `i` supports
11
+ the expression `*i`, resulting in a value of some object type `T`,
12
+ called the *value type* of the iterator. An output iterator `i` has a
13
+ non-empty set of types that are `indirectly_writable` to the iterator;
14
+ for each such type `T`, the expression `*i = o` is valid where `o` is a
15
+ value of type `T`. For every iterator type `X`, there is a corresponding
16
+ signed integer-like type [[iterator.concept.winc]] called the
17
+ *difference type* of the iterator.
 
 
18
 
19
+ Since iterators are an abstraction of pointers, their semantics are a
20
  generalization of most of the semantics of pointers in C++. This ensures
21
  that every function template that takes iterators works as well with
22
+ regular pointers. This document defines six categories of iterators,
23
+ according to the operations defined on them: *input iterators*, *output
24
+ iterators*, *forward iterators*, *bidirectional iterators*, *random
25
+ access iterators*, and *contiguous iterators*, as shown in
26
+ [[iterators.relations]].
27
 
28
+ **Table: Relations among iterator categories** <a id="iterators.relations">[iterators.relations]</a>
29
 
30
+ | | | | | |
31
+ | -------------- | ------------------------------- | ------------------------------- | ------------------------- | ------------------------ |
32
+ | **Contiguous** | $\rightarrow$ **Random Access** | $\rightarrow$ **Bidirectional** | $\rightarrow$ **Forward** | $\rightarrow$ **Input** |
33
+ | | | | | $\rightarrow$ **Output** |
34
 
35
 
36
+ The six categories of iterators correspond to the iterator concepts
 
 
 
 
 
37
 
38
+ - `input_iterator` [[iterator.concept.input]],
39
+ - `output_iterator` [[iterator.concept.output]],
40
+ - `forward_iterator` [[iterator.concept.forward]],
41
+ - `bidirectional_iterator` [[iterator.concept.bidir]]
42
+ - `random_access_iterator` [[iterator.concept.random.access]], and
43
+ - `contiguous_iterator` [[iterator.concept.contiguous]],
44
+
45
+ respectively. The generic term *iterator* refers to any type that models
46
+ the `input_or_output_iterator` concept [[iterator.concept.iterator]].
47
+
48
+ Forward iterators meet all the requirements of input iterators and can
49
+ be used whenever an input iterator is specified; Bidirectional iterators
50
+ also meet all the requirements of forward iterators and can be used
51
+ whenever a forward iterator is specified; Random access iterators also
52
+ meet all the requirements of bidirectional iterators and can be used
53
+ whenever a bidirectional iterator is specified; Contiguous iterators
54
+ also meet all the requirements of random access iterators and can be
55
+ used whenever a random access iterator is specified.
56
+
57
+ Iterators that further meet the requirements of output iterators are
58
  called *mutable iterators*. Nonmutable iterators are referred to as
59
  *constant iterators*.
60
 
61
  In addition to the requirements in this subclause, the nested
62
  *typedef-name*s specified in [[iterator.traits]] shall be provided for
 
64
 
65
  [*Note 1*: Either the iterator type must provide the *typedef-name*s
66
  directly (in which case `iterator_traits` pick them up automatically),
67
  or an `iterator_traits` specialization must provide them. — *end note*]
68
 
 
 
 
 
 
 
 
 
 
 
69
  Just as a regular pointer to an array guarantees that there is a pointer
70
  value pointing past the last element of the array, so for any iterator
71
  type there is an iterator value that points past the last element of a
72
+ corresponding sequence. Such a value is called a *past-the-end value*.
73
  Values of an iterator `i` for which the expression `*i` is defined are
74
  called *dereferenceable*. The library never assumes that past-the-end
75
  values are dereferenceable. Iterators can also have singular values that
76
  are not associated with any sequence.
77
 
 
80
  a pointer. — *end example*]
81
 
82
  Results of most expressions are undefined for singular values; the only
83
  exceptions are destroying an iterator that holds a singular value, the
84
  assignment of a non-singular value to an iterator that holds a singular
85
+ value, and, for iterators that meet the *Cpp17DefaultConstructible*
86
  requirements, using a value-initialized iterator as the source of a copy
87
  or move operation.
88
 
89
+ [*Note 2*: This guarantee is not offered for default-initialization,
90
  although the distinction only matters for types with trivial default
91
  constructors such as pointers or aggregates holding
92
  pointers. — *end note*]
93
 
94
  In these cases the singular value is overwritten the same way as any
95
  other value. Dereferenceable values are always non-singular.
96
 
 
 
 
 
 
97
  Most of the library’s algorithmic templates that operate on data
98
+ structures have interfaces that use ranges. A *range* is an iterator and
99
+ a *sentinel* that designate the beginning and end of the computation, or
100
+ an iterator and a count that designate the beginning and the number of
101
+ elements to which the computation is to be applied.[^1]
102
+
103
+ An iterator and a sentinel denoting a range are comparable. A range
104
+ \[`i`, `s`) is empty if `i == s`; otherwise, \[`i`, `s`) refers to the
105
+ elements in the data structure starting with the element pointed to by
106
+ `i` and up to but not including the element, if any, pointed to by the
107
+ first iterator `j` such that `j == s`.
108
+
109
+ A sentinel `s` is called *reachable from* an iterator `i` if and only if
110
+ there is a finite sequence of applications of the expression `++i` that
111
+ makes `i == s`. If `s` is reachable from `i`, \[`i`, `s`) denotes a
112
+ valid range.
113
+
114
+ A *counted range* `i`+\[0, `n`) is empty if `n == 0`; otherwise,
115
+ `i`+\[0, `n`) refers to the `n` elements in the data structure starting
116
+ with the element pointed to by `i` and up to but not including the
117
+ element, if any, pointed to by the result of `n` applications of `++i`.
118
+ A counted range `i`+\[0, `n`) is valid if and only if `n == 0`; or `n`
119
+ is positive, `i` is dereferenceable, and `++i`+\[0, `-``-``n`) is valid.
120
+
121
+ The result of the application of library functions to invalid ranges is
122
+ undefined.
123
 
124
  All the categories of iterators require only those functions that are
125
  realizable for a given category in constant time (amortized). Therefore,
126
+ requirement tables and concept definitions for the iterators do not
127
+ specify complexity.
128
 
129
+ Destruction of a non-forward iterator may invalidate pointers and
130
+ references previously obtained from that iterator.
131
 
132
+ An *invalid iterator* is an iterator that may be singular.[^2]
133
+
134
+ Iterators are called *constexpr iterators* if all operations provided to
135
+ meet iterator category requirements are constexpr functions.
136
+
137
+ [*Note 3*: For example, the types “pointer to `int`” and
138
+ `reverse_iterator<int*>` are constexpr iterators. — *end note*]
139
+
140
+ ### Associated types <a id="iterator.assoc.types">[[iterator.assoc.types]]</a>
141
+
142
+ #### Incrementable traits <a id="incrementable.traits">[[incrementable.traits]]</a>
143
+
144
+ To implement algorithms only in terms of incrementable types, it is
145
+ often necessary to determine the difference type that corresponds to a
146
+ particular incrementable type. Accordingly, it is required that if `WI`
147
+ is the name of a type that models the `weakly_incrementable` concept
148
+ [[iterator.concept.winc]], the type
149
+
150
+ ``` cpp
151
+ iter_difference_t<WI>
152
+ ```
153
+
154
+ be defined as the incrementable type’s difference type.
155
+
156
+ ``` cpp
157
+ namespace std {
158
+ template<class> struct incrementable_traits { };
159
+
160
+ template<class T>
161
+ requires is_object_v<T>
162
+ struct incrementable_traits<T*> {
163
+ using difference_type = ptrdiff_t;
164
+ };
165
+
166
+ template<class I>
167
+ struct incrementable_traits<const I>
168
+ : incrementable_traits<I> { };
169
+
170
+ template<class T>
171
+ requires requires { typename T::difference_type; }
172
+ struct incrementable_traits<T> {
173
+ using difference_type = typename T::difference_type;
174
+ };
175
+
176
+ template<class T>
177
+ requires (!requires { typename T::difference_type; } &&
178
+ requires(const T& a, const T& b) { { a - b } -> integral; })
179
+ struct incrementable_traits<T> {
180
+ using difference_type = make_signed_t<decltype(declval<T>() - declval<T>())>;
181
+ };
182
+
183
+ template<class T>
184
+ using iter_difference_t = see below;
185
+ }
186
+ ```
187
+
188
+ Let R_`I` be `remove_cvref_t<I>`. The type `iter_difference_t<I>`
189
+ denotes
190
+
191
+ - `incrementable_traits<R_I>::difference_type` if `iterator_traits<R_I>`
192
+ names a specialization generated from the primary template, and
193
+ - `iterator_traits<R_I>::difference_type` otherwise.
194
+
195
+ Users may specialize `incrementable_traits` on program-defined types.
196
+
197
+ #### Indirectly readable traits <a id="readable.traits">[[readable.traits]]</a>
198
+
199
+ To implement algorithms only in terms of indirectly readable types, it
200
+ is often necessary to determine the value type that corresponds to a
201
+ particular indirectly readable type. Accordingly, it is required that if
202
+ `R` is the name of a type that models the `indirectly_readable` concept
203
+ [[iterator.concept.readable]], the type
204
+
205
+ ``` cpp
206
+ iter_value_t<R>
207
+ ```
208
+
209
+ be defined as the indirectly readable type’s value type.
210
+
211
+ ``` cpp
212
+ template<class> struct cond-value-type { }; // exposition only
213
+ template<class T>
214
+ requires is_object_v<T>
215
+ struct cond-value-type<T> {
216
+ using value_type = remove_cv_t<T>;
217
+ };
218
+
219
+ template<class> struct indirectly_readable_traits { };
220
+
221
+ template<class T>
222
+ struct indirectly_readable_traits<T*>
223
+ : cond-value-type<T> { };
224
+
225
+ template<class I>
226
+ requires is_array_v<I>
227
+ struct indirectly_readable_traits<I> {
228
+ using value_type = remove_cv_t<remove_extent_t<I>>;
229
+ };
230
+
231
+ template<class I>
232
+ struct indirectly_readable_traits<const I>
233
+ : indirectly_readable_traits<I> { };
234
+
235
+ template<class T>
236
+ requires requires { typename T::value_type; }
237
+ struct indirectly_readable_traits<T>
238
+ : cond-value-type<typename T::value_type> { };
239
+
240
+ template<class T>
241
+ requires requires { typename T::element_type; }
242
+ struct indirectly_readable_traits<T>
243
+ : cond-value-type<typename T::element_type> { };
244
+
245
+ template<class T> using iter_value_t = see below;
246
+ ```
247
+
248
+ Let R_`I` be `remove_cvref_t<I>`. The type `iter_value_t<I>` denotes
249
+
250
+ - `indirectly_readable_traits<R_I>::value_type` if
251
+ `iterator_traits<R_I>` names a specialization generated from the
252
+ primary template, and
253
+ - `iterator_traits<R_I>::value_type` otherwise.
254
+
255
+ Class template `indirectly_readable_traits` may be specialized on
256
+ program-defined types.
257
+
258
+ [*Note 1*: Some legacy output iterators define a nested type named
259
+ `value_type` that is an alias for `void`. These types are not
260
+ `indirectly_readable` and have no associated value types. — *end note*]
261
+
262
+ [*Note 2*: Smart pointers like `shared_ptr<int>` are
263
+ `indirectly_readable` and have an associated value type, but a smart
264
+ pointer like `shared_ptr<void>` is not `indirectly_readable` and has no
265
+ associated value type. — *end note*]
266
+
267
+ #### Iterator traits <a id="iterator.traits">[[iterator.traits]]</a>
268
+
269
+ To implement algorithms only in terms of iterators, it is sometimes
270
+ necessary to determine the iterator category that corresponds to a
271
+ particular iterator type. Accordingly, it is required that if `I` is the
272
+ type of an iterator, the type
273
+
274
+ ``` cpp
275
+ iterator_traits<I>::iterator_category
276
+ ```
277
+
278
+ be defined as the iterator’s iterator category. In addition, the types
279
+
280
+ ``` cpp
281
+ iterator_traits<I>::pointer
282
+ iterator_traits<I>::reference
283
+ ```
284
+
285
+ shall be defined as the iterator’s pointer and reference types; that is,
286
+ for an iterator object `a` of class type, the same type as
287
+ `decltype(a.operator->())` and `decltype(*a)`, respectively. The type
288
+ `iterator_traits<I>::pointer` shall be `void` for an iterator of class
289
+ type `I` that does not support `operator->`. Additionally, in the case
290
+ of an output iterator, the types
291
+
292
+ ``` cpp
293
+ iterator_traits<I>::value_type
294
+ iterator_traits<I>::difference_type
295
+ iterator_traits<I>::reference
296
+ ```
297
+
298
+ may be defined as `void`.
299
+
300
+ The definitions in this subclause make use of the following
301
+ exposition-only concepts:
302
+
303
+ ``` cpp
304
+ template<class I>
305
+ concept cpp17-iterator =
306
+ copyable<I> && requires(I i) {
307
+ { *i } -> can-reference;
308
+ { ++i } -> same_as<I&>;
309
+ { *i++ } -> can-reference;
310
+ };
311
+
312
+ template<class I>
313
+ concept cpp17-input-iterator =
314
+ cpp17-iterator<I> && equality_comparable<I> && requires(I i) {
315
+ typename incrementable_traits<I>::difference_type;
316
+ typename indirectly_readable_traits<I>::value_type;
317
+ typename common_reference_t<iter_reference_t<I>&&,
318
+ typename indirectly_readable_traits<I>::value_type&>;
319
+ typename common_reference_t<decltype(*i++)&&,
320
+ typename indirectly_readable_traits<I>::value_type&>;
321
+ requires signed_integral<typename incrementable_traits<I>::difference_type>;
322
+ };
323
+
324
+ template<class I>
325
+ concept cpp17-forward-iterator =
326
+ cpp17-input-iterator<I> && constructible_from<I> &&
327
+ is_lvalue_reference_v<iter_reference_t<I>> &&
328
+ same_as<remove_cvref_t<iter_reference_t<I>>,
329
+ typename indirectly_readable_traits<I>::value_type> &&
330
+ requires(I i) {
331
+ { i++ } -> convertible_to<const I&>;
332
+ { *i++ } -> same_as<iter_reference_t<I>>;
333
+ };
334
+
335
+ template<class I>
336
+ concept cpp17-bidirectional-iterator =
337
+ cpp17-forward-iterator<I> && requires(I i) {
338
+ { --i } -> same_as<I&>;
339
+ { i-- } -> convertible_to<const I&>;
340
+ { *i-- } -> same_as<iter_reference_t<I>>;
341
+ };
342
+
343
+ template<class I>
344
+ concept cpp17-random-access-iterator =
345
+ cpp17-bidirectional-iterator<I> && totally_ordered<I> &&
346
+ requires(I i, typename incrementable_traits<I>::difference_type n) {
347
+ { i += n } -> same_as<I&>;
348
+ { i -= n } -> same_as<I&>;
349
+ { i + n } -> same_as<I>;
350
+ { n + i } -> same_as<I>;
351
+ { i - n } -> same_as<I>;
352
+ { i - i } -> same_as<decltype(n)>;
353
+ { i[n] } -> convertible_to<iter_reference_t<I>>;
354
+ };
355
+ ```
356
+
357
+ The members of a specialization `iterator_traits<I>` generated from the
358
+ `iterator_traits` primary template are computed as follows:
359
+
360
+ - If `I` has valid [[temp.deduct]] member types `difference_type`,
361
+ `value_type`, `reference`, and `iterator_category`, then
362
+ `iterator_traits<I>` has the following publicly accessible members:
363
+ ``` cpp
364
+ using iterator_category = typename I::iterator_category;
365
+ using value_type = typename I::value_type;
366
+ using difference_type = typename I::difference_type;
367
+ using pointer = see below;
368
+ using reference = typename I::reference;
369
+ ```
370
+
371
+ If the *qualified-id* `I::pointer` is valid and denotes a type, then
372
+ `iterator_traits<I>::pointer` names that type; otherwise, it names
373
+ `void`.
374
+ - Otherwise, if `I` satisfies the exposition-only concept
375
+ `cpp17-input-iterator`, `iterator_traits<I>` has the following
376
+ publicly accessible members:
377
+ ``` cpp
378
+ using iterator_category = see below;
379
+ using value_type = typename indirectly_readable_traits<I>::value_type;
380
+ using difference_type = typename incrementable_traits<I>::difference_type;
381
+ using pointer = see below;
382
+ using reference = see below;
383
+ ```
384
+
385
+ - If the *qualified-id* `I::pointer` is valid and denotes a type,
386
+ `pointer` names that type. Otherwise, if
387
+ `decltype({}declval<I&>().operator->())` is well-formed, then
388
+ `pointer` names that type. Otherwise, `pointer` names `void`.
389
+ - If the *qualified-id* `I::reference` is valid and denotes a type,
390
+ `reference` names that type. Otherwise, `reference` names
391
+ `iter_reference_t<I>`.
392
+ - If the *qualified-id* `I::iterator_category` is valid and denotes a
393
+ type, `iterator_category` names that type. Otherwise,
394
+ `iterator_category` names:
395
+ - `random_access_iterator_tag` if `I` satisfies
396
+ `cpp17-random-access-iterator`, or otherwise
397
+ - `bidirectional_iterator_tag` if `I` satisfies
398
+ `cpp17-bidirectional-iterator`, or otherwise
399
+ - `forward_iterator_tag` if `I` satisfies `cpp17-forward-iterator`,
400
+ or otherwise
401
+ - `input_iterator_tag`.
402
+ - Otherwise, if `I` satisfies the exposition-only concept
403
+ `cpp17-iterator`, then `iterator_traits<I>` has the following publicly
404
+ accessible members:
405
+ ``` cpp
406
+ using iterator_category = output_iterator_tag;
407
+ using value_type = void;
408
+ using difference_type = see below;
409
+ using pointer = void;
410
+ using reference = void;
411
+ ```
412
+
413
+ If the *qualified-id* `incrementable_traits<I>::difference_type` is
414
+ valid and denotes a type, then `difference_type` names that type;
415
+ otherwise, it names `void`.
416
+ - Otherwise, `iterator_traits<I>` has no members by any of the above
417
+ names.
418
+
419
+ Explicit or partial specializations of `iterator_traits` may have a
420
+ member type `iterator_concept` that is used to indicate conformance to
421
+ the iterator concepts [[iterator.concepts]].
422
+
423
+ `iterator_traits` is specialized for pointers as
424
+
425
+ ``` cpp
426
+ namespace std {
427
+ template<class T>
428
+ requires is_object_v<T>
429
+ struct iterator_traits<T*> {
430
+ using iterator_concept = contiguous_iterator_tag;
431
+ using iterator_category = random_access_iterator_tag;
432
+ using value_type = remove_cv_t<T>;
433
+ using difference_type = ptrdiff_t;
434
+ using pointer = T*;
435
+ using reference = T&;
436
+ };
437
+ }
438
+ ```
439
+
440
+ [*Example 1*:
441
+
442
+ To implement a generic `reverse` function, a C++ program can do the
443
+ following:
444
+
445
+ ``` cpp
446
+ template<class BI>
447
+ void reverse(BI first, BI last) {
448
+ typename iterator_traits<BI>::difference_type n =
449
+ distance(first, last);
450
+ --n;
451
+ while(n > 0) {
452
+ typename iterator_traits<BI>::value_type
453
+ tmp = *first;
454
+ *first++ = *--last;
455
+ *last = tmp;
456
+ n -= 2;
457
+ }
458
+ }
459
+ ```
460
+
461
+ — *end example*]
462
+
463
+ ### Customization points <a id="iterator.cust">[[iterator.cust]]</a>
464
+
465
+ #### `ranges::iter_move` <a id="iterator.cust.move">[[iterator.cust.move]]</a>
466
+
467
+ The name `ranges::iter_move` denotes a customization point object
468
+ [[customization.point.object]]. The expression `ranges::iter_move(E)`
469
+ for a subexpression `E` is expression-equivalent to:
470
+
471
+ - `iter_move(E)`, if `E` has class or enumeration type and
472
+ `iter_move(E)` is a well-formed expression when treated as an
473
+ unevaluated operand, with overload resolution performed in a context
474
+ that does not include a declaration of `ranges::iter_move` but does
475
+ include the declaration
476
+ ``` cpp
477
+ void iter_move();
478
+ ```
479
+ - Otherwise, if the expression `*E` is well-formed:
480
+ - if `*E` is an lvalue, `std::move(*E)`;
481
+ - otherwise, `*E`.
482
+ - Otherwise, `ranges::iter_move(E)` is ill-formed. \[*Note 1*: This case
483
+ can result in substitution failure when `ranges::iter_move(E)` appears
484
+ in the immediate context of a template instantiation. — *end note*]
485
+
486
+ If `ranges::iter_move(E)` is not equal to `*E`, the program is
487
+ ill-formed, no diagnostic required.
488
+
489
+ #### `ranges::iter_swap` <a id="iterator.cust.swap">[[iterator.cust.swap]]</a>
490
+
491
+ The name `ranges::iter_swap` denotes a customization point object
492
+ [[customization.point.object]] that exchanges the values
493
+ [[concept.swappable]] denoted by its arguments.
494
+
495
+ Let *`iter-exchange-move`* be the exposition-only function:
496
+
497
+ ``` cpp
498
+ template<class X, class Y>
499
+ constexpr iter_value_t<X> iter-exchange-move(X&& x, Y&& y)
500
+ noexcept(noexcept(iter_value_t<X>(iter_move(x))) &&
501
+ noexcept(*x = iter_move(y)));
502
+ ```
503
+
504
+ *Effects:* Equivalent to:
505
+
506
+ ``` cpp
507
+ iter_value_t<X> old_value(iter_move(x));
508
+ *x = iter_move(y);
509
+ return old_value;
510
+ ```
511
+
512
+ The expression `ranges::iter_swap(E1, E2)` for subexpressions `E1` and
513
+ `E2` is expression-equivalent to:
514
+
515
+ - `(void)iter_swap(E1, E2)`, if either `E1` or `E2` has class or
516
+ enumeration type and `iter_swap(E1, E2)` is a well-formed expression
517
+ with overload resolution performed in a context that includes the
518
+ declaration
519
+ ``` cpp
520
+ template<class I1, class I2>
521
+ void iter_swap(I1, I2) = delete;
522
+ ```
523
+
524
+ and does not include a declaration of `ranges::iter_swap`. If the
525
+ function selected by overload resolution does not exchange the values
526
+ denoted by `E1` and `E2`, the program is ill-formed, no diagnostic
527
+ required.
528
+ - Otherwise, if the types of `E1` and `E2` each model
529
+ `indirectly_readable`, and if the reference types of `E1` and `E2`
530
+ model `swappable_with` [[concept.swappable]], then
531
+ `ranges::swap(*E1, *E2)`.
532
+ - Otherwise, if the types `T1` and `T2` of `E1` and `E2` model
533
+ `indirectly_movable_storable<T1, T2>` and
534
+ `indirectly_movable_storable<T2, T1>`, then
535
+ `(void)(*E1 = iter-exchange-move(E2, E1))`, except that `E1` is
536
+ evaluated only once.
537
+ - Otherwise, `ranges::iter_swap(E1, E2)` is ill-formed. \[*Note 1*: This
538
+ case can result in substitution failure when
539
+ `ranges::iter_swap(E1, E2)` appears in the immediate context of a
540
+ template instantiation. — *end note*]
541
+
542
+ ### Iterator concepts <a id="iterator.concepts">[[iterator.concepts]]</a>
543
+
544
+ #### General <a id="iterator.concepts.general">[[iterator.concepts.general]]</a>
545
+
546
+ For a type `I`, let `ITER_TRAITS(I)` denote the type `I` if
547
+ `iterator_traits<I>` names a specialization generated from the primary
548
+ template. Otherwise, `ITER_TRAITS(I)` denotes `iterator_traits<I>`.
549
+
550
+ - If the *qualified-id* `ITER_TRAITS(I)::iterator_concept` is valid and
551
+ names a type, then `ITER_CONCEPT(I)` denotes that type.
552
+ - Otherwise, if the *qualified-id* `ITER_TRAITS(I){}::iterator_category`
553
+ is valid and names a type, then `ITER_CONCEPT(I)` denotes that type.
554
+ - Otherwise, if `iterator_traits<I>` names a specialization generated
555
+ from the primary template, then `ITER_CONCEPT(I)` denotes
556
+ `random_access_iterator_tag`.
557
+ - Otherwise, `ITER_CONCEPT(I)` does not denote a type.
558
+
559
+ [*Note 1*: `ITER_TRAITS` enables independent syntactic determination of
560
+ an iterator’s category and concept. — *end note*]
561
+
562
+ [*Example 1*:
563
+
564
+ ``` cpp
565
+ struct I {
566
+ using value_type = int;
567
+ using difference_type = int;
568
+
569
+ int operator*() const;
570
+ I& operator++();
571
+ I operator++(int);
572
+ I& operator--();
573
+ I operator--(int);
574
+
575
+ bool operator==(I) const;
576
+ bool operator!=(I) const;
577
+ };
578
+ ```
579
+
580
+ `iterator_traits<I>::iterator_category` denotes `input_iterator_tag`,
581
+ and `ITER_CONCEPT(I)` denotes `random_access_iterator_tag`.
582
+
583
+ — *end example*]
584
+
585
+ #### Concept <a id="iterator.concept.readable">[[iterator.concept.readable]]</a>
586
+
587
+ Types that are indirectly readable by applying `operator*` model the
588
+ `indirectly_readable` concept, including pointers, smart pointers, and
589
+ iterators.
590
+
591
+ ``` cpp
592
+ template<class In>
593
+ concept indirectly-readable-impl =
594
+ requires(const In in) {
595
+ typename iter_value_t<In>;
596
+ typename iter_reference_t<In>;
597
+ typename iter_rvalue_reference_t<In>;
598
+ { *in } -> same_as<iter_reference_t<In>>;
599
+ { ranges::iter_move(in) } -> same_as<iter_rvalue_reference_t<In>>;
600
+ } &&
601
+ common_reference_with<iter_reference_t<In>&&, iter_value_t<In>&> &&
602
+ common_reference_with<iter_reference_t<In>&&, iter_rvalue_reference_t<In>&&> &&
603
+ common_reference_with<iter_rvalue_reference_t<In>&&, const iter_value_t<In>&>;
604
+ ```
605
+
606
+ ``` cpp
607
+ template<class In>
608
+ concept indirectly_readable =
609
+ indirectly-readable-impl<remove_cvref_t<In>>;
610
+ ```
611
+
612
+ Given a value `i` of type `I`, `I` models `indirectly_readable` only if
613
+ the expression `*i` is equality-preserving.
614
+
615
+ [*Note 1*: The expression `*i` is indirectly required to be valid via
616
+ the exposition-only `dereferenceable` concept
617
+ [[iterator.synopsis]]. — *end note*]
618
+
619
+ #### Concept <a id="iterator.concept.writable">[[iterator.concept.writable]]</a>
620
+
621
+ The `indirectly_writable` concept specifies the requirements for writing
622
+ a value into an iterator’s referenced object.
623
+
624
+ ``` cpp
625
+ template<class Out, class T>
626
+ concept indirectly_writable =
627
+ requires(Out&& o, T&& t) {
628
+ *o = std::forward<T>(t); // not required to be equality-preserving
629
+ *std::forward<Out>(o) = std::forward<T>(t); // not required to be equality-preserving
630
+ const_cast<const iter_reference_t<Out>&&>(*o) =
631
+ std::forward<T>(t); // not required to be equality-preserving
632
+ const_cast<const iter_reference_t<Out>&&>(*std::forward<Out>(o)) =
633
+ std::forward<T>(t); // not required to be equality-preserving
634
+ };
635
+ ```
636
+
637
+ Let `E` be an expression such that `decltype((E))` is `T`, and let `o`
638
+ be a dereferenceable object of type `Out`. `Out` and `T` model
639
+ `indirectly_writable<Out, T>` only if
640
+
641
+ - If `Out` and `T` model
642
+ `indirectly_readable<Out> && same_as<iter_value_t<Out>, decay_t<T>{>}`,
643
+ then `*o` after any above assignment is equal to the value of `E`
644
+ before the assignment.
645
+
646
+ After evaluating any above assignment expression, `o` is not required to
647
+ be dereferenceable.
648
+
649
+ If `E` is an xvalue [[basic.lval]], the resulting state of the object it
650
+ denotes is valid but unspecified [[lib.types.movedfrom]].
651
+
652
+ [*Note 1*: The only valid use of an `operator*` is on the left side of
653
+ the assignment statement. Assignment through the same value of the
654
+ indirectly writable type happens only once. — *end note*]
655
+
656
+ [*Note 2*: `indirectly_writable` has the awkward `const_cast`
657
+ expressions to reject iterators with prvalue non-proxy reference types
658
+ that permit rvalue assignment but do not also permit `const` rvalue
659
+ assignment. Consequently, an iterator type `I` that returns
660
+ `std::string` by value does not model
661
+ `indirectly_writable<I, std::string>`. — *end note*]
662
+
663
+ #### Concept <a id="iterator.concept.winc">[[iterator.concept.winc]]</a>
664
+
665
+ The `weakly_incrementable` concept specifies the requirements on types
666
+ that can be incremented with the pre- and post-increment operators. The
667
+ increment operations are not required to be equality-preserving, nor is
668
+ the type required to be `equality_comparable`.
669
+
670
+ ``` cpp
671
+ template<class T>
672
+ inline constexpr bool is-integer-like = see below; // exposition only
673
+
674
+ template<class T>
675
+ inline constexpr bool is-signed-integer-like = see below; // exposition only
676
+
677
+ template<class I>
678
+ concept weakly_incrementable =
679
+ default_initializable<I> && movable<I> &&
680
+ requires(I i) {
681
+ typename iter_difference_t<I>;
682
+ requires is-signed-integer-like<iter_difference_t<I>>;
683
+ { ++i } -> same_as<I&>; // not required to be equality-preserving
684
+ i++; // not required to be equality-preserving
685
+ };
686
+ ```
687
+
688
+ A type `I` is an *integer-class type* if it is in a set of
689
+ implementation-defined class types that behave as integer types do, as
690
+ defined in below.
691
+
692
+ The range of representable values of an integer-class type is the
693
+ continuous set of values over which it is defined. The values 0 and 1
694
+ are part of the range of every integer-class type. If any negative
695
+ numbers are part of the range, the type is a
696
+ *signed-integer-class type*; otherwise, it is an
697
+ *unsigned-integer-class type*.
698
+
699
+ For every integer-class type `I`, let `B(I)` be a hypothetical extended
700
+ integer type of the same signedness with the smallest width
701
+ [[basic.fundamental]] capable of representing the same range of values.
702
+ The width of `I` is equal to the width of `B(I)`.
703
+
704
+ Let `a` and `b` be objects of integer-class type `I`, let `x` and `y` be
705
+ objects of type `B(I)` as described above that represent the same values
706
+ as `a` and `b` respectively, and let `c` be an lvalue of any integral
707
+ type.
708
+
709
+ - For every unary operator `@` for which the expression `@x` is
710
+ well-formed, `@a` shall also be well-formed and have the same value,
711
+ effects, and value category as `@x` provided that value is
712
+ representable by `I`. If `@x` has type `bool`, so too does `@a`; if
713
+ `@x` has type `B(I)`, then `@a` has type `I`.
714
+ - For every assignment operator `@=` for which `c @= x` is well-formed,
715
+ `c @= a` shall also be well-formed and shall have the same value and
716
+ effects as `c @= x`. The expression `c @= a` shall be an lvalue
717
+ referring to `c`.
718
+ - For every binary operator `@` for which `x @ y` is well-formed,
719
+ `a @ b` shall also be well-formed and shall have the same value,
720
+ effects, and value category as `x @ y` provided that value is
721
+ representable by `I`. If `x @ y` has type `bool`, so too does `a @ b`;
722
+ if `x @ y` has type `B(I)`, then `a @ b` has type `I`.
723
+
724
+ Expressions of integer-class type are explicitly convertible to any
725
+ integral type. Expressions of integral type are both implicitly and
726
+ explicitly convertible to any integer-class type. Conversions between
727
+ integral and integer-class types do not exit via an exception.
728
+
729
+ An expression `E` of integer-class type `I` is contextually convertible
730
+ to `bool` as if by `bool(E != I(0))`.
731
+
732
+ All integer-class types model `regular` [[concepts.object]] and
733
+ `totally_ordered` [[concept.totallyordered]].
734
+
735
+ A value-initialized object of integer-class type has value 0.
736
+
737
+ For every (possibly cv-qualified) integer-class type `I`,
738
+ `numeric_limits<I>` is specialized such that:
739
+
740
+ - `numeric_limits<I>::is_specialized` is `true`,
741
+ - `numeric_limits<I>::is_signed` is `true` if and only if `I` is a
742
+ signed-integer-class type,
743
+ - `numeric_limits<I>::is_integer` is `true`,
744
+ - `numeric_limits<I>::is_exact` is `true`,
745
+ - `numeric_limits<I>::digits` is equal to the width of the integer-class
746
+ type,
747
+ - `numeric_limits<I>::digits10` is equal to
748
+ `static_cast<int>(digits * log10(2))`, and
749
+ - `numeric_limits<I>::min()` and `numeric_limits<I>::max()` return the
750
+ lowest and highest representable values of `I`, respectively, and
751
+ `numeric_limits<I>::lowest()` returns `numeric_limits<I>::{}min()`.
752
+
753
+ A type `I` is *integer-like* if it models `integral<I>` or if it is an
754
+ integer-class type. A type `I` is *signed-integer-like* if it models
755
+ `signed_integral<I>` or if it is a signed-integer-class type. A type `I`
756
+ is *unsigned-integer-like* if it models `unsigned_integral<I>` or if it
757
+ is an unsigned-integer-class type.
758
+
759
+ `is-integer-like<I>` is `true` if and only if `I` is an integer-like
760
+ type. `is-signed-integer-like<I>` is `true` if and only if I is a
761
+ signed-integer-like type.
762
+
763
+ Let `i` be an object of type `I`. When `i` is in the domain of both pre-
764
+ and post-increment, `i` is said to be *incrementable*. `I` models
765
+ `weakly_incrementable<I>` only if
766
+
767
+ - The expressions `++i` and `i++` have the same domain.
768
+ - If `i` is incrementable, then both `++i` and `i++` advance `i` to the
769
+ next element.
770
+ - If `i` is incrementable, then `addressof(++i)` is equal to
771
+ `addressof(i)`.
772
+
773
+ [*Note 1*: For `weakly_incrementable` types, `a` equals `b` does not
774
+ imply that `++a` equals `++b`. (Equality does not guarantee the
775
+ substitution property or referential transparency.) Algorithms on weakly
776
+ incrementable types should never attempt to pass through the same
777
+ incrementable value twice. They should be single-pass algorithms. These
778
+ algorithms can be used with istreams as the source of the input data
779
+ through the `istream_iterator` class template. — *end note*]
780
+
781
+ #### Concept <a id="iterator.concept.inc">[[iterator.concept.inc]]</a>
782
+
783
+ The `incrementable` concept specifies requirements on types that can be
784
+ incremented with the pre- and post-increment operators. The increment
785
+ operations are required to be equality-preserving, and the type is
786
+ required to be `equality_comparable`.
787
+
788
+ [*Note 1*: This supersedes the annotations on the increment expressions
789
+ in the definition of `weakly_incrementable`. — *end note*]
790
+
791
+ ``` cpp
792
+ template<class I>
793
+ concept incrementable =
794
+ regular<I> &&
795
+ weakly_incrementable<I> &&
796
+ requires(I i) {
797
+ { i++ } -> same_as<I>;
798
+ };
799
+ ```
800
+
801
+ Let `a` and `b` be incrementable objects of type `I`. `I` models
802
+ `incrementable` only if
803
+
804
+ - If `bool(a == b)` then `bool(a++ == b)`.
805
+ - If `bool(a == b)` then `bool(((void)a++, a) == ++b)`.
806
+
807
+ [*Note 2*: The requirement that `a` equals `b` implies `++a` equals
808
+ `++b` (which is not true for weakly incrementable types) allows the use
809
+ of multi-pass one-directional algorithms with types that model
810
+ `incrementable`. — *end note*]
811
+
812
+ #### Concept <a id="iterator.concept.iterator">[[iterator.concept.iterator]]</a>
813
+
814
+ The `input_or_output_iterator` concept forms the basis of the iterator
815
+ concept taxonomy; every iterator models `input_or_output_iterator`. This
816
+ concept specifies operations for dereferencing and incrementing an
817
+ iterator. Most algorithms will require additional operations to compare
818
+ iterators with sentinels [[iterator.concept.sentinel]], to read
819
+ [[iterator.concept.input]] or write [[iterator.concept.output]] values,
820
+ or to provide a richer set of iterator movements (
821
+ [[iterator.concept.forward]], [[iterator.concept.bidir]],
822
+ [[iterator.concept.random.access]]).
823
+
824
+ ``` cpp
825
+ template<class I>
826
+ concept input_or_output_iterator =
827
+ requires(I i) {
828
+ { *i } -> can-reference;
829
+ } &&
830
+ weakly_incrementable<I>;
831
+ ```
832
+
833
+ [*Note 1*: Unlike the *Cpp17Iterator* requirements, the
834
+ `input_or_output_iterator` concept does not require
835
+ copyability. — *end note*]
836
+
837
+ #### Concept <a id="iterator.concept.sentinel">[[iterator.concept.sentinel]]</a>
838
+
839
+ The `sentinel_for` concept specifies the relationship between an
840
+ `input_or_output_iterator` type and a `semiregular` type whose values
841
+ denote a range.
842
+
843
+ ``` cpp
844
+ template<class S, class I>
845
+ concept sentinel_for =
846
+ semiregular<S> &&
847
+ input_or_output_iterator<I> &&
848
+ weakly-equality-comparable-with<S, I>; // See [concept.equalitycomparable]
849
+ ```
850
+
851
+ Let `s` and `i` be values of type `S` and `I` such that \[`i`, `s`)
852
+ denotes a range. Types `S` and `I` model `sentinel_for<S, I>` only if
853
+
854
+ - `i == s` is well-defined.
855
+ - If `bool(i != s)` then `i` is dereferenceable and \[`++i`, `s`)
856
+ denotes a range.
857
+
858
+ The domain of `==` is not static. Given an iterator `i` and sentinel `s`
859
+ such that \[`i`, `s`) denotes a range and `i != s`, `i` and `s` are not
860
+ required to continue to denote a range after incrementing any other
861
+ iterator equal to `i`. Consequently, `i == s` is no longer required to
862
+ be well-defined.
863
+
864
+ #### Concept <a id="iterator.concept.sizedsentinel">[[iterator.concept.sizedsentinel]]</a>
865
+
866
+ The `sized_sentinel_for` concept specifies requirements on an
867
+ `input_or_output_iterator` type `I` and a corresponding
868
+ `sentinel_for<I>` that allow the use of the `-` operator to compute the
869
+ distance between them in constant time.
870
+
871
+ ``` cpp
872
+ template<class S, class I>
873
+ concept sized_sentinel_for =
874
+ sentinel_for<S, I> &&
875
+ !disable_sized_sentinel_for<remove_cv_t<S>, remove_cv_t<I>> &&
876
+ requires(const I& i, const S& s) {
877
+ { s - i } -> same_as<iter_difference_t<I>>;
878
+ { i - s } -> same_as<iter_difference_t<I>>;
879
+ };
880
+ ```
881
+
882
+ Let `i` be an iterator of type `I`, and `s` a sentinel of type `S` such
883
+ that \[`i`, `s`) denotes a range. Let N be the smallest number of
884
+ applications of `++i` necessary to make `bool(i == s)` be `true`. `S`
885
+ and `I` model `sized_sentinel_for<S, I>` only if
886
+
887
+ - If N is representable by `iter_difference_t<I>`, then `s - i` is
888
+ well-defined and equals N.
889
+ - If -N is representable by `iter_difference_t<I>`, then `i - s` is
890
+ well-defined and equals -N.
891
+
892
+ ``` cpp
893
+ template<class S, class I>
894
+ inline constexpr bool disable_sized_sentinel_for = false;
895
+ ```
896
+
897
+ *Remarks:* Pursuant to [[namespace.std]], users may specialize
898
+ `disable_sized_sentinel_for` for cv-unqualified non-array object types
899
+ `S` and `I` if `S` and/or `I` is a program-defined type. Such
900
+ specializations shall be usable in constant expressions [[expr.const]]
901
+ and have type `const bool`.
902
+
903
+ [*Note 1*: `disable_sized_sentinel_for` allows use of sentinels and
904
+ iterators with the library that satisfy but do not in fact model
905
+ `sized_sentinel_for`. — *end note*]
906
+
907
+ [*Example 1*: The `sized_sentinel_for` concept is modeled by pairs of
908
+ `random_access_iterator`s [[iterator.concept.random.access]] and by
909
+ counted iterators and their
910
+ sentinels [[counted.iterator]]. — *end example*]
911
+
912
+ #### Concept <a id="iterator.concept.input">[[iterator.concept.input]]</a>
913
+
914
+ The `input_iterator` concept defines requirements for a type whose
915
+ referenced values can be read (from the requirement for
916
+ `indirectly_readable` [[iterator.concept.readable]]) and which can be
917
+ both pre- and post-incremented.
918
+
919
+ [*Note 1*: Unlike the *Cpp17InputIterator* requirements
920
+ [[input.iterators]], the `input_iterator` concept does not need equality
921
+ comparison since iterators are typically compared to
922
+ sentinels. — *end note*]
923
+
924
+ ``` cpp
925
+ template<class I>
926
+ concept input_iterator =
927
+ input_or_output_iterator<I> &&
928
+ indirectly_readable<I> &&
929
+ requires { typename ITER_CONCEPT(I); } &&
930
+ derived_from<ITER_CONCEPT(I), input_iterator_tag>;
931
+ ```
932
+
933
+ #### Concept <a id="iterator.concept.output">[[iterator.concept.output]]</a>
934
+
935
+ The `output_iterator` concept defines requirements for a type that can
936
+ be used to write values (from the requirement for `indirectly_writable`
937
+ [[iterator.concept.writable]]) and which can be both pre- and
938
+ post-incremented.
939
+
940
+ [*Note 1*: Output iterators are not required to model
941
+ `equality_comparable`. — *end note*]
942
+
943
+ ``` cpp
944
+ template<class I, class T>
945
+ concept output_iterator =
946
+ input_or_output_iterator<I> &&
947
+ indirectly_writable<I, T> &&
948
+ requires(I i, T&& t) {
949
+ *i++ = std::forward<T>(t); // not required to be equality-preserving
950
+ };
951
+ ```
952
+
953
+ Let `E` be an expression such that `decltype((E))` is `T`, and let `i`
954
+ be a dereferenceable object of type `I`. `I` and `T` model
955
+ `output_iterator<I, T>` only if `*i++ = E;` has effects equivalent to:
956
+
957
+ ``` cpp
958
+ *i = E;
959
+ ++i;
960
+ ```
961
+
962
+ [*Note 2*: Algorithms on output iterators should never attempt to pass
963
+ through the same iterator twice. They should be single-pass
964
+ algorithms. — *end note*]
965
+
966
+ #### Concept <a id="iterator.concept.forward">[[iterator.concept.forward]]</a>
967
+
968
+ The `forward_iterator` concept adds copyability, equality comparison,
969
+ and the multi-pass guarantee, specified below.
970
+
971
+ ``` cpp
972
+ template<class I>
973
+ concept forward_iterator =
974
+ input_iterator<I> &&
975
+ derived_from<ITER_CONCEPT(I), forward_iterator_tag> &&
976
+ incrementable<I> &&
977
+ sentinel_for<I, I>;
978
+ ```
979
+
980
+ The domain of `==` for forward iterators is that of iterators over the
981
+ same underlying sequence. However, value-initialized iterators of the
982
+ same type may be compared and shall compare equal to other
983
+ value-initialized iterators of the same type.
984
+
985
+ [*Note 1*: Value-initialized iterators behave as if they refer past the
986
+ end of the same empty sequence. — *end note*]
987
+
988
+ Pointers and references obtained from a forward iterator into a range
989
+ \[`i`, `s`) shall remain valid while \[`i`, `s`) continues to denote a
990
+ range.
991
+
992
+ Two dereferenceable iterators `a` and `b` of type `X` offer the
993
+ *multi-pass guarantee* if:
994
+
995
+ - `a == b` implies `++a == ++b` and
996
+ - The expression `((void)[](X x){++x;}(a), *a)` is equivalent to the
997
+ expression `*a`.
998
+
999
+ [*Note 2*: The requirement that `a == b` implies `++a == ++b` and the
1000
+ removal of the restrictions on the number of assignments through a
1001
+ mutable iterator (which applies to output iterators) allow the use of
1002
+ multi-pass one-directional algorithms with forward
1003
+ iterators. — *end note*]
1004
+
1005
+ #### Concept <a id="iterator.concept.bidir">[[iterator.concept.bidir]]</a>
1006
+
1007
+ The `bidirectional_iterator` concept adds the ability to move an
1008
+ iterator backward as well as forward.
1009
+
1010
+ ``` cpp
1011
+ template<class I>
1012
+ concept bidirectional_iterator =
1013
+ forward_iterator<I> &&
1014
+ derived_from<ITER_CONCEPT(I), bidirectional_iterator_tag> &&
1015
+ requires(I i) {
1016
+ { --i } -> same_as<I&>;
1017
+ { i-- } -> same_as<I>;
1018
+ };
1019
+ ```
1020
+
1021
+ A bidirectional iterator `r` is decrementable if and only if there
1022
+ exists some `q` such that `++q == r`. Decrementable iterators `r` shall
1023
+ be in the domain of the expressions `--r` and `r--`.
1024
+
1025
+ Let `a` and `b` be equal objects of type `I`. `I` models
1026
+ `bidirectional_iterator` only if:
1027
+
1028
+ - If `a` and `b` are decrementable, then all of the following are
1029
+ `true`:
1030
+ - `addressof(--a) == addressof(a)`
1031
+ - `bool(a-- == b)`
1032
+ - after evaluating both `a--` and `--b`, `bool(a == b)` is still
1033
+ `true`
1034
+ - `bool(++(--a) == b)`
1035
+ - If `a` and `b` are incrementable, then `bool(--(++a) == b)`.
1036
+
1037
+ #### Concept <a id="iterator.concept.random.access">[[iterator.concept.random.access]]</a>
1038
+
1039
+ The `random_access_iterator` concept adds support for constant-time
1040
+ advancement with `+=`, `+`, `-=`, and `-`, as well as the computation of
1041
+ distance in constant time with `-`. Random access iterators also support
1042
+ array notation via subscripting.
1043
+
1044
+ ``` cpp
1045
+ template<class I>
1046
+ concept random_access_iterator =
1047
+ bidirectional_iterator<I> &&
1048
+ derived_from<ITER_CONCEPT(I), random_access_iterator_tag> &&
1049
+ totally_ordered<I> &&
1050
+ sized_sentinel_for<I, I> &&
1051
+ requires(I i, const I j, const iter_difference_t<I> n) {
1052
+ { i += n } -> same_as<I&>;
1053
+ { j + n } -> same_as<I>;
1054
+ { n + j } -> same_as<I>;
1055
+ { i -= n } -> same_as<I&>;
1056
+ { j - n } -> same_as<I>;
1057
+ { j[n] } -> same_as<iter_reference_t<I>>;
1058
+ };
1059
+ ```
1060
+
1061
+ Let `a` and `b` be valid iterators of type `I` such that `b` is
1062
+ reachable from `a` after `n` applications of `++a`, let `D` be
1063
+ `iter_difference_t<I>`, and let `n` denote a value of type `D`. `I`
1064
+ models `random_access_iterator` only if
1065
+
1066
+ - `(a += n)` is equal to `b`.
1067
+ - `addressof(a += n)` is equal to `addressof(a)`.
1068
+ - `(a + n)` is equal to `(a += n)`.
1069
+ - For any two positive values `x` and `y` of type `D`, if
1070
+ `(a + D(x + y))` is valid, then `(a + D(x + y))` is equal to
1071
+ `((a + x) + y)`.
1072
+ - `(a + D(0))` is equal to `a`.
1073
+ - If `(a + D(n - 1))` is valid, then `(a + n)` is equal to
1074
+ `[](I c){ return ++c; }(a + D(n - 1))`.
1075
+ - `(b += D(-n))` is equal to `a`.
1076
+ - `(b -= n)` is equal to `a`.
1077
+ - `addressof(b -= n)` is equal to `addressof(b)`.
1078
+ - `(b - n)` is equal to `(b -= n)`.
1079
+ - If `b` is dereferenceable, then `a[n]` is valid and is equal to `*b`.
1080
+ - `bool(a <= b)` is `true`.
1081
+
1082
+ #### Concept <a id="iterator.concept.contiguous">[[iterator.concept.contiguous]]</a>
1083
+
1084
+ The `contiguous_iterator` concept provides a guarantee that the denoted
1085
+ elements are stored contiguously in memory.
1086
+
1087
+ ``` cpp
1088
+ template<class I>
1089
+ concept contiguous_iterator =
1090
+ random_access_iterator<I> &&
1091
+ derived_from<ITER_CONCEPT(I), contiguous_iterator_tag> &&
1092
+ is_lvalue_reference_v<iter_reference_t<I>> &&
1093
+ same_as<iter_value_t<I>, remove_cvref_t<iter_reference_t<I>>> &&
1094
+ requires(const I& i) {
1095
+ { to_address(i) } -> same_as<add_pointer_t<iter_reference_t<I>>>;
1096
+ };
1097
+ ```
1098
+
1099
+ Let `a` and `b` be dereferenceable iterators and `c` be a
1100
+ non-dereferenceable iterator of type `I` such that `b` is reachable from
1101
+ `a` and `c` is reachable from `b`, and let `D` be
1102
+ `iter_difference_t<I>`. The type `I` models `contiguous_iterator` only
1103
+ if
1104
+
1105
+ - `to_address(a) == addressof(*a)`,
1106
+ - `to_address(b) == to_address(a) + D(b - a)`, and
1107
+ - `to_address(c) == to_address(a) + D(c - a)`.
1108
+
1109
+ ### C++17 iterator requirements <a id="iterator.cpp17">[[iterator.cpp17]]</a>
1110
 
1111
  In the following sections, `a` and `b` denote values of type `X` or
1112
  `const X`, `difference_type` and `reference` refer to the types
1113
  `iterator_traits<X>::difference_type` and
1114
  `iterator_traits<X>::reference`, respectively, `n` denotes a value of
1115
  `difference_type`, `u`, `tmp`, and `m` denote identifiers, `r` denotes a
1116
  value of `X&`, `t` denotes a value of value type `T`, `o` denotes a
1117
  value of some type that is writable to the output iterator.
1118
 
1119
+ [*Note 1*: For an iterator type `X` there must be an instantiation of
1120
+ `iterator_traits<X>` [[iterator.traits]]. — *end note*]
1121
 
1122
+ #### *Cpp17Iterator* <a id="iterator.iterators">[[iterator.iterators]]</a>
1123
 
1124
+ The *Cpp17Iterator* requirements form the basis of the iterator
1125
+ taxonomy; every iterator meets the *Cpp17Iterator* requirements. This
1126
+ set of requirements specifies operations for dereferencing and
1127
+ incrementing an iterator. Most algorithms will require additional
1128
+ operations to read [[input.iterators]] or write [[output.iterators]]
1129
+ values, or to provide a richer set of iterator movements (
1130
+ [[forward.iterators]], [[bidirectional.iterators]],
1131
+ [[random.access.iterators]]).
1132
 
1133
+ A type `X` meets the *Cpp17Iterator* requirements if:
1134
 
1135
+ - `X` meets the *Cpp17CopyConstructible*, *Cpp17CopyAssignable*, and
1136
+ *Cpp17Destructible* requirements [[utility.arg.requirements]] and
1137
+ lvalues of type `X` are swappable [[swappable.requirements]], and
1138
+ - `iterator_traits<X>::difference_type` is a signed integer type or
1139
+ `void`, and
1140
+ - the expressions in [[iterator]] are valid and have the indicated
 
 
 
 
 
 
 
1141
  semantics.
1142
 
1143
+ #### Input iterators <a id="input.iterators">[[input.iterators]]</a>
1144
+
1145
+ A class or pointer type `X` meets the requirements of an input iterator
1146
+ for the value type `T` if `X` meets the *Cpp17Iterator*
1147
+ [[iterator.iterators]] and *Cpp17EqualityComparable* (
1148
+ [[cpp17.equalitycomparable]]) requirements and the expressions in
1149
+ [[inputiterator]] are valid and have the indicated semantics.
1150
+
1151
+ In [[inputiterator]], the term *the domain of `==`* is used in the
1152
+ ordinary mathematical sense to denote the set of values over which `==`
1153
+ is (required to be) defined. This set can change over time. Each
1154
+ algorithm places additional requirements on the domain of `==` for the
1155
+ iterator values it uses. These requirements can be inferred from the
1156
+ uses that algorithm makes of `==` and `!=`.
1157
 
1158
  [*Example 1*: The call `find(a,b,x)` is defined only if the value of
1159
  `a` has the property *p* defined as follows: `b` has property *p* and a
1160
  value `i` has property *p* if (`*i==x`) or if (`*i!=x` and `++i` has
1161
  property *p*). — *end example*]
1162
 
1163
  [*Note 1*: For input iterators, `a == b` does not imply `++a == ++b`.
1164
  (Equality does not guarantee the substitution property or referential
1165
  transparency.) Algorithms on input iterators should never attempt to
1166
  pass through the same iterator twice. They should be *single pass*
1167
+ algorithms. Value type `T` is not required to be a *Cpp17CopyAssignable*
1168
+ type ([[cpp17.copyassignable]]). These algorithms can be used with
1169
  istreams as the source of the input data through the `istream_iterator`
1170
  class template. — *end note*]
1171
 
1172
+ #### Output iterators <a id="output.iterators">[[output.iterators]]</a>
1173
 
1174
+ A class or pointer type `X` meets the requirements of an output iterator
1175
+ if `X` meets the *Cpp17Iterator* requirements [[iterator.iterators]] and
1176
+ the expressions in [[outputiterator]] are valid and have the indicated
 
1177
  semantics.
1178
 
1179
  [*Note 1*: The only valid use of an `operator*` is on the left side of
1180
+ the assignment statement. Assignment through the same value of the
1181
+ iterator happens only once. Algorithms on output iterators should never
1182
+ attempt to pass through the same iterator twice. They should be
1183
+ single-pass algorithms. Equality and inequality might not be
1184
+ defined. *end note*]
 
 
1185
 
1186
+ #### Forward iterators <a id="forward.iterators">[[forward.iterators]]</a>
1187
 
1188
+ A class or pointer type `X` meets the requirements of a forward iterator
1189
+ if
1190
 
1191
+ - `X` meets the *Cpp17InputIterator* requirements [[input.iterators]],
1192
+ - `X` meets the *Cpp17DefaultConstructible* requirements
1193
+ [[utility.arg.requirements]],
 
1194
  - if `X` is a mutable iterator, `reference` is a reference to `T`; if
1195
  `X` is a constant iterator, `reference` is a reference to `const T`,
1196
+ - the expressions in [[forwarditerator]] are valid and have the
1197
+ indicated semantics, and
1198
  - objects of type `X` offer the multi-pass guarantee, described below.
1199
 
1200
  The domain of `==` for forward iterators is that of iterators over the
1201
  same underlying sequence. However, value-initialized iterators may be
1202
  compared and shall compare equal to other value-initialized iterators of
 
1222
  dereferenceable or else neither is dereferenceable.
1223
 
1224
  If `a` and `b` are both dereferenceable, then `a == b` if and only if
1225
  `*a` and `*b` are bound to the same object.
1226
 
1227
+ #### Bidirectional iterators <a id="bidirectional.iterators">[[bidirectional.iterators]]</a>
1228
 
1229
+ A class or pointer type `X` meets the requirements of a bidirectional
1230
+ iterator if, in addition to meeting the *Cpp17ForwardIterator*
1231
+ requirements, the following expressions are valid as shown in
1232
+ [[bidirectionaliterator]].
1233
 
1234
  [*Note 1*: Bidirectional iterators allow algorithms to move iterators
1235
  backward as well as forward. — *end note*]
1236
 
1237
+ #### Random access iterators <a id="random.access.iterators">[[random.access.iterators]]</a>
1238
 
1239
+ A class or pointer type `X` meets the requirements of a random access
1240
+ iterator if, in addition to meeting the *Cpp17BidirectionalIterator*
1241
+ requirements, the following expressions are valid as shown in
1242
+ [[randomaccessiterator]].
1243
+
1244
+ ### Indirect callable requirements <a id="indirectcallable">[[indirectcallable]]</a>
1245
+
1246
+ #### General <a id="indirectcallable.general">[[indirectcallable.general]]</a>
1247
+
1248
+ There are several concepts that group requirements of algorithms that
1249
+ take callable objects ([[func.def]]) as arguments.
1250
+
1251
+ #### Indirect callables <a id="indirectcallable.indirectinvocable">[[indirectcallable.indirectinvocable]]</a>
1252
+
1253
+ The indirect callable concepts are used to constrain those algorithms
1254
+ that accept callable objects ([[func.def]]) as arguments.
1255
+
1256
+ ``` cpp
1257
+ namespace std {
1258
+ template<class F, class I>
1259
+ concept indirectly_unary_invocable =
1260
+ indirectly_readable<I> &&
1261
+ copy_constructible<F> &&
1262
+ invocable<F&, iter_value_t<I>&> &&
1263
+ invocable<F&, iter_reference_t<I>> &&
1264
+ invocable<F&, iter_common_reference_t<I>> &&
1265
+ common_reference_with<
1266
+ invoke_result_t<F&, iter_value_t<I>&>,
1267
+ invoke_result_t<F&, iter_reference_t<I>>>;
1268
+
1269
+ template<class F, class I>
1270
+ concept indirectly_regular_unary_invocable =
1271
+ indirectly_readable<I> &&
1272
+ copy_constructible<F> &&
1273
+ regular_invocable<F&, iter_value_t<I>&> &&
1274
+ regular_invocable<F&, iter_reference_t<I>> &&
1275
+ regular_invocable<F&, iter_common_reference_t<I>> &&
1276
+ common_reference_with<
1277
+ invoke_result_t<F&, iter_value_t<I>&>,
1278
+ invoke_result_t<F&, iter_reference_t<I>>>;
1279
+
1280
+ template<class F, class I>
1281
+ concept indirect_unary_predicate =
1282
+ indirectly_readable<I> &&
1283
+ copy_constructible<F> &&
1284
+ predicate<F&, iter_value_t<I>&> &&
1285
+ predicate<F&, iter_reference_t<I>> &&
1286
+ predicate<F&, iter_common_reference_t<I>>;
1287
+
1288
+ template<class F, class I1, class I2>
1289
+ concept indirect_binary_predicate =
1290
+ indirectly_readable<I1> && indirectly_readable<I2> &&
1291
+ copy_constructible<F> &&
1292
+ predicate<F&, iter_value_t<I1>&, iter_value_t<I2>&> &&
1293
+ predicate<F&, iter_value_t<I1>&, iter_reference_t<I2>> &&
1294
+ predicate<F&, iter_reference_t<I1>, iter_value_t<I2>&> &&
1295
+ predicate<F&, iter_reference_t<I1>, iter_reference_t<I2>> &&
1296
+ predicate<F&, iter_common_reference_t<I1>, iter_common_reference_t<I2>>;
1297
+
1298
+ template<class F, class I1, class I2 = I1>
1299
+ concept indirect_equivalence_relation =
1300
+ indirectly_readable<I1> && indirectly_readable<I2> &&
1301
+ copy_constructible<F> &&
1302
+ equivalence_relation<F&, iter_value_t<I1>&, iter_value_t<I2>&> &&
1303
+ equivalence_relation<F&, iter_value_t<I1>&, iter_reference_t<I2>> &&
1304
+ equivalence_relation<F&, iter_reference_t<I1>, iter_value_t<I2>&> &&
1305
+ equivalence_relation<F&, iter_reference_t<I1>, iter_reference_t<I2>> &&
1306
+ equivalence_relation<F&, iter_common_reference_t<I1>, iter_common_reference_t<I2>>;
1307
+
1308
+ template<class F, class I1, class I2 = I1>
1309
+ concept indirect_strict_weak_order =
1310
+ indirectly_readable<I1> && indirectly_readable<I2> &&
1311
+ copy_constructible<F> &&
1312
+ strict_weak_order<F&, iter_value_t<I1>&, iter_value_t<I2>&> &&
1313
+ strict_weak_order<F&, iter_value_t<I1>&, iter_reference_t<I2>> &&
1314
+ strict_weak_order<F&, iter_reference_t<I1>, iter_value_t<I2>&> &&
1315
+ strict_weak_order<F&, iter_reference_t<I1>, iter_reference_t<I2>> &&
1316
+ strict_weak_order<F&, iter_common_reference_t<I1>, iter_common_reference_t<I2>>;
1317
+ }
1318
+ ```
1319
+
1320
+ #### Class template `projected` <a id="projected">[[projected]]</a>
1321
+
1322
+ Class template `projected` is used to constrain algorithms that accept
1323
+ callable objects and projections [[defns.projection]]. It combines a
1324
+ `indirectly_readable` type `I` and a callable object type `Proj` into a
1325
+ new `indirectly_readable` type whose `reference` type is the result of
1326
+ applying `Proj` to the `iter_reference_t` of `I`.
1327
+
1328
+ ``` cpp
1329
+ namespace std {
1330
+ template<indirectly_readable I, indirectly_regular_unary_invocable<I> Proj>
1331
+ struct projected {
1332
+ using value_type = remove_cvref_t<indirect_result_t<Proj&, I>>;
1333
+ indirect_result_t<Proj&, I> operator*() const; // not defined
1334
+ };
1335
+
1336
+ template<weakly_incrementable I, class Proj>
1337
+ struct incrementable_traits<projected<I, Proj>> {
1338
+ using difference_type = iter_difference_t<I>;
1339
+ };
1340
+ }
1341
+ ```
1342
+
1343
+ ### Common algorithm requirements <a id="alg.req">[[alg.req]]</a>
1344
+
1345
+ #### General <a id="alg.req.general">[[alg.req.general]]</a>
1346
+
1347
+ There are several additional iterator concepts that are commonly applied
1348
+ to families of algorithms. These group together iterator requirements of
1349
+ algorithm families. There are three relational concepts that specify how
1350
+ element values are transferred between `indirectly_readable` and
1351
+ `indirectly_writable` types: `indirectly_movable`,
1352
+ `indirectly_copyable`, and `indirectly_swappable`. There are three
1353
+ relational concepts for rearrangements: `permutable`, `mergeable`, and
1354
+ `sortable`. There is one relational concept for comparing values from
1355
+ different sequences: `indirectly_comparable`.
1356
+
1357
+ [*Note 1*: The `ranges::less` function object type used in the concepts
1358
+ below imposes constraints on the concepts’ arguments in addition to
1359
+ those that appear in the concepts’ bodies [[range.cmp]]. — *end note*]
1360
+
1361
+ #### Concept <a id="alg.req.ind.move">[[alg.req.ind.move]]</a>
1362
+
1363
+ The `indirectly_movable` concept specifies the relationship between a
1364
+ `indirectly_readable` type and a `indirectly_writable` type between
1365
+ which values may be moved.
1366
+
1367
+ ``` cpp
1368
+ template<class In, class Out>
1369
+ concept indirectly_movable =
1370
+ indirectly_readable<In> &&
1371
+ indirectly_writable<Out, iter_rvalue_reference_t<In>>;
1372
+ ```
1373
+
1374
+ The `indirectly_movable_storable` concept augments `indirectly_movable`
1375
+ with additional requirements enabling the transfer to be performed
1376
+ through an intermediate object of the `indirectly_readable` type’s value
1377
+ type.
1378
+
1379
+ ``` cpp
1380
+ template<class In, class Out>
1381
+ concept indirectly_movable_storable =
1382
+ indirectly_movable<In, Out> &&
1383
+ indirectly_writable<Out, iter_value_t<In>> &&
1384
+ movable<iter_value_t<In>> &&
1385
+ constructible_from<iter_value_t<In>, iter_rvalue_reference_t<In>> &&
1386
+ assignable_from<iter_value_t<In>&, iter_rvalue_reference_t<In>>;
1387
+ ```
1388
+
1389
+ Let `i` be a dereferenceable value of type `In`. `In` and `Out` model
1390
+ `indirectly_movable_storable<In, Out>` only if after the initialization
1391
+ of the object `obj` in
1392
+
1393
+ ``` cpp
1394
+ iter_value_t<In> obj(ranges::iter_move(i));
1395
+ ```
1396
+
1397
+ `obj` is equal to the value previously denoted by `*i`. If
1398
+ `iter_rvalue_reference_t<In>` is an rvalue reference type, the resulting
1399
+ state of the value denoted by `*i` is valid but unspecified
1400
+ [[lib.types.movedfrom]].
1401
+
1402
+ #### Concept <a id="alg.req.ind.copy">[[alg.req.ind.copy]]</a>
1403
+
1404
+ The `indirectly_copyable` concept specifies the relationship between a
1405
+ `indirectly_readable` type and a `indirectly_writable` type between
1406
+ which values may be copied.
1407
+
1408
+ ``` cpp
1409
+ template<class In, class Out>
1410
+ concept indirectly_copyable =
1411
+ indirectly_readable<In> &&
1412
+ indirectly_writable<Out, iter_reference_t<In>>;
1413
+ ```
1414
+
1415
+ The `indirectly_copyable_storable` concept augments
1416
+ `indirectly_copyable` with additional requirements enabling the transfer
1417
+ to be performed through an intermediate object of the
1418
+ `indirectly_readable` type’s value type. It also requires the capability
1419
+ to make copies of values.
1420
+
1421
+ ``` cpp
1422
+ template<class In, class Out>
1423
+ concept indirectly_copyable_storable =
1424
+ indirectly_copyable<In, Out> &&
1425
+ indirectly_writable<Out, iter_value_t<In>&> &&
1426
+ indirectly_writable<Out, const iter_value_t<In>&> &&
1427
+ indirectly_writable<Out, iter_value_t<In>&&> &&
1428
+ indirectly_writable<Out, const iter_value_t<In>&&> &&
1429
+ copyable<iter_value_t<In>> &&
1430
+ constructible_from<iter_value_t<In>, iter_reference_t<In>> &&
1431
+ assignable_from<iter_value_t<In>&, iter_reference_t<In>>;
1432
+ ```
1433
+
1434
+ Let `i` be a dereferenceable value of type `In`. `In` and `Out` model
1435
+ `indirectly_copyable_storable<In, Out>` only if after the initialization
1436
+ of the object `obj` in
1437
+
1438
+ ``` cpp
1439
+ iter_value_t<In> obj(*i);
1440
+ ```
1441
+
1442
+ `obj` is equal to the value previously denoted by `*i`. If
1443
+ `iter_reference_t<In>` is an rvalue reference type, the resulting state
1444
+ of the value denoted by `*i` is valid but unspecified
1445
+ [[lib.types.movedfrom]].
1446
+
1447
+ #### Concept <a id="alg.req.ind.swap">[[alg.req.ind.swap]]</a>
1448
+
1449
+ The `indirectly_swappable` concept specifies a swappable relationship
1450
+ between the values referenced by two `indirectly_readable` types.
1451
+
1452
+ ``` cpp
1453
+ template<class I1, class I2 = I1>
1454
+ concept indirectly_swappable =
1455
+ indirectly_readable<I1> && indirectly_readable<I2> &&
1456
+ requires(const I1 i1, const I2 i2) {
1457
+ ranges::iter_swap(i1, i1);
1458
+ ranges::iter_swap(i2, i2);
1459
+ ranges::iter_swap(i1, i2);
1460
+ ranges::iter_swap(i2, i1);
1461
+ };
1462
+ ```
1463
+
1464
+ #### Concept <a id="alg.req.ind.cmp">[[alg.req.ind.cmp]]</a>
1465
+
1466
+ The `indirectly_comparable` concept specifies the common requirements of
1467
+ algorithms that compare values from two different sequences.
1468
+
1469
+ ``` cpp
1470
+ template<class I1, class I2, class R, class P1 = identity,
1471
+ class P2 = identity>
1472
+ concept indirectly_comparable =
1473
+ indirect_binary_predicate<R, projected<I1, P1>, projected<I2, P2>>;
1474
+ ```
1475
+
1476
+ #### Concept <a id="alg.req.permutable">[[alg.req.permutable]]</a>
1477
+
1478
+ The `permutable` concept specifies the common requirements of algorithms
1479
+ that reorder elements in place by moving or swapping them.
1480
+
1481
+ ``` cpp
1482
+ template<class I>
1483
+ concept permutable =
1484
+ forward_iterator<I> &&
1485
+ indirectly_movable_storable<I, I> &&
1486
+ indirectly_swappable<I, I>;
1487
+ ```
1488
+
1489
+ #### Concept <a id="alg.req.mergeable">[[alg.req.mergeable]]</a>
1490
+
1491
+ The `mergeable` concept specifies the requirements of algorithms that
1492
+ merge sorted sequences into an output sequence by copying elements.
1493
+
1494
+ ``` cpp
1495
+ template<class I1, class I2, class Out, class R = ranges::less,
1496
+ class P1 = identity, class P2 = identity>
1497
+ concept mergeable =
1498
+ input_iterator<I1> &&
1499
+ input_iterator<I2> &&
1500
+ weakly_incrementable<Out> &&
1501
+ indirectly_copyable<I1, Out> &&
1502
+ indirectly_copyable<I2, Out> &&
1503
+ indirect_strict_weak_order<R, projected<I1, P1>, projected<I2, P2>>;
1504
+ ```
1505
+
1506
+ #### Concept <a id="alg.req.sortable">[[alg.req.sortable]]</a>
1507
+
1508
+ The `sortable` concept specifies the common requirements of algorithms
1509
+ that permute sequences into ordered sequences (e.g., `sort`).
1510
+
1511
+ ``` cpp
1512
+ template<class I, class R = ranges::less, class P = identity>
1513
+ concept sortable =
1514
+ permutable<I> &&
1515
+ indirect_strict_weak_order<R, projected<I, P>>;
1516
+ ```
1517