forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 5
/
strings.tex
5532 lines (4744 loc) · 166 KB
/
strings.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[strings]{Strings library}
\rSec1[strings.general]{General}
\pnum
This Clause describes components for manipulating sequences of
any non-array trivially copyable standard-layout\iref{term.standard.layout.type} type \tcode{T}
where \tcode{is_trivially_default_constructible_v<T>} is \tcode{true}.
Such types are called \defnx{char-like types}{char-like type},
and objects of
char-like types are called \defnx{char-like objects}{char-like object} or
simply \term{characters}.
\pnum
The following subclauses describe a
character traits class, string classes, and
null-terminated sequence utilities,
as summarized in \tref{strings.summary}.
\begin{libsumtab}[x{2.1in}]{Strings library summary}{strings.summary}
\ref{char.traits} & Character traits & \tcode{<string>} \\
\ref{string.view} & String view classes & \tcode{<string_view>} \\ \rowsep
\ref{string.classes} & String classes & \tcode{<string>} \\ \rowsep
\ref{c.strings} & Null-terminated sequence utilities & \tcode{<cstring>} \\
\end{libsumtab}
\rSec1[char.traits]{Character traits}
\rSec2[char.traits.general]{General}
\pnum
Subclause \ref{char.traits} defines requirements on classes representing
\term{character traits},
and defines a class template
\tcode{char_traits<charT>},
along with five specializations,
\tcode{char_traits<char>},
\tcode{char_traits<char8_t>},
\tcode{char_traits<char16_t>},
\tcode{char_traits<char32_t>},
and
\tcode{char_traits<wchar_t>},
that meet those requirements.
\pnum
Most classes specified in \ref{string.classes}, \ref{string.view},
and \ref{input.output} need a set of related types and functions to complete
the definition of their semantics. These types and functions are provided as a
set of member \grammarterm{typedef-name}{s} and functions in the template
parameter \tcode{traits} used by each such template.
Subclause \ref{char.traits} defines the semantics of these members.
\pnum
To specialize those templates to generate a string, string view, or
iostream class to handle a particular character container type\iref{defns.character.container}
\tcode{C},
that and its related character traits class
\tcode{X}
are passed as a pair of parameters to the string, string view, or iostream template as
parameters
\tcode{charT}
and
\tcode{traits}.
If
\tcode{X::char_type}
is not the same type as
\tcode{C}, the program is ill-formed.
\rSec2[char.traits.require]{Character traits requirements}
\pnum
In \tref{char.traits.req},
\tcode{X}
denotes a traits class defining types and functions for the
character container type
\tcode{C};
\tcode{c}
and
\tcode{d}
denote values of type
\tcode{C};
\tcode{p}
and
\tcode{q}
denote values of type
\tcode{const C*};
\tcode{s}
denotes a value of type
\tcode{C*};
\tcode{n},
\tcode{i}
and
\tcode{j}
denote values of type
\tcode{size_t};
\tcode{e}
and
\tcode{f}
denote values of type
\tcode{X::int_type};
\tcode{pos}
denotes a value of type
\tcode{X::pos_type};
and
\tcode{r}
denotes an lvalue of type
\tcode{C}.
No expression which is part of the character traits requirements
specified in \ref{char.traits.require}
shall exit via an exception.
\begin{libreqtab4d}
{Character traits requirements}
{char.traits.req}
\\ \topline
\lhdr{Expression} & \chdr{Return type} & \chdr{Assertion/note} & \rhdr{Complexity}\\
& & \chdr{pre-/post-condition} & \\ \capsep
\endfirsthead
\continuedcaption\\
\topline
\lhdr{Expression} & \chdr{Return type} & \chdr{Assertion/note} & \rhdr{Complexity}\\
& & \chdr{pre-/post-condition} & \\ \capsep
\endhead
\tcode{X::char_type} & \tcode{C} &
& \\ \rowsep
\tcode{X::int_type} & &
(described in~\ref{char.traits.typedefs}) & \\ \rowsep
\tcode{X::off_type} & &
(described in~\ref{iostreams.limits.pos} and \ref{iostream.forward}) & \\ \rowsep
\tcode{X::pos_type} & &
(described in~\ref{iostreams.limits.pos} and \ref{iostream.forward}) & \\ \rowsep
\tcode{X::state_type} & &
(described in~\ref{char.traits.typedefs}) & \\ \rowsep
\tcode{X::eq(c,d)} & \tcode{bool} &
\returns
whether \tcode{c} is to be treated as equal to \tcode{d}. & constant \\ \rowsep
\tcode{X::lt(c,d)} & \tcode{bool} &
\returns
whether \tcode{c} is to be treated as less than \tcode{d}. & constant \\ \rowsep
\tcode{X::compare(p,q,n)} & \tcode{int} &
\returns
\tcode{0} if for each \tcode{i} in \tcode{[0,n)}, \tcode{X::eq(p[i],q[i])}
is \tcode{true}; else, a negative value if, for some \tcode{j} in \tcode{[0,n)},
\tcode{X::lt(p[j],q[j])} is \tcode{true} and for each \tcode{i} in \tcode{[0,j)}
\tcode{X::eq(p[i],q[i])} is \tcode{true}; else a positive value. & linear \\ \rowsep
\tcode{X::length(p)} & \tcode{size_t} &
\returns
the smallest \tcode{i} such that \tcode{X::eq(p[i],charT())} is \tcode{true}. & linear \\ \rowsep
\tcode{X::find(p,n,c)} & \tcode{const X::char_type*} &
\returns
the smallest \tcode{q} in \tcode{[p,p+n)} such that
\tcode{X::eq(*q,c)} is \tcode{true}, \tcode{nullptr} otherwise. & linear \\ \rowsep
\tcode{X::move(s,p,n)} & \tcode{X::char_type*} &
for each \tcode{i} in \range{0}{n}, performs \tcode{X::assign(s[i],p[i])}.
Copies correctly even where the ranges \range{p}{p+n} and \range{s}{s+n} overlap.\br \returns \tcode{s}. & linear \\ \rowsep
\tcode{X::copy(s,p,n)} & \tcode{X::char_type*} &
\expects
The ranges \range{p}{p+n} and \range{s}{s+n} do not overlap.\par
\returns
\tcode{s}.\br
for each \tcode{i} in
\range{0}{n}, performs \tcode{X::assign(s[i],p[i])}. & linear \\ \rowsep
\tcode{X::assign(r,d)} & (not used) &
assigns \tcode{r=d}. & constant \\ \rowsep
\tcode{X::assign\-(s,n,c)} & \tcode{X::char_type*} &
for each \tcode{i} in \range{0}{n}, performs
\tcode{X::assign(s[i],c)}.\br
\returns
\tcode{s}. & linear \\ \rowsep
\tcode{X::not_eof(e)} & \tcode{int_type} &
\returns
\tcode{e} if \tcode{X::eq_int_type(e,X::eof())} is \tcode{false},
otherwise a value \tcode{f} such that
\tcode{X::eq_int_type(f,X::eof())} is \tcode{false}. & constant \\ \rowsep
\tcode{X::to_char_type\-(e)} & \tcode{X::char_type} &
\returns
if for some \tcode{c}, \tcode{X::eq_int_type(e,X::to_int_type(c))}
is \tcode{true}, \tcode{c}; else some unspecified value. & constant \\ \rowsep
\tcode{X::to_int_type\-(c)} & \tcode{X::int_type} &
\returns
some value \tcode{e}, constrained by the definitions of
\tcode{to_char_type} and \tcode{eq_int_type}. & constant \\ \rowsep
\tcode{X::eq_int_type\-(e,f)} & \tcode{bool} &
\returns
for all \tcode{c} and \tcode{d}, \tcode{X::eq(c,d)} is equal to
\tcode{X::eq_int_type(X::to_int_type(c), X::to_int_type(d))}; otherwise, yields \tcode{true}
if \tcode{e} and \tcode{f} are both copies of \tcode{X::eof()}; otherwise, yields \tcode{false} if
one of \tcode{e} and \tcode{f} is a copy of \tcode{X::eof()} and the other is not; otherwise
the value is unspecified. & constant \\ \rowsep
\tcode{X::eof()} & \tcode{X::int_type} &
\returns
a value \tcode{e} such that \tcode{X::eq_int_type(e,X::to_int_type(c))}
is \tcode{false} for all values \tcode{c}. & constant \\
\end{libreqtab4d}
\pnum
The class template
\indexlibraryglobal{char_traits}%
\begin{codeblock}
template<class charT> struct char_traits;
\end{codeblock}
is provided in the header \libheaderref{string}
as a basis for explicit specializations.
\rSec2[char.traits.typedefs]{Traits typedefs}
\indexlibrarymember{char_type}{char_traits}%
\indexlibrarymember{int_type}{char_traits}%
\begin{itemdecl}
using int_type = @\seebelow@;
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
\tcode{int_type}
shall be able to represent all of the
valid characters converted from the corresponding
\tcode{char_type}
values, as well as an end-of-file value,
\tcode{eof()}.
\begin{footnote}
If
\tcode{eof()}
can be held in
\tcode{char_type}
then some iostreams operations can give surprising results.
\end{footnote}
\end{itemdescr}
\indexlibrarymember{state_type}{char_traits}%
\begin{itemdecl}
using state_type = @\seebelow@;
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
\tcode{state_type} meets the
\oldconceptref{Destructible} (\tref{cpp17.destructible}),
\oldconceptref{CopyAssignable} (\tref{cpp17.copyassignable}),
\oldconceptref{CopyConstructible} (\tref{cpp17.copyconstructible}), and
\oldconceptref{DefaultConstructible} (\tref{cpp17.defaultconstructible}) requirements.
\end{itemdescr}
\rSec2[char.traits.specializations]{\tcode{char_traits} specializations}
\rSec3[char.traits.specializations.general]{General}
\indexlibraryglobal{char_traits}%
\begin{codeblock}
namespace std {
template<> struct char_traits<char>;
template<> struct char_traits<char8_t>;
template<> struct char_traits<char16_t>;
template<> struct char_traits<char32_t>;
template<> struct char_traits<wchar_t>;
}
\end{codeblock}
\pnum
The header \libheader{string}
defines five specializations of the class template
\tcode{char_traits}:
\tcode{char_traits<\brk{}char>},
\tcode{char_traits<char8_t>},
\tcode{char_traits<char16_t>},
\tcode{char_traits<char32_t>},
and
\tcode{char_traits<wchar_t>}.
\rSec3[char.traits.specializations.char]{\tcode{struct char_traits<char>}}
\indexlibraryglobal{char_traits}%
\begin{codeblock}
namespace std {
template<> struct char_traits<char> {
using char_type = char;
using int_type = int;
using off_type = streamoff;
using pos_type = streampos;
using state_type = mbstate_t;
using comparison_category = strong_ordering;
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
static constexpr bool eq(char_type c1, char_type c2) noexcept;
static constexpr bool lt(char_type c1, char_type c2) noexcept;
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
static constexpr size_t length(const char_type* s);
static constexpr const char_type* find(const char_type* s, size_t n,
const char_type& a);
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
static constexpr int_type not_eof(int_type c) noexcept;
static constexpr char_type to_char_type(int_type c) noexcept;
static constexpr int_type to_int_type(char_type c) noexcept;
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
static constexpr int_type eof() noexcept;
};
}
\end{codeblock}
\pnum
The type \tcode{mbstate_t} is defined in \libheaderref{cwchar}
and can represent any of the conversion states that can occur in an
\impldef{supported multibyte character encoding rules} set of supported multibyte
character encoding rules.
\pnum
The two-argument member \tcode{assign} is defined identically to the
built-in operator \tcode{=}. The two-argument members \tcode{eq}
and \tcode{lt} are defined identically to the built-in operators
\tcode{==} and \tcode{<} for type \tcode{unsigned char}.
\pnum
The member
\tcode{eof()}
returns
\tcode{EOF}.
\rSec3[char.traits.specializations.char8.t]{\tcode{struct char_traits<char8_t>}}
\indexlibraryglobal{char_traits}%
\begin{codeblock}
namespace std {
template<> struct char_traits<char8_t> {
using char_type = char8_t;
using int_type = unsigned int;
using off_type = streamoff;
using pos_type = u8streampos;
using state_type = mbstate_t;
using comparison_category = strong_ordering;
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
static constexpr bool eq(char_type c1, char_type c2) noexcept;
static constexpr bool lt(char_type c1, char_type c2) noexcept;
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
static constexpr size_t length(const char_type* s);
static constexpr const char_type* find(const char_type* s, size_t n,
const char_type& a);
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
static constexpr int_type not_eof(int_type c) noexcept;
static constexpr char_type to_char_type(int_type c) noexcept;
static constexpr int_type to_int_type(char_type c) noexcept;
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
static constexpr int_type eof() noexcept;
};
}
\end{codeblock}
\pnum
The two-argument members \tcode{assign}, \tcode{eq}, and \tcode{lt}
are defined identically to
the built-in operators \tcode{=}, \tcode{==}, and \tcode{<} respectively.
\pnum
\indextext{UTF-8}%
The member \tcode{eof()} returns an
\impldef{return value of \tcode{char_traits<char8_t>::eof}} constant
that cannot appear as a valid UTF-8 code unit.
\rSec3[char.traits.specializations.char16.t]{\tcode{struct char_traits<char16_t>}}
\indexlibraryglobal{char_traits}%
\begin{codeblock}
namespace std {
template<> struct char_traits<char16_t> {
using char_type = char16_t;
using int_type = uint_least16_t;
using off_type = streamoff;
using pos_type = u16streampos;
using state_type = mbstate_t;
using comparison_category = strong_ordering;
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
static constexpr bool eq(char_type c1, char_type c2) noexcept;
static constexpr bool lt(char_type c1, char_type c2) noexcept;
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
static constexpr size_t length(const char_type* s);
static constexpr const char_type* find(const char_type* s, size_t n,
const char_type& a);
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
static constexpr int_type not_eof(int_type c) noexcept;
static constexpr char_type to_char_type(int_type c) noexcept;
static constexpr int_type to_int_type(char_type c) noexcept;
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
static constexpr int_type eof() noexcept;
};
}
\end{codeblock}
\pnum
The two-argument members \tcode{assign},
\tcode{eq}, and \tcode{lt} are defined identically to
the built-in operators \tcode{=}, \tcode{==}, and
\tcode{<}, respectively.
\pnum
The member \tcode{eof()} returns an
\impldef{return value of \tcode{char_traits<char16_t>::eof}} constant that cannot appear
as a valid UTF-16 code unit.
\rSec3[char.traits.specializations.char32.t]{\tcode{struct char_traits<char32_t>}}
\indexlibraryglobal{char_traits}%
\begin{codeblock}
namespace std {
template<> struct char_traits<char32_t> {
using char_type = char32_t;
using int_type = uint_least32_t;
using off_type = streamoff;
using pos_type = u32streampos;
using state_type = mbstate_t;
using comparison_category = strong_ordering;
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
static constexpr bool eq(char_type c1, char_type c2) noexcept;
static constexpr bool lt(char_type c1, char_type c2) noexcept;
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
static constexpr size_t length(const char_type* s);
static constexpr const char_type* find(const char_type* s, size_t n,
const char_type& a);
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
static constexpr int_type not_eof(int_type c) noexcept;
static constexpr char_type to_char_type(int_type c) noexcept;
static constexpr int_type to_int_type(char_type c) noexcept;
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
static constexpr int_type eof() noexcept;
};
}
\end{codeblock}
\pnum
The two-argument members \tcode{assign},
\tcode{eq}, and \tcode{lt} are defined identically to
the built-in operators \tcode{=}, \tcode{==}, and
\tcode{<}, respectively.
\pnum
The member \tcode{eof()} returns an
\impldef{return value of \tcode{char_traits<char32_t>::eof}} constant that cannot appear as a Unicode
code point.
\rSec3[char.traits.specializations.wchar.t]{\tcode{struct char_traits<wchar_t>}}
\indexlibraryglobal{char_traits}%
\begin{codeblock}
namespace std {
template<> struct char_traits<wchar_t> {
using char_type = wchar_t;
using int_type = wint_t;
using off_type = streamoff;
using pos_type = wstreampos;
using state_type = mbstate_t;
using comparison_category = strong_ordering;
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
static constexpr bool eq(char_type c1, char_type c2) noexcept;
static constexpr bool lt(char_type c1, char_type c2) noexcept;
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
static constexpr size_t length(const char_type* s);
static constexpr const char_type* find(const char_type* s, size_t n,
const char_type& a);
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
static constexpr int_type not_eof(int_type c) noexcept;
static constexpr char_type to_char_type(int_type c) noexcept;
static constexpr int_type to_int_type(char_type c) noexcept;
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
static constexpr int_type eof() noexcept;
};
}
\end{codeblock}
\pnum
The two-argument members
\tcode{assign},
\tcode{eq},
and
\tcode{lt}
are defined identically
to the built-in operators
\tcode{=},
\tcode{==},
and
\tcode{<},
respectively.
\pnum
The member
\tcode{eof()}
returns
\tcode{WEOF}.
\rSec1[string.view]{String view classes}
\rSec2[string.view.general]{General}
\pnum
The class template \tcode{basic_string_view} describes an object that can refer to a constant contiguous sequence of char-like\iref{strings.general} objects with the first element of the sequence at position zero.
In the rest of \ref{string.view}, the type of the char-like objects held in a \tcode{basic_string_view} object is designated by \tcode{charT}.
\pnum
\begin{note}
The library provides implicit conversions from \tcode{const charT*} and \tcode{std::basic_string<charT, ...>} to \tcode{std::basic_string_view<charT, ...>} so that user code can accept just \tcode{std::basic_string_view<charT>} as a non-templated parameter wherever a sequence of characters is expected.
User-defined types can define their own implicit conversions to \tcode{std::basic_string_view<charT>} in order to interoperate with these functions.
\end{note}
\rSec2[string.view.synop]{Header \tcode{<string_view>} synopsis}
\indexheader{string_view}%
\begin{codeblock}
// mostly freestanding
#include <compare> // see \ref{compare.syn}
namespace std {
// \ref{string.view.template}, class template \tcode{basic_string_view}
template<class charT, class traits = char_traits<charT>>
class basic_string_view; // partially freestanding
template<class charT, class traits>
constexpr bool ranges::@\libspec{enable_view}{basic_string_view}@<basic_string_view<charT, traits>> = true;
template<class charT, class traits>
constexpr bool ranges::@\libspec{enable_borrowed_range}{basic_string_view}@<basic_string_view<charT, traits>> = true;
// \ref{string.view.comparison}, non-member comparison functions
template<class charT, class traits>
constexpr bool operator==(basic_string_view<charT, traits> x,
type_identity_t<basic_string_view<charT, traits>> y) noexcept;
template<class charT, class traits>
constexpr @\seebelow@ operator<=>(basic_string_view<charT, traits> x,
@\itcorr@ type_identity_t<basic_string_view<charT,
@\itcorr@ traits>> y) noexcept;
// \ref{string.view.io}, inserters and extractors
template<class charT, class traits>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
basic_string_view<charT, traits> str); // hosted
// \tcode{basic_string_view} \grammarterm{typedef-name}s
using string_view = basic_string_view<char>;
using u8string_view = basic_string_view<char8_t>;
using u16string_view = basic_string_view<char16_t>;
using u32string_view = basic_string_view<char32_t>;
using wstring_view = basic_string_view<wchar_t>;
// \ref{string.view.hash}, hash support
template<class T> struct hash;
template<> struct hash<string_view>;
template<> struct hash<u8string_view>;
template<> struct hash<u16string_view>;
template<> struct hash<u32string_view>;
template<> struct hash<wstring_view>;
inline namespace literals {
inline namespace string_view_literals {
// \ref{string.view.literals}, suffix for \tcode{basic_string_view} literals
constexpr string_view operator""sv(const char* str, size_t len) noexcept;
constexpr u8string_view operator""sv(const char8_t* str, size_t len) noexcept;
constexpr u16string_view operator""sv(const char16_t* str, size_t len) noexcept;
constexpr u32string_view operator""sv(const char32_t* str, size_t len) noexcept;
constexpr wstring_view operator""sv(const wchar_t* str, size_t len) noexcept;
}
}
}
\end{codeblock}
\pnum
The function templates defined in \ref{utility.swap} and \ref{iterator.range}
are available when \tcode{<string_view>} is included.
\rSec2[string.view.template]{Class template \tcode{basic_string_view}}
\rSec3[string.view.template.general]{General}
\indexlibraryglobal{basic_string_view}%
\indexlibrarymember{traits_type}{basic_string_view}%
\indexlibrarymember{value_type}{basic_string_view}%
\indexlibrarymember{pointer}{basic_string_view}%
\indexlibrarymember{const_pointer}{basic_string_view}%
\indexlibrarymember{reference}{basic_string_view}%
\indexlibrarymember{const_reference}{basic_string_view}%
\indexlibrarymember{const_iterator}{basic_string_view}%
\indexlibrarymember{iterator}{basic_string_view}%
\indexlibrarymember{const_reverse_iterator}{basic_string_view}%
\indexlibrarymember{reverse_iterator}{basic_string_view}%
\indexlibrarymember{size_type}{basic_string_view}%
\indexlibrarymember{difference_type}{basic_string_view}%
\begin{codeblock}
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_string_view {
public:
// types
using traits_type = traits;
using value_type = charT;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using const_iterator = @\impdefx{type of \tcode{basic_string_view::const_iterator}}@; // see \ref{string.view.iterators}
using iterator = const_iterator;@
\begin{footnote}
Because \tcode{basic_string_view} refers to a constant sequence, \tcode{iterator} and \tcode{const_iterator} are the same type.
\end{footnote}@
using const_reverse_iterator = reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
using size_type = size_t;
using difference_type = ptrdiff_t;
static constexpr size_type npos = size_type(-1);
// \ref{string.view.cons}, construction and assignment
constexpr basic_string_view() noexcept;
constexpr basic_string_view(const basic_string_view&) noexcept = default;
constexpr basic_string_view& operator=(const basic_string_view&) noexcept = default;
constexpr basic_string_view(const charT* str);
basic_string_view(nullptr_t) = delete;
constexpr basic_string_view(const charT* str, size_type len);
template<class It, class End>
constexpr basic_string_view(It begin, End end);
template<class R>
constexpr explicit basic_string_view(R&& r);
// \ref{string.view.iterators}, iterator support
constexpr const_iterator begin() const noexcept;
constexpr const_iterator end() const noexcept;
constexpr const_iterator cbegin() const noexcept;
constexpr const_iterator cend() const noexcept;
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr const_reverse_iterator rend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;
// \ref{string.view.capacity}, capacity
constexpr size_type size() const noexcept;
constexpr size_type length() const noexcept;
constexpr size_type max_size() const noexcept;
constexpr bool empty() const noexcept;
// \ref{string.view.access}, element access
constexpr const_reference operator[](size_type pos) const;
constexpr const_reference at(size_type pos) const; // freestanding-deleted
constexpr const_reference front() const;
constexpr const_reference back() const;
constexpr const_pointer data() const noexcept;
// \ref{string.view.modifiers}, modifiers
constexpr void remove_prefix(size_type n);
constexpr void remove_suffix(size_type n);
constexpr void swap(basic_string_view& s) noexcept;
// \ref{string.view.ops}, string operations
constexpr size_type copy(charT* s, size_type n,
size_type pos = 0) const; // freestanding-deleted
constexpr basic_string_view substr(size_type pos = 0,
size_type n = npos) const; // freestanding-deleted
constexpr int compare(basic_string_view s) const noexcept;
constexpr int compare(size_type pos1, size_type n1,
basic_string_view s) const; // freestanding-deleted
constexpr int compare(size_type pos1, size_type n1, basic_string_view s,
size_type pos2, size_type n2) const; // freestanding-deleted
constexpr int compare(const charT* s) const;
constexpr int compare(size_type pos1, size_type n1,
const charT* s) const; // freestanding-deleted
constexpr int compare(size_type pos1, size_type n1, const charT* s,
size_type n2) const; // freestanding-deleted
constexpr bool starts_with(basic_string_view x) const noexcept;
constexpr bool starts_with(charT x) const noexcept;
constexpr bool starts_with(const charT* x) const;
constexpr bool ends_with(basic_string_view x) const noexcept;
constexpr bool ends_with(charT x) const noexcept;
constexpr bool ends_with(const charT* x) const;
constexpr bool contains(basic_string_view x) const noexcept;
constexpr bool contains(charT x) const noexcept;
constexpr bool contains(const charT* x) const;
// \ref{string.view.find}, searching
constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
constexpr size_type find(charT c, size_type pos = 0) const noexcept;
constexpr size_type find(const charT* s, size_type pos, size_type n) const;
constexpr size_type find(const charT* s, size_type pos = 0) const;
constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
constexpr size_type rfind(const charT* s, size_type pos, size_type n) const;
constexpr size_type rfind(const charT* s, size_type pos = npos) const;
constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const;
constexpr size_type find_first_of(const charT* s, size_type pos = 0) const;
constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const;
constexpr size_type find_last_of(const charT* s, size_type pos = npos) const;
constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
constexpr size_type find_first_not_of(const charT* s, size_type pos,
size_type n) const;
constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const;
constexpr size_type find_last_not_of(basic_string_view s,
size_type pos = npos) const noexcept;
constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
constexpr size_type find_last_not_of(const charT* s, size_type pos,
size_type n) const;
constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const;
private:
const_pointer data_; // \expos
size_type size_; // \expos
};
// \ref{string.view.deduct}, deduction guides
template<class It, class End>
basic_string_view(It, End) -> basic_string_view<iter_value_t<It>>;
template<class R>
basic_string_view(R&&) -> basic_string_view<ranges::range_value_t<R>>;
}
\end{codeblock}
\pnum
In every specialization \tcode{basic_string_view<charT, traits>}, the type \tcode{traits} shall meet the character traits requirements\iref{char.traits}.
\begin{note}
The program is ill-formed if \tcode{traits::char_type} is not the same type as \tcode{charT}.
\end{note}
\pnum
For a \tcode{basic_string_view str},
any operation that invalidates a pointer
in the range
\begin{codeblock}
@\range{str.data()}{str.data() + str.size()}@
\end{codeblock}
invalidates pointers, iterators, and references
to elements of \tcode{str}.
\pnum
The complexity of \tcode{basic_string_view} member functions is \bigoh{1}
unless otherwise specified.
\pnum
\tcode{basic_string_view<charT, traits>} is
a trivially copyable type\iref{term.trivially.copyable.type}.
\rSec3[string.view.cons]{Construction and assignment}
\indexlibraryctor{basic_string_view}%
\begin{itemdecl}
constexpr basic_string_view() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\ensures
\tcode{size_ == 0} and \tcode{data_ == nullptr}.
\end{itemdescr}
\indexlibraryctor{basic_string_view}%
\begin{itemdecl}
constexpr basic_string_view(const charT* str);
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
\range{str}{str + traits::length(str)} is a valid range.
\pnum
\effects
Constructs a \tcode{basic_string_view}, initializing \tcode{data_} with \tcode{str}
and initializing \tcode{size_} with \tcode{traits::length(str)}.
\pnum
\complexity
\bigoh{\tcode{traits::length(str)}}.
\end{itemdescr}
\indexlibraryctor{basic_string_view}%
\begin{itemdecl}
constexpr basic_string_view(const charT* str, size_type len);
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
\range{str}{str + len} is a valid range.
\pnum
\effects
Constructs a \tcode{basic_string_view}, initializing \tcode{data_} with \tcode{str}
and initializing \tcode{size_} with \tcode{len}.
\end{itemdescr}
\indexlibraryctor{basic_string_view}%
\begin{itemdecl}
template<class It, class End>
constexpr basic_string_view(It begin, End end);
\end{itemdecl}
\begin{itemdescr}
\pnum
\constraints
\begin{itemize}
\item \tcode{It} satisfies \libconcept{contiguous_iterator}.
\item \tcode{End} satisfies \tcode{\libconcept{sized_sentinel_for}<It>}.
\item \tcode{is_same_v<iter_value_t<It>, charT>} is \tcode{true}.
\item \tcode{is_convertible_v<End, size_type>} is \tcode{false}.
\end{itemize}
\pnum
\expects
\begin{itemize}
\item \range{begin}{end} is a valid range.
\item \tcode{It} models \libconcept{contiguous_iterator}.
\item \tcode{End} models \tcode{\libconcept{sized_sentinel_for}<It>}.
\end{itemize}
\pnum
\effects
Initializes \tcode{data_} with \tcode{to_address(begin)} and
initializes \tcode{size_} with \tcode{end - begin}.
\pnum
\throws
When and what \tcode{end - begin} throws.
\end{itemdescr}
\indexlibraryctor{basic_string_view}%
\begin{itemdecl}
template<class R>
constexpr explicit basic_string_view(R&& r);
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{d} be an lvalue of type \tcode{remove_cvref_t<R>}.
\pnum
\constraints
\begin{itemize}
\item
\tcode{remove_cvref_t<R>} is not the same type as \tcode{basic_string_view},
\item
\tcode{R} models
\tcode{ranges::\libconcept{contiguous_range}} and \tcode{ranges::\libconcept{sized_range}},
\item
\tcode{is_same_v<ranges::range_value_t<R>, charT>} is \tcode{true},
\item
\tcode{is_convertible_v<R, const charT*>} is \tcode{false}, and
\item
\tcode{d.operator ::std::basic_string_view<charT, traits>()}
is not a valid expression.
\end{itemize}
\pnum
\effects
Initializes \tcode{data_} with \tcode{ranges::data(r)} and
\tcode{size_} with \tcode{ranges::size(r)}.
\pnum
\throws
Any exception thrown by \tcode{ranges::data(r)} and \tcode{ranges::size(r)}.
\end{itemdescr}
\rSec3[string.view.deduct]{Deduction guides}
\begin{itemdecl}
template<class It, class End>
basic_string_view(It, End) -> basic_string_view<iter_value_t<It>>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\constraints
\begin{itemize}
\item \tcode{It} satisfies \libconcept{contiguous_iterator}.
\item \tcode{End} satisfies \tcode{\libconcept{sized_sentinel_for}<It>}.
\end{itemize}
\end{itemdescr}
\begin{itemdecl}
template<class R>
basic_string_view(R&&) -> basic_string_view<ranges::range_value_t<R>>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\constraints
\tcode{R} satisfies \tcode{ranges::\libconcept{contiguous_range}}.
\end{itemdescr}
\rSec3[string.view.iterators]{Iterator support}
\indexlibrarymember{const_iterator}{basic_string_view}%
\begin{itemdecl}
using const_iterator = @\impdefx{type of \tcode{basic_string_view::const_iterator}}@;
\end{itemdecl}
\begin{itemdescr}
\pnum
A type that meets the requirements
of a constant
\oldconceptref{RandomAccessIterator}\iref{random.access.iterators},
models \libconcept{contiguous_iterator}\iref{iterator.concept.contiguous}, and
meets the constexpr iterator requirements\iref{iterator.requirements.general},
whose \tcode{value_type} is the template parameter \tcode{charT}.
\pnum
All requirements on container iterators\iref{container.requirements} apply to \tcode{basic_string_view::const_iterator} as well.
\end{itemdescr}
\indexlibrarymember{begin}{basic_string_view}%
\indexlibrarymember{cbegin}{basic_string_view}%
\begin{itemdecl}
constexpr const_iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
An iterator such that
\begin{itemize}
\item if \tcode{!empty()}, \tcode{addressof(*begin()) == data_},
\item otherwise, an unspecified value such that \range{begin()}{end()} is a valid range.
\end{itemize}
\end{itemdescr}
\indexlibrarymember{end}{basic_string_view}%
\indexlibrarymember{cend}{basic_string_view}%
\begin{itemdecl}
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{begin() + size()}.
\end{itemdescr}
\indexlibrarymember{rbegin}{basic_string_view}%
\indexlibrarymember{crbegin}{basic_string_view}%
\begin{itemdecl}
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{const_reverse_iterator(end())}.
\end{itemdescr}
\indexlibrarymember{rend}{basic_string_view}%
\indexlibrarymember{crend}{basic_string_view}%
\begin{itemdecl}
constexpr const_reverse_iterator rend() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{const_reverse_iterator(begin())}.
\end{itemdescr}
\rSec3[string.view.capacity]{Capacity}
\indexlibrarymember{size}{basic_string_view}%
\indexlibrarymember{length}{basic_string_view}%
\begin{itemdecl}
constexpr size_type size() const noexcept;
constexpr size_type length() const noexcept;