-
Notifications
You must be signed in to change notification settings - Fork 8
/
iterators.tex
6115 lines (5099 loc) · 181 KB
/
iterators.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[iterators]{Iterators library}
\rSec1[iterators.general]{General}
\pnum
This Clause describes components that \Cpp programs may use to perform
iterations over containers (Clause \cxxref{containers}),
streams~(\cxxref{iostream.format}),
and stream buffers~(\cxxref{stream.buffers}).
\pnum
The following subclauses describe
iterator requirements, and
components for
iterator primitives,
predefined iterators,
and stream iterators,
as summarized in Table~\ref{tab:iterators.lib.summary}.
\begin{libsumtab}{Iterators library summary}{tab:iterators.lib.summary}
\ref{iterator.requirements} & Iterator requirements & \\
\ref{indirectcallable} & Indirect callable requirements & \\
\ref{commonalgoreq} & Common algorithm requirements & \\ \rowsep
\ref{iterator.primitives} & Iterator primitives & \tcode{<experimental/ranges/iterator>} \\
\ref{iterators.predef} & Predefined iterators & \\
\ref{iterators.stream} & Stream iterators & \\
\end{libsumtab}
\rSec1[iterator.synopsis]{Header \tcode{<experimental/ranges/iterator>} synopsis}
\indexlibrary{\idxhdr{experimental/ranges/iterator}}%
\begin{codeblock}
namespace std { namespace experimental { namespace ranges { inline namespace v1 {
template <class T> concept bool @\placeholder{dereferenceable}@ // \expos
= requires(T& t) { {*t} -> auto&&; };
// \ref{iterator.requirements}, iterator requirements:
// \ref{iterator.custpoints}, customization points:
namespace {
// \ref{iterator.custpoints.iter_move}, iter_move:
constexpr @\unspec@ iter_move = @\unspec@;
// \ref{iterator.custpoints.iter_swap}, iter_swap:
constexpr @\unspec@ iter_swap = @\unspec@;
}
// \ref{iterator.assoc.types}, associated types:
// \ref{iterator.assoc.types.difference_type}, difference_type:
template <class> struct difference_type;
template <class T> using difference_type_t
= typename difference_type<T>::type;
// \ref{iterator.assoc.types.value_type}, value_type:
template <class> struct value_type;
template <class T> using value_type_t
= typename value_type<T>::type;
// \ref{iterator.assoc.types.iterator_category}, iterator_category:
template <class> struct iterator_category;
template <class T> using iterator_category_t
= typename iterator_category<T>::type;
template <@\placeholder{dereferenceable}@ T> using reference_t
= decltype(*declval<T&>());
template <@\placeholder{dereferenceable}@ T>
requires @\seebelow@ using rvalue_reference_t
= decltype(ranges::iter_move(declval<T&>()));
// \ref{iterators.readable}, Readable:
template <class In>
concept bool Readable = @\seebelow@;
// \ref{iterators.writable}, Writable:
template <class Out, class T>
concept bool Writable = @\seebelow@;
// \ref{iterators.weaklyincrementable}, WeaklyIncrementable:
template <class I>
concept bool WeaklyIncrementable = @\seebelow@;
// \ref{iterators.incrementable}, Incrementable:
template <class I>
concept bool Incrementable = @\seebelow@;
// \ref{iterators.iterator}, Iterator:
template <class I>
concept bool Iterator = @\seebelow@;
// \ref{iterators.sentinel}, Sentinel:
template <class S, class I>
concept bool Sentinel = @\seebelow@;
// \ref{iterators.sizedsentinel}, SizedSentinel:
template <class S, class I>
constexpr bool disable_sized_sentinel = false;
template <class S, class I>
concept bool SizedSentinel = @\seebelow@;
// \ref{iterators.input}, InputIterator:
template <class I>
concept bool InputIterator = @\seebelow@;
// \ref{iterators.output}, OutputIterator:
template <class I>
concept bool OutputIterator = @\seebelow@;
// \ref{iterators.forward}, ForwardIterator:
template <class I>
concept bool ForwardIterator = @\seebelow@;
// \ref{iterators.bidirectional}, BidirectionalIterator:
template <class I>
concept bool BidirectionalIterator = @\seebelow@;
// \ref{iterators.random.access}, RandomAccessIterator:
template <class I>
concept bool RandomAccessIterator = @\seebelow@;
// \ref{indirectcallable}, indirect callable requirements:
// \ref{indirectcallable.indirectinvocable}, indirect callables:
template <class F, class I>
concept bool IndirectUnaryInvocable = @\seebelow@;
template <class F, class I>
concept bool IndirectRegularUnaryInvocable = @\seebelow@;
template <class F, class I>
concept bool IndirectUnaryPredicate = @\seebelow@;
template <class F, class I1, class I2 = I1>
concept bool IndirectRelation = @\seebelow@;
template <class F, class I1, class I2 = I1>
concept bool IndirectStrictWeakOrder = @\seebelow@;
template <class> struct indirect_result_of;
template <class F, class... Is>
requires Invocable<F, reference_t<Is>...>
struct indirect_result_of<F(Is...)>;
template <class F>
using indirect_result_of_t
= typename indirect_result_of<F>::type;
// \ref{projected}, projected:
template <Readable I, IndirectRegularUnaryInvocable<I> Proj>
struct projected;
template <WeaklyIncrementable I, class Proj>
struct difference_type<projected<I, Proj>>;
// \ref{commonalgoreq}, common algorithm requirements:
// \ref{commonalgoreq.indirectlymovable} IndirectlyMovable:
template <class In, class Out>
concept bool IndirectlyMovable = @\seebelow@;
template <class In, class Out>
concept bool IndirectlyMovableStorable = @\seebelow@;
// \ref{commonalgoreq.indirectlycopyable} IndirectlyCopyable:
template <class In, class Out>
concept bool IndirectlyCopyable = @\seebelow@;
template <class In, class Out>
concept bool IndirectlyCopyableStorable = @\seebelow@;
// \ref{commonalgoreq.indirectlyswappable} IndirectlySwappable:
template <class I1, class I2 = I1>
concept bool IndirectlySwappable = @\seebelow@;
// \ref{commonalgoreq.indirectlycomparable} IndirectlyComparable:
template <class I1, class I2, class R = equal_to<>, class P1 = identity,
class P2 = identity>
concept bool IndirectlyComparable = @\seebelow@;
// \ref{commonalgoreq.permutable} Permutable:
template <class I>
concept bool Permutable = @\seebelow@;
// \ref{commonalgoreq.mergeable} Mergeable:
template <class I1, class I2, class Out,
class R = less<>, class P1 = identity, class P2 = identity>
concept bool Mergeable = @\seebelow@;
template <class I, class R = less<>, class P = identity>
concept bool Sortable = @\seebelow@;
// \ref{iterator.primitives}, primitives:
// \ref{iterator.traits}, traits:
template <class Iterator> using iterator_traits = @\seebelow@;
template <Readable T> using iter_common_reference_t
= common_reference_t<reference_t<T>, value_type_t<T>&>;
// \ref{std.iterator.tags}, iterator tags:
struct output_iterator_tag { };
struct input_iterator_tag { };
struct forward_iterator_tag : input_iterator_tag { };
struct bidirectional_iterator_tag : forward_iterator_tag { };
struct random_access_iterator_tag : bidirectional_iterator_tag { };
// \ref{iterator.operations}, iterator operations:
namespace {
constexpr @\unspec@ advance = @\unspec@;
constexpr @\unspec@ distance = @\unspec@;
constexpr @\unspec@ next = @\unspec@;
constexpr @\unspec@ prev = @\unspec@;
}
// \ref{iterators.predef}, predefined iterators and sentinels:
// \ref{iterators.reverse}, reverse iterators:
template <BidirectionalIterator I> class reverse_iterator;
template <class I1, class I2>
requires EqualityComparableWith<I1, I2>
constexpr bool operator==(
const reverse_iterator<I1>& x,
const reverse_iterator<I2>& y);
template <class I1, class I2>
requires EqualityComparableWith<I1, I2>
constexpr bool operator!=(
const reverse_iterator<I1>& x,
const reverse_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator<(
const reverse_iterator<I1>& x,
const reverse_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator>(
const reverse_iterator<I1>& x,
const reverse_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator>=(
const reverse_iterator<I1>& x,
const reverse_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator<=(
const reverse_iterator<I1>& x,
const reverse_iterator<I2>& y);
template <class I1, class I2>
requires SizedSentinel<I1, I2>
constexpr difference_type_t<I2> operator-(
const reverse_iterator<I1>& x,
const reverse_iterator<I2>& y);
template <RandomAccessIterator I>
constexpr reverse_iterator<I> operator+(
difference_type_t<I> n,
const reverse_iterator<I>& x);
template <BidirectionalIterator I>
constexpr reverse_iterator<I> make_reverse_iterator(I i);
// \ref{iterators.insert}, insert iterators:
template <class Container> class back_insert_iterator;
template <class Container>
back_insert_iterator<Container> back_inserter(Container& x);
template <class Container> class front_insert_iterator;
template <class Container>
front_insert_iterator<Container> front_inserter(Container& x);
template <class Container> class insert_iterator;
template <class Container>
insert_iterator<Container> inserter(Container& x, iterator_t<Container> i);
// \ref{iterators.move}, move iterators and sentinels:
template <InputIterator I> class move_iterator;
template <class I1, class I2>
requires EqualityComparableWith<I1, I2>
constexpr bool operator==(
const move_iterator<I1>& x, const move_iterator<I2>& y);
template <class I1, class I2>
requires EqualityComparableWith<I1, I2>
constexpr bool operator!=(
const move_iterator<I1>& x, const move_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator<(
const move_iterator<I1>& x, const move_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator<=(
const move_iterator<I1>& x, const move_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator>(
const move_iterator<I1>& x, const move_iterator<I2>& y);
template <class I1, class I2>
requires StrictTotallyOrderedWith<I1, I2>
constexpr bool operator>=(
const move_iterator<I1>& x, const move_iterator<I2>& y);
template <class I1, class I2>
requires SizedSentinel<I1, I2>
constexpr difference_type_t<I2> operator-(
const move_iterator<I1>& x,
const move_iterator<I2>& y);
template <RandomAccessIterator I>
constexpr move_iterator<I> operator+(
difference_type_t<I> n,
const move_iterator<I>& x);
template <InputIterator I>
constexpr move_iterator<I> make_move_iterator(I i);
template <Semiregular S> class move_sentinel;
template <class I, Sentinel<I> S>
constexpr bool operator==(
const move_iterator<I>& i, const move_sentinel<S>& s);
template <class I, Sentinel<I> S>
constexpr bool operator==(
const move_sentinel<S>& s, const move_iterator<I>& i);
template <class I, Sentinel<I> S>
constexpr bool operator!=(
const move_iterator<I>& i, const move_sentinel<S>& s);
template <class I, Sentinel<I> S>
constexpr bool operator!=(
const move_sentinel<S>& s, const move_iterator<I>& i);
template <class I, SizedSentinel<I> S>
constexpr difference_type_t<I> operator-(
const move_sentinel<S>& s, const move_iterator<I>& i);
template <class I, SizedSentinel<I> S>
constexpr difference_type_t<I> operator-(
const move_iterator<I>& i, const move_sentinel<S>& s);
template <Semiregular S>
constexpr move_sentinel<S> make_move_sentinel(S s);
// \ref{iterators.common}, common iterators:
template <Iterator I, Sentinel<I> S>
requires !Same<I, S>
class common_iterator;
template <Readable I, class S>
struct value_type<common_iterator<I, S>>;
template <InputIterator I, class S>
struct iterator_category<common_iterator<I, S>>;
template <ForwardIterator I, class S>
struct iterator_category<common_iterator<I, S>>;
template <class I1, class I2, Sentinel<I2> S1, Sentinel<I1> S2>
bool operator==(
const common_iterator<I1, S1>& x, const common_iterator<I2, S2>& y);
template <class I1, class I2, Sentinel<I2> S1, Sentinel<I1> S2>
requires EqualityComparableWith<I1, I2>
bool operator==(
const common_iterator<I1, S1>& x, const common_iterator<I2, S2>& y);
template <class I1, class I2, Sentinel<I2> S1, Sentinel<I1> S2>
bool operator!=(
const common_iterator<I1, S1>& x, const common_iterator<I2, S2>& y);
template <class I2, SizedSentinel<I2> I1, SizedSentinel<I2> S1, SizedSentinel<I1> S2>
difference_type_t<I2> operator-(
const common_iterator<I1, S1>& x, const common_iterator<I2, S2>& y);
// \ref{default.sentinels}, default sentinels:
class default_sentinel;
// \ref{iterators.counted}, counted iterators:
template <Iterator I> class counted_iterator;
template <class I1, class I2>
requires Common<I1, I2>
constexpr bool operator==(
const counted_iterator<I1>& x, const counted_iterator<I2>& y);
constexpr bool operator==(
const counted_iterator<auto>& x, default_sentinel);
constexpr bool operator==(
default_sentinel, const counted_iterator<auto>& x);
template <class I1, class I2>
requires Common<I1, I2>
constexpr bool operator!=(
const counted_iterator<I1>& x, const counted_iterator<I2>& y);
constexpr bool operator!=(
const counted_iterator<auto>& x, default_sentinel y);
constexpr bool operator!=(
default_sentinel x, const counted_iterator<auto>& y);
template <class I1, class I2>
requires Common<I1, I2>
constexpr bool operator<(
const counted_iterator<I1>& x, const counted_iterator<I2>& y);
template <class I1, class I2>
requires Common<I1, I2>
constexpr bool operator<=(
const counted_iterator<I1>& x, const counted_iterator<I2>& y);
template <class I1, class I2>
requires Common<I1, I2>
constexpr bool operator>(
const counted_iterator<I1>& x, const counted_iterator<I2>& y);
template <class I1, class I2>
requires Common<I1, I2>
constexpr bool operator>=(
const counted_iterator<I1>& x, const counted_iterator<I2>& y);
template <class I1, class I2>
requires Common<I1, I2>
constexpr difference_type_t<I2> operator-(
const counted_iterator<I1>& x, const counted_iterator<I2>& y);
template <class I>
constexpr difference_type_t<I> operator-(
const counted_iterator<I>& x, default_sentinel y);
template <class I>
constexpr difference_type_t<I> operator-(
default_sentinel x, const counted_iterator<I>& y);
template <RandomAccessIterator I>
constexpr counted_iterator<I>
operator+(difference_type_t<I> n, const counted_iterator<I>& x);
template <Iterator I>
constexpr counted_iterator<I> make_counted_iterator(I i, difference_type_t<I> n);
// \ref{unreachable.sentinels}, unreachable sentinels:
class unreachable;
template <Iterator I>
constexpr bool operator==(const I&, unreachable) noexcept;
template <Iterator I>
constexpr bool operator==(unreachable, const I&) noexcept;
template <Iterator I>
constexpr bool operator!=(const I&, unreachable) noexcept;
template <Iterator I>
constexpr bool operator!=(unreachable, const I&) noexcept;
// \ref{dangling.wrappers}, dangling wrapper:
template <class T> class dangling;
// \ref{iterators.stream}, stream iterators:
template <class T, class charT = char, class traits = char_traits<charT>,
class Distance = ptrdiff_t>
class istream_iterator;
template <class T, class charT, class traits, class Distance>
bool operator==(const istream_iterator<T, charT, traits, Distance>& x,
const istream_iterator<T, charT, traits, Distance>& y);
template <class T, class charT, class traits, class Distance>
bool operator==(default_sentinel x,
const istream_iterator<T, charT, traits, Distance>& y);
template <class T, class charT, class traits, class Distance>
bool operator==(const istream_iterator<T, charT, traits, Distance>& x,
default_sentinel y);
template <class T, class charT, class traits, class Distance>
bool operator!=(const istream_iterator<T, charT, traits, Distance>& x,
const istream_iterator<T, charT, traits, Distance>& y);
template <class T, class charT, class traits, class Distance>
bool operator!=(default_sentinel x,
const istream_iterator<T, charT, traits, Distance>& y);
template <class T, class charT, class traits, class Distance>
bool operator!=(const istream_iterator<T, charT, traits, Distance>& x,
default_sentinel y);
template <class T, class charT = char, class traits = char_traits<charT>>
class ostream_iterator;
template <class charT, class traits = char_traits<charT> >
class istreambuf_iterator;
template <class charT, class traits>
bool operator==(const istreambuf_iterator<charT, traits>& a,
const istreambuf_iterator<charT, traits>& b);
template <class charT, class traits>
bool operator==(default_sentinel a,
const istreambuf_iterator<charT, traits>& b);
template <class charT, class traits>
bool operator==(const istreambuf_iterator<charT, traits>& a,
default_sentinel b);
template <class charT, class traits>
bool operator!=(const istreambuf_iterator<charT, traits>& a,
const istreambuf_iterator<charT, traits>& b);
template <class charT, class traits>
bool operator!=(default_sentinel a,
const istreambuf_iterator<charT, traits>& b);
template <class charT, class traits>
bool operator!=(const istreambuf_iterator<charT, traits>& a,
default_sentinel b);
template <class charT, class traits = char_traits<charT> >
class ostreambuf_iterator;
}}}}
namespace std {
// \ref{iterator.stdtraits}, iterator traits:
template <experimental::ranges::Iterator Out>
struct iterator_traits<Out>;
template <experimental::ranges::InputIterator In>
struct iterator_traits<In>;
template <experimental::ranges::InputIterator In>
requires experimental::ranges::Sentinel<In, In>
struct iterator_traits;
}
\end{codeblock}
\rSec1[iterator.requirements]{Iterator requirements}
\rSec2[iterator.requirements.general]{General}
\pnum
\indextext{requirements!iterator}%
Iterators are a generalization of pointers that allow a \Cpp program to work with different data structures
(for example, containers and ranges) in a uniform manner.
To be able to construct template algorithms that work correctly and
efficiently on different types of data structures, the library formalizes not just the interfaces but also the
semantics and complexity assumptions of iterators.
All input iterators
\tcode{i}
support the expression
\tcode{*i},
resulting in a value of some object type
\tcode{T},
called the
\term{value type}
of the iterator.
All output iterators support the expression
\tcode{*i = o}
where
\tcode{o}
is a value of some type that is in the set of types that are
\term{writable}
to the particular iterator type of
\tcode{i}.
For every iterator type
\tcode{X}
there is a corresponding signed integer type called the
\term{difference type}
of the iterator.
\pnum
Since iterators are an abstraction of pointers, their semantics are
a generalization of most of the semantics of pointers in \Cpp.
This ensures that every
function template
that takes iterators
works as well with regular pointers.
This document defines
five categories of iterators, according to the operations
defined on them:
\techterm{input iterators},
\techterm{output iterators},
\techterm{forward iterators},
\techterm{bidirectional iterators}
and
\techterm{random access iterators},
as shown in Table~\ref{tab:iterators.relations}.
\begin{floattable}{Relations among iterator categories}{tab:iterators.relations}
{llll}
\topline
\textbf{Random Access} & $\rightarrow$ \textbf{Bidirectional} &
$\rightarrow$ \textbf{Forward} & $\rightarrow$ \textbf{Input} \\
& & & $\rightarrow$ \textbf{Output} \\
\end{floattable}
\pnum
The five categories of iterators correspond to the iterator concepts
\tcode{Input\-Iterator},
\tcode{Output\-Iterator},
\tcode{Forward\-Iterator},
\tcode{Bidirectional\-Iterator}, and
\tcode{RandomAccess\-Iterator}, respectively. The generic term \techterm{iterator} refers to
any type that satisfies \tcode{Iterator}.
\pnum
Forward iterators satisfy all the requirements of input
iterators and can be used whenever an input iterator is specified;
Bidirectional iterators also satisfy all the requirements of
forward iterators and can be used whenever a forward iterator is specified;
Random access iterators also satisfy all the requirements of bidirectional
iterators and can be used whenever a bidirectional iterator is specified.
\pnum
Iterators that further satisfy the requirements of output iterators are
called \defn{mutable iterator}{s}. Nonmutable iterators are referred to
as \defn{constant iterator}{s}.
\pnum
Just as a regular pointer to an array guarantees that there is a pointer value pointing past the last element
of the array, so for any iterator type there is an iterator value that points past the last element of a
corresponding sequence.
These values are called
\term{past-the-end}
values.
Values of an iterator
\tcode{i}
for which the expression
\tcode{*i}
is defined are called
\term{dereferenceable}.
The library never assumes that past-the-end values are dereferenceable.
Iterators can also have singular values that are not associated with any
sequence.
\enterexample
After the declaration of an uninitialized pointer
\tcode{x}
(as with
\tcode{int* x;}),
\tcode{x}
must always be assumed to have a singular value of a pointer.
\exitexample
Results of most expressions are undefined for singular values;
the only exceptions are destroying an iterator that holds a singular value,
the assignment of a non-singular value to
an iterator that holds a singular value, and using a value-initialized iterator
as the source of a copy or move operation. \enternote This guarantee is not
offered for default initialization, although the distinction only matters for types
with trivial default constructors such as pointers or aggregates holding pointers.
\exitnote
In these cases the singular
value is overwritten the same way as any other value.
Dereferenceable
values are always non-singular.
\pnum
Most of the library's algorithmic templates that operate on data structures have
interfaces that use ranges. A range is an iterator and a \term{sentinel} that designate
the beginning and end of the computation, or an iterator and a count that designate
the beginning and the number of elements to which the computation is to be applied.
\pnum
An iterator and a sentinel denoting a range are comparable. The types of a sentinel
and an iterator that denote a range must satisfy
\tcode{Sentinel}~(\ref{iterators.sentinel}).
A range \range{i}{s}
is empty if \tcode{i == s};
otherwise, \range{i}{s}
refers to the elements in the data structure starting with the element
pointed to by
\tcode{i}
and up to but not including the element pointed to by
the first iterator \tcode{j} such that \tcode{j == s}.
\pnum
A sentinel
\tcode{s}
is called
\term{reachable}
from an iterator
\tcode{i}
if and only if there is a finite sequence of applications of
the expression
\tcode{++i}
that makes
\tcode{i == s}.
If
\tcode{s}
is reachable from
\tcode{i},
\range{i}{s} denotes a range.
\pnum
A counted range \range{i}{n} is empty if \tcode{n == 0}; otherwise, \range{i}{n}
refers to the \tcode{n} elements in the data structure starting with the element
pointed to by \tcode{i} and up to but not including the element pointed to by the
result of incrementing \tcode{i} \tcode{n} times.
\pnum
A range \range{i}{s}
is valid if and only if
\tcode{s}
is reachable from
\tcode{i}.
A counted range \range{i}{n} is valid if and only if \tcode{n == 0}; or \tcode{n}
is positive, \tcode{i} is dereferenceable, and \range{++i}{-{-}n} is valid.
The result of the application of functions in the library to invalid ranges is
undefined.
\pnum
All the categories of iterators require only those functions that are realizable for a given category in
constant time (amortized).
\pnum
Destruction of an iterator may invalidate pointers and references
previously obtained from that iterator.
\pnum
An
\techterm{invalid}
iterator is an iterator that may be singular.\footnote{This definition applies to pointers, since pointers are iterators.
The effect of dereferencing an iterator that has been invalidated
is undefined.
}
\rSec2[iterator.custpoints]{Customization points}
\rSec3[iterator.custpoints.iter_move]{\tcode{iter_move}}
\pnum
The name \tcode{iter_move} denotes a \techterm{customization point
object}~(\ref{customization.point.object}). The expression
\tcode{ranges::iter_move(E)} for some subexpression \tcode{E} is expression-equivalent to the
following:
\begin{itemize}
\item \tcode{static_cast<decltype(iter_move(E))>(iter_move(E))}, if that expression is well-formed when evaluated in
a context that does not include \tcode{ranges::iter_move} but does include the
lookup set produced by argument-dependent lookup~(\cxxref{basic.lookup.argdep}).
\item Otherwise, if the expression \tcode{*E} is well-formed:
\begin{itemize}
\item if \tcode{*E} is an lvalue, \tcode{std::move(*E)};
\item otherwise, \tcode{static_cast<decltype(*E)>(*E)}.
\end{itemize}
\item Otherwise, \tcode{ranges::iter_move(E)} is ill-formed.
\end{itemize}
\pnum
If \tcode{ranges::iter_move(E)} does not equal \tcode{*E}, the program is
ill-formed with no diagnostic required.
\rSec3[iterator.custpoints.iter_swap]{\tcode{iter_swap}}
\pnum
The name \tcode{iter_swap} denotes a \techterm{customization point
object}~(\ref{customization.point.object}). The expression
\tcode{ranges::iter_swap(E1, E2)} for some subexpressions \tcode{E1} and \tcode{E2}
is expression-equivalent to the following:
\begin{itemize}
\item \tcode{(void)iter_swap(E1, E2)}, if that expression is well-formed when
evaluated in a context that does not include \tcode{ranges::iter_swap} but does
include the lookup set produced by argument-dependent
lookup~(\cxxref{basic.lookup.argdep}) and the following declaration:
\begin{codeblock}
void iter_swap(auto, auto) = delete;
\end{codeblock}
\item Otherwise, if the types of \tcode{E1} and \tcode{E2} both satisfy
\tcode{Readable}, and if the reference type of \tcode{E1} is swappable
with~(\ref{concepts.lib.corelang.swappable}) the reference type of \tcode{E2},
then \tcode{ranges::swap(*E1, *E2)}
\item Otherwise, if the types \tcode{T1} and \tcode{T2} of \tcode{E1} and
\tcode{E2} satisfy \tcode{IndirectlyMovableStorable<T1, T2> \&\&
IndirectlyMovableStorable<T2, T1>}, \tcode{(void)(*E1 = iter_exchange_move(E2, E1))},
except that \tcode{E1} is evaluated only once.
\item Otherwise, \tcode{ranges::iter_swap(E1, E2)} is ill-formed.
\end{itemize}
\pnum
If \tcode{ranges::iter_swap(E1, E2)} does not swap the values denoted by the
expressions \tcode{E1} and \tcode{E2}, the program is ill-formed with no
diagnostic required.
\pnum
\tcode{iter_exchange_move} is an exposition-only function specified as:
\begin{itemdecl}
template <class X, class Y>
constexpr value_type_t<remove_reference_t<X>> iter_exchange_move(X&& x, Y&& y)
noexcept(@\seebelow@);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects Equivalent to:
\begin{codeblock}
value_type_t<remove_reference_t<X>> old_value(iter_move(x));
*x = iter_move(y);
return old_value;
\end{codeblock}
\pnum
\remarks The expression in the \tcode{noexcept} is equivalent to:
\begin{codeblock}
NE(remove_reference_t<X>, remove_reference_t<Y>) &&
NE(remove_reference_t<Y>, remove_reference_t<X>)
\end{codeblock}
Where \tcode{NE(T1, T2)} is the expression:
\begin{codeblock}
is_nothrow_constructible<value_type_t<T1>, rvalue_reference_t<T1>>::value &&
is_nothrow_assignable<value_type_t<T1>&, rvalue_reference_t<T1>>::value &&
is_nothrow_assignable<reference_t<T1>, rvalue_reference_t<T2>>::value &&
is_nothrow_assignable<reference_t<T1>, value_type_t<T2>>::value> &&
is_nothrow_move_constructible<value_type_t<T1>>::value &&
noexcept(ranges::iter_move(declval<T1&>()))
\end{codeblock}
\end{itemdescr}
\rSec2[iterator.assoc.types]{Iterator associated types}
\pnum
To implement algorithms only in terms of iterators, it is often necessary to
determine the value and
difference types that correspond to a particular iterator type.
Accordingly, it is required that if
\tcode{WI} is the name of a type that
satisfies the \tcode{WeaklyIncrementable} concept~(\ref{iterators.weaklyincrementable}),
\tcode{R} is the name of a type that
satisfies the \tcode{Readable} concept~(\ref{iterators.readable}), and
\tcode{II} is the name of a type that satisfies the
\tcode{InputIterator} concept~(\ref{iterators.input}) concept, the types
\begin{codeblock}
difference_type_t<WI>
value_type_t<R>
iterator_category_t<II>
\end{codeblock}
be defined as the iterator's difference type, value type and iterator category, respectively.
\rSec3[iterator.assoc.types.difference_type]{\tcode{difference_type}}
\pnum
\indexlibrary{\idxcode{difference_type_t}}%
\tcode{difference_type_t<T>} is implemented as if:
\indexlibrary{\idxcode{difference_type}}%
\begin{codeblock}
template <class> struct difference_type { };
template <class T>
struct difference_type<T*>
: enable_if<is_object<T>::value, ptrdiff_t> { };
template <class I>
struct difference_type<const I> : difference_type<decay_t<I>> { };
template <class T>
requires requires { typename T::difference_type; }
struct difference_type<T> {
using type = typename T::difference_type;
};
template <class T>
requires !requires { typename T::difference_type; } &&
requires(const T& a, const T& b) { { a - b } -> Integral; }
struct difference_type<T>
: make_signed< decltype(declval<T>() - declval<T>()) > {
};
template <class T> using difference_type_t
= typename difference_type<T>::type;
\end{codeblock}
\pnum
Users may specialize \tcode{difference_type} on user-defined types.
\rSec3[iterator.assoc.types.value_type]{\tcode{value_type}}
\pnum
A \tcode{Readable} type has an associated value type that can be accessed with the
\tcode{value_type_t} alias template.
\indexlibrary{\idxcode{value_type}}%
\begin{codeblock}
template <class> struct value_type { };
template <class T>
struct value_type<T*>
: enable_if<is_object<T>::value, remove_cv_t<T>> { };
template <class I>
requires is_array<I>::value
struct value_type<I> : value_type<decay_t<I>> { };
template <class I>
struct value_type<const I> : value_type<decay_t<I>> { };
template <class T>
requires requires { typename T::value_type; }
struct value_type<T>
: enable_if<is_object<typename T::value_type>::value, typename T::value_type> { };
template <class T>
requires requires { typename T::element_type; }
struct value_type<T>
: enable_if<
is_object<typename T::element_type>::value,
remove_cv_t<typename T::element_type>>
{ };
template <class T> using value_type_t
= typename value_type<T>::type;
\end{codeblock}
\pnum
If a type \tcode{I} has an associated value type, then \tcode{value_type<I>::type} shall name the
value type. Otherwise, there shall be no nested type \tcode{type}.
\pnum
The \tcode{value_type} class template may be specialized on user-defined types.
\pnum
When instantiated with a type \tcode{I}
such that \tcode{I::value_type} is valid and denotes a type,
\tcode{value_type<I>::type} names that type, unless it is not an object type~(\cxxref{basic.types}) in which case
\tcode{value_type<I>} shall have no nested type \tcode{type}. \enternote Some legacy output
iterators define a nested type named \tcode{value_type} that is an alias for \tcode{void}. These
types are not \tcode{Readable} and have no associated value types.\exitnote
\pnum
When instantiated with a type \tcode{I}
such that \tcode{I::element_type} is valid and denotes a type,
\tcode{value_type<I>::\brk{}type} names the type \tcode{remove_cv_t<I::element_type>}, unless it is
not an object type~(\cxxref{basic.types}) in which case
\tcode{value_type<I>} shall have no nested type \tcode{type}. \enternote Smart pointers like
\tcode{shared_ptr<int>} are \tcode{Readable} and have an associated value type. But a smart pointer
like \tcode{shared_ptr<void>} is not \tcode{Readable} and has no associated value type.\exitnote
\rSec3[iterator.assoc.types.iterator_category]{\tcode{iterator_category}}
\pnum
\indexlibrary{\idxcode{iterator_category_t}}%
\tcode{iterator_category_t<T>}
is implemented as if:
\indexlibrary{\idxcode{iterator_category}}%
\begin{codeblock}
template <class> struct iterator_category { };
template <class T>
struct iterator_category<T*>
: enable_if<is_object<T>::value, random_access_iterator_tag> { };
template <class T>
struct iterator_category<T const> : iterator_category<T> { };
template <class T>
requires requires { typename T::iterator_category; }
struct iterator_category<T> {
using type = @\seebelow@;
};
template <class T> using iterator_category_t
= typename iterator_category<T>::type;
\end{codeblock}
\pnum
Users may specialize \tcode{iterator_category} on user-defined types.
\pnum
If
\tcode{T::iterator_category} is valid and denotes a type, then the
type \tcode{iterator_category<T>::type} is computed as follows:
\begin{itemize}
\item If \tcode{T::iterator_category} is the same as or derives from \tcode{std::random_access_iterator_tag},
\tcode{iter\-ator_category<T>::type} is \tcode{ranges::random_access_iterator_tag}.
\item Otherwise, if \tcode{T::iterator_category} is the same as or derives from \tcode{std::bidirectional_iterator_tag},
\tcode{iterator_category<T>::type} is \tcode{ranges::bidirectional_iterator_tag}.
\item Otherwise, if \tcode{T::iterator_category} is the same as or derives from \tcode{std::forward_iterator_tag},
\tcode{iterator_category<T>::type} is \tcode{ranges::forward_iterator_tag}.
\item Otherwise, if \tcode{T::iterator_category} is the same as or derives from \tcode{std::input_iterator_tag},
\tcode{iterator_category<T>::type} is \tcode{ranges::input_iterator_tag}.
\item Otherwise, if \tcode{T::iterator_category} is the same as or derives from \tcode{std::output_iterator_tag},
\tcode{iterator_category<T>} has no nested \tcode{type}.
\item Otherwise, \tcode{iterator_category<T>::type} is \tcode{T::iterator_category}
\end{itemize}
\pnum
\indexlibrary{\idxcode{rvalue_reference_t}}%
\tcode{rvalue_reference_t<T>} is implemented as if:
\begin{itemdecl}
template <@\placeholder{dereferenceable}@ T>
requires @\seebelow{ }@using rvalue_reference_t
= decltype(ranges::iter_move(declval<T&>()));
\end{itemdecl}
\begin{itemdescr}
\pnum
The expression in the \tcode{requires} clause is equivalent to:
\begin{codeblock}
requires(T& t) { { ranges::iter_move(t) } -> auto&&; }
\end{codeblock}
\end{itemdescr}
\rSec2[iterators.readable]{Concept \tcode{Readable}}
\pnum
The \tcode{Readable} concept is satisfied by types that are readable by
applying \tcode{operator*} including pointers, smart pointers, and iterators.
\indexlibrary{\idxcode{Readable}}%
\begin{codeblock}
template <class In>
concept bool Readable =
requires {
typename value_type_t<In>;
typename reference_t<In>;
typename rvalue_reference_t<In>;
} &&
CommonReference<reference_t<In>&&, value_type_t<In>&> &&
CommonReference<reference_t<In>&&, rvalue_reference_t<In>&&> &&
CommonReference<rvalue_reference_t<In>&&, const value_type_t<In>&>;
\end{codeblock}
\rSec2[iterators.writable]{Concept \tcode{Writable}}
\pnum
The \tcode{Writable} concept specifies the requirements for writing a value into an iterator's
referenced object.