-
Notifications
You must be signed in to change notification settings - Fork 739
/
utilities.tex
12663 lines (10459 loc) · 407 KB
/
utilities.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
\rSec0[utilities]{General utilities library}
\rSec1[utilities.general]{General}
\pnum
This Clause describes utilities that are generally useful in \Cpp programs; some
of these utilities are used by other elements of the \Cpp standard library.
These utilities are summarized in Table~\ref{tab:util.lib.summary}.
\begin{libsumtab}{General utilities library summary}{tab:util.lib.summary}
\ref{utility} & Utility components & \tcode{<utility>} \\ \rowsep
\ref{pairs} & Pairs & \tcode{<utility>} \\ \rowsep
\ref{tuple} & Tuples & \tcode{<tuple>} \\ \rowsep
\ref{template.bitset} & Fixed-size sequences of bits & \tcode{<bitset>} \\ \rowsep
& & \tcode{<memory>} \\
\ref{memory} & Memory & \tcode{<cstdlib>} \\
& & \tcode{<cstring>} \\ \rowsep
\ref{smartptr} & Smart pointers & \tcode{<memory>} \\ \rowsep
\ref{function.objects} & Function objects & \tcode{<functional>} \\ \rowsep
\ref{meta} & Type traits & \tcode{<type_traits>} \\ \rowsep
\ref{ratio} & Compile-time rational arithmetic & \tcode{<ratio>} \\ \rowsep
\ref{time} & Time utilities & \tcode{<chrono>} \\
& & \tcode{<ctime>} \\ \rowsep
\ref{allocator.adaptor} & Scoped allocators & \tcode{<scoped_allocator>} \\ \rowsep
\ref{type.index} & Type indexes & \tcode{<typeindex>} \\
\end{libsumtab}
\rSec1[utility]{Utility components}
\pnum
This subclause contains some basic function and class templates that are used
throughout the rest of the library.
\indexlibrary{\idxhdr{utility}}%
\indexlibrary{\idxcode{rel_ops}}%
\synopsis{Header \tcode{<utility>} synopsis}
\pnum
The header \tcode{<utility>} defines several types and function templates
that are described in this Clause. It also defines the template \tcode{pair}
and various function templates that operate on \tcode{pair} objects.
\begin{codeblock}
#include <initializer_list>
namespace std {
// \ref{operators}, operators:
namespace rel_ops {
template<class T> bool operator!=(const T&, const T&);
template<class T> bool operator> (const T&, const T&);
template<class T> bool operator<=(const T&, const T&);
template<class T> bool operator>=(const T&, const T&);
}
// \ref{utility.swap}, swap:
template<class T> void swap(T& a, T& b) noexcept(@\seebelow@);
template <class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
// \ref{utility.exchange}, exchange:
template <class T, class U=T> T exchange(T& obj, U&& new_val);
// \ref{forward}, forward/move:
template <class T>
constexpr T&& forward(remove_reference_t<T>& t) noexcept;
template <class T>
constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
template <class T>
constexpr remove_reference_t<T>&& move(T&&) noexcept;
template <class T>
constexpr conditional_t<
!is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
const T&, T&&> move_if_noexcept(T& x) noexcept;
// \ref{declval}, declval:
template <class T>
add_rvalue_reference_t<T> declval() noexcept; // as unevaluated operand
// \ref{pairs}, pairs:
template <class T1, class T2> struct pair;
// \ref{pairs.spec}, pair specialized algorithms:
template <class T1, class T2>
constexpr bool operator==(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
void swap(pair<T1,T2>& x, pair<T1,T2>& y) noexcept(noexcept(x.swap(y)));
template <class T1, class T2>
constexpr @\seebelow@ make_pair(T1&&, T2&&);
// \ref{pair.astuple}, tuple-like access to pair:
template <class T> class tuple_size;
template <size_t I, class T> class tuple_element;
template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >;
template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >;
template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
template<size_t I, class T1, class T2>
constexpr typename tuple_element<I, std::pair<T1, T2> >::type&
get(std::pair<T1, T2>&) noexcept;
template<size_t I, class T1, class T2>
constexpr typename tuple_element<I, std::pair<T1, T2> >::type&&
get(std::pair<T1, T2>&&) noexcept;
template<size_t I, class T1, class T2>
constexpr const typename tuple_element<I, std::pair<T1, T2> >::type&
get(const std::pair<T1, T2>&) noexcept;
template <class T, class U>
constexpr T& get(pair<T, U>& p) noexcept;
template <class T, class U>
constexpr const T& get(const pair<T, U>& p) noexcept;
template <class T, class U>
constexpr T&& get(pair<T, U>&& p) noexcept;
template <class T, class U>
constexpr T& get(pair<U, T>& p) noexcept;
template <class T, class U>
constexpr const T& get(const pair<U, T>& p) noexcept;
template <class T, class U>
constexpr T&& get(pair<U, T>&& p) noexcept;
// \ref{pair.piecewise}, pair piecewise construction
struct piecewise_construct_t { };
constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
template <class... Types> class tuple; // defined in \tcode{<tuple>}
// \ref{intseq}, Compile-time integer sequences
template<class T, T...> struct integer_sequence;
template<size_t... I>
using index_sequence = integer_sequence<size_t, I...>;
template<class T, T N>
using make_integer_sequence = integer_sequence<T, @\seebelow{}@>;
template<size_t N>
using make_index_sequence = make_integer_sequence<size_t, N>;
template<class... T>
using index_sequence_for = make_index_sequence<sizeof...(T)>;
}
\end{codeblock}
\rSec2[operators]{Operators}
\pnum
To avoid redundant definitions of \tcode{operator!=} out of \tcode{operator==}
and operators \tcode{>}, \tcode{<=}, and \tcode{>=} out of \tcode{operator<},
the library provides the following:
\indexlibrary{\idxcode{operator"!=}}%
\begin{itemdecl}
template <class T> bool operator!=(const T& x, const T& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires
Type \tcode{T} is \tcode{EqualityComparable} (Table~\ref{equalitycomparable}).
\pnum
\returns
\tcode{!(x == y)}.
\end{itemdescr}
\indexlibrary{\idxcode{operator>}}%
\begin{itemdecl}
template <class T> bool operator>(const T& x, const T& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires
Type \tcode{T} is \tcode{LessThanComparable} (Table~\ref{lessthancomparable}).
\pnum
\returns
\tcode{y < x}.
\end{itemdescr}
\indexlibrary{\idxcode{operator<=}}%
\begin{itemdecl}
template <class T> bool operator<=(const T& x, const T& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires
Type \tcode{T} is \tcode{LessThanComparable} (Table~\ref{lessthancomparable}).
\pnum
\returns
\tcode{!(y < x)}.
\end{itemdescr}
\indexlibrary{\idxcode{operator>=}}%
\begin{itemdecl}
template <class T> bool operator>=(const T& x, const T& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires
Type \tcode{T} is \tcode{LessThanComparable} (Table~\ref{lessthancomparable}).
\pnum
\returns
\tcode{!(x < y)}.
\end{itemdescr}
\pnum
In this library, whenever a declaration is provided for an \tcode{operator!=},
\tcode{operator>}, \tcode{operator>=}, or \tcode{operator<=},
and requirements and semantics are not explicitly provided,
the requirements and semantics are as specified in this Clause.
\rSec2[utility.swap]{swap}
\indexlibrary{\idxcode{swap}}%
\begin{itemdecl}
template<class T> void swap(T& a, T& b) noexcept(@\seebelow@);
\end{itemdecl}
\begin{itemdescr}
\pnum
\remark The expression inside \tcode{noexcept} is equivalent to:
\begin{codeblock}
is_nothrow_move_constructible<T>::value &&
is_nothrow_move_assignable<T>::value
\end{codeblock}
\pnum
\requires
Type
\tcode{T}
shall be
\tcode{MoveConstructible} (Table~\ref{moveconstructible})
and
\tcode{MoveAssignable} (Table~\ref{moveassignable}).
\pnum
\effects
Exchanges values stored in two locations.
\end{itemdescr}
\indexlibrary{\idxcode{swap}}%
\begin{itemdecl}
template<class T, size_t N>
void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires
\tcode{a[i]} shall be swappable with~(\ref{swappable.requirements}) \tcode{b[i]}
for all \tcode{i} in the range \range{0}{N}.
\pnum
\effects \tcode{swap_ranges(a, a + N, b)}
\end{itemdescr}
\rSec2[utility.exchange]{exchange}
\begin{itemdecl}
template <class T, class U=T> T exchange(T& obj, U&& new_val);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Equivalent to:
\begin{codeblock}
T old_val = std::move(obj);
obj = std::forward<U>(new_val);
return old_val;
\end{codeblock}
\end{itemdescr}
\rSec2[forward]{forward/move helpers}
\pnum
The library provides templated helper functions to simplify
applying move semantics to an lvalue and to simplify the implementation
of forwarding functions.
\indexlibrary{\idxcode{forward}}%
\begin{itemdecl}
template <class T> constexpr T&& forward(remove_reference_t<T>& t) noexcept;
template <class T> constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{static_cast<T\&\&>(t)}.
\pnum
\remark If the second form is instantiated with an lvalue reference type, the program is ill-formed.
\pnum
\enterexample
\begin{codeblock}
template <class T, class A1, class A2>
shared_ptr<T> factory(A1&& a1, A2&& a2) {
return shared_ptr<T>(new T(std::forward<A1>(a1), std::forward<A2>(a2)));
}
struct A {
A(int&, const double&);
};
void g() {
shared_ptr<A> sp1 = factory<A>(2, 1.414); // error: 2 will not bind to \tcode{int\&}
int i = 2;
shared_ptr<A> sp2 = factory<A>(i, 1.414); // OK
}
\end{codeblock}
\pnum
In the first call to \tcode{factory},
\tcode{A1} is deduced as \tcode{int}, so 2 is forwarded
to \tcode{A}'s constructor as an rvalue.
In the second call to \tcode{factory},
\tcode{A1} is deduced as \tcode{int\&}, so \tcode{i} is forwarded
to \tcode{A}'s constructor as an lvalue. In
both cases, \tcode{A2} is deduced as \tcode{double}, so
1.414 is forwarded to \tcode{A}'s constructor as an rvalue.
\exitexample
\end{itemdescr}
\indexlibrary{\idxcode{move}}%
\begin{itemdecl}
template <class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{static_cast<remove_reference_t<T>\&\&>(t)}.
\pnum
\enterexample
\begin{codeblock}
template <class T, class A1>
shared_ptr<T> factory(A1&& a1) {
return shared_ptr<T>(new T(std::forward<A1>(a1)));
}
struct A {
A();
A(const A&); // copies from lvalues
A(A&&); // moves from rvalues
};
void g() {
A a;
shared_ptr<A> sp1 = factory<A>(a); // ``\tcode{a}'' binds to \tcode{A(const A\&)}
shared_ptr<A> sp1 = factory<A>(std::move(a)); // ``\tcode{a}'' binds to \tcode{A(A\&\&)}
}
\end{codeblock}
\pnum
In the first call to \tcode{factory},
\tcode{A1} is deduced as \tcode{A\&}, so \tcode{a} is forwarded
as a non-const lvalue. This binds to the constructor \tcode{A(const A\&)},
which copies the value from \tcode{a}.
In the second call to \tcode{factory}, because of the call
\tcode{std::move(a)},
\tcode{A1} is deduced as \tcode{A}, so \tcode{a} is forwarded
as an rvalue. This binds to the constructor \tcode{A(A\&\&)},
which moves the value from \tcode{a}.
\exitexample
\end{itemdescr}
\indexlibrary{\idxcode{move_if_noexcept}}%
\begin{itemdecl}
template <class T> constexpr conditional_t<
!is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
const T&, T&&> move_if_noexcept(T& x) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{std::move(x)}
\end{itemdescr}
\rSec2[declval]{Function template \tcode{declval}}
\pnum
The library provides the function template \tcode{declval} to simplify the definition of
expressions which occur as unevaluated operands (Clause~\ref{expr}).
\indexlibrary{\idxcode{declval}}%
\begin{itemdecl}
template <class T>
add_rvalue_reference_t<T> declval() noexcept; // as unevaluated operand
\end{itemdecl}
\begin{itemdescr}
\pnum
\notes If this function is odr-used~(\ref{basic.def.odr}), the program is ill-formed.
\pnum
\notes The template parameter \tcode{T} of \tcode{declval} may be an incomplete type.
\enterexample
\begin{codeblock}
template <class To, class From>
decltype(static_cast<To>(declval<From>())) convert(From&&);
\end{codeblock}
declares a function template \tcode{convert} which only participates in overloading if the
type \tcode{From} can be explicitly converted to type \tcode{To}. For another example see class
template \tcode{common_type}~(\ref{meta.trans.other}).
\exitexample
\end{itemdescr}
\rSec1[pairs]{Pairs}
\rSec2[pairs.general]{In general}
\pnum
The library provides a template for heterogeneous pairs of values.
The library also provides a matching function template to simplify
their construction and several templates that provide access to \tcode{pair}
objects as if they were \tcode{tuple} objects (see~\ref{tuple.helper}
and~\ref{tuple.elem}).%
\indexlibrary{\idxcode{pair}}%
\indextext{\idxcode{pair}!tuple interface to}%
\indextext{\idxcode{tuple}!and pair@and \tcode{pair}}%
\rSec2[pairs.pair]{Class template \tcode{pair}}
\indexlibrary{\idxcode{pair}}%
\begin{codeblock}
// defined in header \tcode{<utility>}
namespace std {
template <class T1, class T2>
struct pair {
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair(const pair&) = default;
pair(pair&&) = default;
constexpr pair();
constexpr pair(const T1& x, const T2& y);
template<class U, class V> constexpr pair(U&& x, V&& y);
template<class U, class V> constexpr pair(const pair<U, V>& p);
template<class U, class V> constexpr pair(pair<U, V>&& p);
template <class... Args1, class... Args2>
pair(piecewise_construct_t,
tuple<Args1...> first_args, tuple<Args2...> second_args);
pair& operator=(const pair& p);
template<class U, class V> pair& operator=(const pair<U, V>& p);
pair& operator=(pair&& p) noexcept(@\seebelow@);
template<class U, class V> pair& operator=(pair<U, V>&& p);
void swap(pair& p) noexcept(@\seebelow@);
};
}
\end{codeblock}
\pnum
Constructors and member functions of \tcode{pair} shall not throw exceptions unless one of
the element-wise operations specified to be called for that operation
throws an exception.
\pnum
The defaulted move and copy constructor, respectively, of pair shall
be a \tcode{constexpr} function if and only if all required element-wise
initializations for copy and move, respectively, would satisfy the
requirements for a \tcode{constexpr} function.
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\begin{itemdecl}
constexpr pair();
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_default_constructible<first_type>::value} is \tcode{true}
and \tcode{is_default_construct-}\\\tcode{ible<second_type>::value} is \tcode{true}.
\pnum
\effects
Value-initializes \tcode{first} and \tcode{second}.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\begin{itemdecl}
constexpr pair(const T1& x, const T2& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_copy_constructible<first_type>::value} is \tcode{true}
and \tcode{is_copy_constructible<sec\-ond_type>::value} is \tcode{true}.
\pnum
\effects
The constructor initializes \tcode{first} with \tcode{x} and \tcode{second}
with \tcode{y}.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\begin{itemdecl}
template<class U, class V> constexpr pair(U&& x, V&& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_constructible<first_type, U\&\&>::value} is \tcode{true}
and \tcode{is_constructible<second_type, V\&\&>::value} is \tcode{true}.
\pnum
\effects
The constructor initializes \tcode{first} with
\tcode{std::forward<U>(x)} and \tcode{second}
with \tcode{std::forward<\brk{}V>(y)}.
\pnum
\notes
If \tcode{U} is not implicitly convertible to \tcode{first_type} or \tcode{V} is not
implicitly convertible to \tcode{second_type} this constructor shall not participate in
overload resolution.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\begin{itemdecl}
template<class U, class V> constexpr pair(const pair<U, V>& p);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_constructible<first_type, const U\&>::value} is \tcode{true}
and \tcode{is_constructible<sec\-ond_type, const V\&>::value} is \tcode{true}.
\pnum
\effects
Initializes members from the corresponding members of the argument.
\pnum
\remark This constructor shall not participate in overload resolution unless
\tcode{const U\&} is implicitly convertible to \tcode{first_type} and
\tcode{const V\&} is implicitly convertible to \tcode{second_type}.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\begin{itemdecl}
template<class U, class V> constexpr pair(pair<U, V>&& p);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_constructible<first_type, U\&\&>::value} is \tcode{true}
and \tcode{is_constructible<second_type, V\&\&>::value} is \tcode{true}.
\pnum
\effects
The constructor initializes \tcode{first} with
\tcode{std::forward<U>(p.first)}
and \tcode{second} with
\tcode{std::\brk{}forward<V>(p.second)}.
\pnum
\remark This constructor shall not participate in overload resolution unless
\tcode{U} is implicitly convertible to \tcode{first_type} and
\tcode{V} is implicitly convertible to \tcode{second_type}.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{pair}}
\begin{itemdecl}
template<class... Args1, class... Args2>
pair(piecewise_construct_t,
tuple<Args1...> first_args, tuple<Args2...> second_args);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_constructible<first_type, Args1\&\&...>::value} is \tcode{true}
and \tcode{is_con\-structible<second_type, Args2\&\&...>::value} is \tcode{true}.
\pnum
\effects The constructor initializes \tcode{first} with arguments of types
\tcode{Args1...} obtained by forwarding the elements of \tcode{first_args}
and initializes \tcode{second} with arguments of types \tcode{Args2...}
obtained by forwarding the elements of \tcode{second_args}. (Here, forwarding
an element \tcode{x} of type \tcode{U} within a \tcode{tuple} object means calling
\tcode{std::forward<U>(x)}.) This form of construction, whereby constructor
arguments for \tcode{first} and \tcode{second} are each provided in a separate
\tcode{tuple} object, is called \defn{piecewise construction}.
\end{itemdescr}
\indexlibrary{\idxcode{operator=}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{operator=}}
\begin{itemdecl}
pair& operator=(const pair& p);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_copy_assignable<first_type>::value} is \tcode{true}
and \tcode{is_copy_assignable<second_type>::value} is \tcode{true}.
\pnum
\effects Assigns \tcode{p.first} to \tcode{first} and \tcode{p.second} to \tcode{second}.
\pnum
\returns \tcode{*this}.
\end{itemdescr}
\indexlibrary{\idxcode{operator=}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{operator=}}
\begin{itemdecl}
template<class U, class V> pair& operator=(const pair<U, V>& p);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_assignable<first_type\&, const U\&>::value} is \tcode{true}
and \tcode{is_assignable<second_type\&, const V\&>::value} is \tcode{true}.
\pnum
\effects Assigns \tcode{p.first} to \tcode{first} and \tcode{p.second} to \tcode{second}.
\pnum
\returns \tcode{*this}.
\end{itemdescr}
\indexlibrary{\idxcode{operator=}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{operator=}}
\begin{itemdecl}
pair& operator=(pair&& p) noexcept(@\seebelow@);
\end{itemdecl}
\begin{itemdescr}
\pnum
\remarks The expression inside \tcode{noexcept} is equivalent to:
\begin{codeblock}
is_nothrow_move_assignable<T1>::value &&
is_nothrow_move_assignable<T2>::value
\end{codeblock}
\pnum
\requires \tcode{is_move_assignable<first_type>::value} is \tcode{true}
and \tcode{is_move_assignable<second_type>::value} is \tcode{true}.
\pnum
\effects
Assigns to \tcode{first} with \tcode{std::forward<first_type>(p.first)}
and to \tcode{second} with\\ \tcode{std::forward<second_type>(p.second)}.
\pnum
\returns \tcode{*this}.
\end{itemdescr}
\indexlibrary{\idxcode{operator=}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{operator=}}
\begin{itemdecl}
template<class U, class V> pair& operator=(pair<U, V>&& p);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{is_assignable<first_type\&, U\&\&>::value} is \tcode{true}
and \tcode{is_assignable<second_type\&, V\&\&>::value} is \tcode{true}.
\pnum
\effects
Assigns to \tcode{first} with \tcode{std::forward<U>(p.first)}
and to \tcode{second} with\\ \tcode{std::forward<V>(p.second)}.
\pnum
\returns \tcode{*this}.
\end{itemdescr}
\indexlibrary{\idxcode{swap}!\idxcode{pair}}
\indexlibrary{\idxcode{pair}!\idxcode{swap}}
\begin{itemdecl}
void swap(pair& p) noexcept(@\seebelow@);
\end{itemdecl}
\begin{itemdescr}
\pnum
\remarks The expression inside \tcode{noexcept} is equivalent to:
\begin{codeblock}
noexcept(swap(first, p.first)) &&
noexcept(swap(second, p.second))
\end{codeblock}
\pnum
\requires
\tcode{first} shall be swappable with~(\ref{swappable.requirements})
\tcode{p.first} and \tcode{second} shall be swappable with \tcode{p.second}.
\pnum
\effects Swaps
\tcode{first} with \tcode{p.first} and
\tcode{second} with \tcode{p.second}.
\end{itemdescr}
\rSec2[pairs.spec]{Specialized algorithms}
\indexlibrary{\idxcode{operator==}!\tcode{pair}}%
\begin{itemdecl}
template <class T1, class T2>
constexpr bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{x.first == y.first \&\& x.second == y.second}.
\end{itemdescr}
\indexlibrary{\idxcode{operator<}!\tcode{pair}}%
\begin{itemdecl}
template <class T1, class T2>
constexpr bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{x.first < y.first || (!(y.first < x.first) \&\& x.second < y.second)}.
\end{itemdescr}
\indexlibrary{\idxcode{operator"!=}!\tcode{pair}}%
\begin{itemdecl}
template <class T1, class T2>
constexpr bool operator!=(const pair<T1, T2>& x, const pair<T1, T2>& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{!(x == y)}
\end{itemdescr}
\indexlibrary{\idxcode{operator>}!\tcode{pair}}%
\begin{itemdecl}
template <class T1, class T2>
constexpr bool operator>(const pair<T1, T2>& x, const pair<T1, T2>& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{y < x}
\end{itemdescr}
\indexlibrary{\idxcode{operator>=}!\tcode{pair}}%
\begin{itemdecl}
template <class T1, class T2>
constexpr bool operator>=(const pair<T1, T2>& x, const pair<T1, T2>& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{!(x < y)}
\end{itemdescr}
\indexlibrary{\idxcode{operator<=}!\tcode{pair}}%
\begin{itemdecl}
template <class T1, class T2>
constexpr bool operator<=(const pair<T1, T2>& x, const pair<T1, T2>& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{!(y < x)}
\end{itemdescr}
\indexlibrary{\idxcode{swap}!\tcode{pair}}%
\begin{itemdecl}
template<class T1, class T2> void swap(pair<T1, T2>& x, pair<T1, T2>& y)
noexcept(noexcept(x.swap(y)));
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects \tcode{x.swap(y)}
\end{itemdescr}
\indexlibrary{\idxcode{make_pair}}%
\begin{itemdecl}
template <class T1, class T2>
constexpr pair<V1, V2> make_pair(T1&& x, T2&& y);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{pair<V1, V2>(std::forward<T1>(x), std::forward<T2>(y))};
where \tcode{V1} and \tcode{V2} are determined as follows: Let \tcode{Ui} be
\tcode{decay<Ti>::type} for each \tcode{Ti}. Then each \tcode{Vi} is \tcode{X\&}
if \tcode{Ui} equals \tcode{reference_wrapper<X>}, otherwise \tcode{Vi} is
\tcode{Ui}.
\pnum
\enterexample
In place of:
\begin{codeblock}
return pair<int, double>(5, 3.1415926); // explicit types
\end{codeblock}
a \Cpp program may contain:
\begin{codeblock}
return make_pair(5, 3.1415926); // types are deduced
\end{codeblock}
\exitexample
\end{itemdescr}
\rSec2[pair.astuple]{Tuple-like access to pair}
\indexlibrary{\idxcode{tuple_size}}%
\begin{itemdecl}
tuple_size<pair<T1, T2> >::value
\end{itemdecl}
\begin{itemdescr}
\pnum\returns Integral constant expression.
\pnum\textit{Value:} 2.
\end{itemdescr}
\indexlibrary{\idxcode{tuple_element}}%
\begin{itemdecl}
tuple_element<0, pair<T1, T2> >::type
\end{itemdecl}
\begin{itemdescr}
\pnum\textit{Value:} the type \tcode{T1}.
\end{itemdescr}
\indexlibrary{\idxcode{tuple_element}}%
\begin{itemdecl}
tuple_element<1, pair<T1, T2> >::type
\end{itemdecl}
\begin{itemdescr}
\pnum\textit{Value:} the type T2.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{get}}%
\indexlibrary{\idxcode{get}!\idxcode{pair}}%
\begin{itemdecl}
template<size_t I, class T1, class T2>
constexpr tuple_element_t<I, std::pair<T1, T2> >&
get(pair<T1, T2>&) noexcept;
template<size_t I, class T1, class T2>
constexpr const typename tuple_element<I, std::pair<T1, T2> >::type&
get(const pair<T1, T2>&) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum\returns If \tcode{I == 0} returns \tcode{p.first};
if \tcode{I == 1} returns \tcode{p.second}; otherwise the program is ill-formed.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{get}}%
\indexlibrary{\idxcode{get}!\idxcode{pair}}%
\begin{itemdecl}
template<size_t I, class T1, class T2>
constexpr typename tuple_element<I, std::pair<T1, T2> >::type&&
get(std::pair<T1, T2>&&) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns If \tcode{I == 0} returns \tcode{std::forward<T1\&\&>(p.first)};
if \tcode{I == 1} returns
\tcode{std::forward<T2\&\&>(\brk{}p.second)};
otherwise the program is ill-formed.
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{get}}%
\indexlibrary{\idxcode{get}!\idxcode{pair}}%
\begin{itemdecl}
template <class T, class U>
constexpr T& get(pair<T, U>& p) noexcept;
template <class T, class U>
constexpr const T& get(const pair<T, U>& p) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{T} and \tcode{U} are distinct types. Otherwise, the program is ill-formed.
\pnum
\returns \tcode{get<0>(p);}
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{get}}%
\indexlibrary{\idxcode{get}!\idxcode{pair}}%
\begin{itemdecl}
template <class T, class U>
constexpr T&& get(pair<T, U>&& p) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{T} and \tcode{U} are distinct types. Otherwise, the program is ill-formed.
\pnum
\returns \tcode{get<0>(std::move(p));}
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{get}}%
\indexlibrary{\idxcode{get}!\idxcode{pair}}%
\begin{itemdecl}
template <class T, class U>
constexpr T& get(pair<U, T>& p) noexcept;
template <class T, class U>
constexpr const T& get(const pair<U, T>& p) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{T} and \tcode{U} are distinct types. Otherwise, the program is ill-formed.
\pnum
\returns \tcode{get<1>(p);}
\end{itemdescr}
\indexlibrary{\idxcode{pair}!\idxcode{get}}%
\indexlibrary{\idxcode{get}!\idxcode{pair}}%
\begin{itemdecl}
template <class T, class U>
constexpr T&& get(pair<U, T>&& p) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires \tcode{T} and \tcode{U} are distinct types. Otherwise, the program is ill-formed.
\pnum
\returns \tcode{get<1>(std::move(p));}
\end{itemdescr}
\rSec2[pair.piecewise]{Piecewise construction}
\indexlibrary{\idxcode{piecewise_construct_t}}%
\indexlibrary{\idxcode{piecewise_construct}}%
\begin{itemdecl}
struct piecewise_construct_t { };
constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
\end{itemdecl}
\pnum
The \tcode{struct} \tcode{piecewise_construct_t} is an empty structure type
used as a unique type to disambiguate constructor and function overloading. Specifically,
\tcode{pair} has a constructor with \tcode{piecewise_construct_t} as the
first argument, immediately followed by two \tcode{tuple}~(\ref{tuple}) arguments used
for piecewise construction of the elements of the \tcode{pair} object.
\rSec1[tuple]{Tuples}
\rSec2[tuple.general]{In general}
\pnum
\indexlibrary{\idxcode{tuple}}%
This subclause describes the tuple library that provides a tuple type as
the class template \tcode{tuple} that can be instantiated with any number
of arguments. Each template argument specifies
the type of an element in the \tcode{tuple}. Consequently, tuples are
heterogeneous, fixed-size collections of values. An instantiation of \tcode{tuple} with
two arguments is similar to an instantiation of \tcode{pair} with the same two arguments.
See~\ref{pairs}.
\pnum
\synopsis{Header \tcode{<tuple>} synopsis}
\indexlibrary{\idxhdr{tuple}}%
\begin{codeblock}
namespace std {
// \ref{tuple.tuple}, class template \tcode{tuple}:
template <class... Types> class tuple;
// \ref{tuple.creation}, tuple creation functions:
const @\unspec@ ignore;