-
Notifications
You must be signed in to change notification settings - Fork 8
/
concepts.tex
1175 lines (973 loc) · 38.5 KB
/
concepts.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[concepts.lib]{Concepts library}
\rSec1[concepts.lib.general]{General}
\pnum
This Clause describes library components that \Cpp programs may use to perform
compile-time validation of template parameters and perform function dispatch
based on properties of types. The purpose of these concepts is to establish
a foundation for equational reasoning in programs.
\pnum
The following subclauses describe core language concepts,
comparison concepts, object concepts, and function concepts
as summarized in Table~\ref{tab:concepts.lib.summary}.
\begin{libsumtab}{Fundamental concepts library summary}{tab:concepts.lib.summary}
\ref{concepts.lib.corelang} & Core language concepts & \tcode{<experimental/ranges/concepts>} \\
\ref{concepts.lib.compare} & Comparison concepts & \\
\ref{concepts.lib.object} & Object concepts & \\
\ref{concepts.lib.callable} & Callable concepts & \\
\end{libsumtab}
\rSec2[concepts.lib.general.equality]{Equality Preservation}
\pnum
An expression is \techterm{equality preserving} if, given equal inputs, the expression results in
equal outputs. The inputs to an expression are the set of the expression's operands. The
output of an expression is the expression's result and all operands modified by the expression.
\pnum
Not all input values must be valid for a given expression; e.g., for integers \tcode{a}
and \tcode{b}, the expression \tcode{a / b} is not well-defined when \tcode{b} is \tcode{0}. This
does not preclude the expression \tcode{a / b} being equality preserving. The
\techterm{domain} of an expression is the set of input values for which the
expression is required to be well-defined.
\pnum
Expressions required by this specification to be equality preserving are
further required to be stable: two evaluations of such an expression with the same
input objects must have equal outputs absent any
explicit intervening modification of those input objects.
\enternote This requirement allows generic code to reason
about the current values of objects based on knowledge of the prior values as
observed via equality preserving expressions. It effectively forbids spontaneous
changes to an object, changes to an object from another thread of execution, changes
to an object as side effects of non-modifying expressions, and changes to an object as
side effects of modifying a distinct object if those changes could be observable
to a library function via an equality preserving expression that is required to be
valid for that object. \exitnote
\pnum
Expressions declared in a \grammarterm{requires-expression} in this document are
required to be equality preserving, except for those annotated with the comment
``not required to be equality preserving.'' An expression so annotated
may be equality preserving, but is not required to be so.
\pnum
An expression that may alter the value of one or more of its inputs in a manner
observable to equality preserving expressions is said to modify those inputs.
This document uses a notational convention to specify which expressions declared
in a \grammarterm{requires-expression} modify which inputs: except where otherwise
specified, an expression operand that is a non-constant lvalue or rvalue may be
modified. Operands that are constant lvalues or rvalues must not be modified.
\pnum
Where a \grammarterm{requires-expression} declares an expression that is non-modifying
for some constant lvalue operand, additional variations of that expression that accept
a non-constant lvalue or (possibly constant) rvalue for the given operand are also
required except where such an expression variation is explicitly required with
differing semantics. These \techterm{implicit expression variations} must meet the semantic
requirements of the declared expression. The extent to which an implementation
validates the syntax of the variations is unspecified.
\enterexample
\begin{codeblock}
template <class T>
concept bool C =
requires(T a, T b, const T c, const T d) {
c == d; // \#1
a = std::move(b); // \#2
a = c; // \#3
};
\end{codeblock}
Expression \#1 does not modify either of its operands, \#2 modifies both of its
operands, and \#3 modifies only its first operand \tcode{a}.
Expression \#1 implicitly requires additional expression variations that meet the
requirements for \tcode{c == d} (including non-modification), as if the expressions
\begin{codeblock}
a == d; a == b; a == move(b); a == d;
c == a; c == move(a); c == move(d);
move(a) == d; move(a) == b; move(a) == move(b); move(a) == move(d);
move(c) == b; move(c) == move(b); move(c) == d; move(c) == move(d);
\end{codeblock}
had been declared as well.
Expression \#3 implicitly requires additional expression variations that meet the
requirements for \tcode{a = c} (including non-modification of the second operand),
as if the expressions \tcode{a = b} and \tcode{a = move(c)} had been declared.
Expression \#3 does not implicitly require an expression variation with a
non-constant rvalue second operand, since expression \#2 already specifies exactly
such an expression explicitly.
\exitexample
\enterexample
The following type \tcode{T} meets the explicitly stated syntactic requirements
of concept \tcode{C} above but does not meet the additional implicit requirements:
\begin{codeblock}
struct T {
bool operator==(const T&) const { return true; }
bool operator==(T&) = delete;
};
\end{codeblock}
\tcode{T} fails to meet the implicit
requirements of \tcode{C}, so \tcode{C<T>} is not satisfied. Since
implementations are not required to validate the syntax of implicit requirements, it
is unspecified whether or not an implementation diagnoses as ill-formed a program
which requires \tcode{C<T>}.
\exitexample
\rSec1[concepts.lib.synopsis]{Header \tcode{<experimental/ranges/concepts>} synopsis}
\indexlibrary{\idxhdr{experimental/ranges/concepts}}%
\begin{codeblock}
namespace std { namespace experimental { namespace ranges { inline namespace v1 {
// \ref{concepts.lib.corelang}, core language concepts:
// \ref{concepts.lib.corelang.same}, Same:
template <class T, class U>
concept bool Same = @\seebelow@;
// \ref{concepts.lib.corelang.derived}, DerivedFrom:
template <class T, class U>
concept bool DerivedFrom = @\seebelow@;
// \ref{concepts.lib.corelang.convertibleto}, ConvertibleTo:
template <class T, class U>
concept bool ConvertibleTo = @\seebelow@;
// \ref{concepts.lib.corelang.commonref}, CommonReference:
template <class T, class U>
concept bool CommonReference = @\seebelow@;
// \ref{concepts.lib.corelang.common}, Common:
template <class T, class U>
concept bool Common = @\seebelow@;
// \ref{concepts.lib.corelang.integral}, Integral:
template <class T>
concept bool Integral = @\seebelow@;
// \ref{concepts.lib.corelang.signedintegral}, SignedIntegral:
template <class T>
concept bool SignedIntegral = @\seebelow@;
// \ref{concepts.lib.corelang.unsignedintegral}, UnsignedIntegral:
template <class T>
concept bool UnsignedIntegral = @\seebelow@;
// \ref{concepts.lib.corelang.assignable}, Assignable:
template <class T, class U>
concept bool Assignable = @\seebelow@;
// \ref{concepts.lib.corelang.swappable}, Swappable:
template <class T>
concept bool Swappable = @\seebelow@;
template <class T, class U>
concept bool SwappableWith = @\seebelow@;
// \ref{concepts.lib.corelang.destructible}, Destructible:
template <class T>
concept bool Destructible = @\seebelow@;
// \ref{concepts.lib.corelang.constructible}, Constructible:
template <class T, class... Args>
concept bool Constructible = @\seebelow@;
// \ref{concepts.lib.corelang.defaultconstructible}, DefaultConstructible:
template <class T>
concept bool DefaultConstructible = @\seebelow@;
// \ref{concepts.lib.corelang.moveconstructible}, MoveConstructible:
template <class T>
concept bool MoveConstructible = @\seebelow@;
// \ref{concepts.lib.corelang.copyconstructible}, CopyConstructible:
template <class T>
concept bool CopyConstructible = @\seebelow@;
// \ref{concepts.lib.compare}, comparison concepts:
// \ref{concepts.lib.compare.boolean}, Boolean:
template <class B>
concept bool Boolean = @\seebelow@;
// \ref{concepts.lib.compare.equalitycomparable}, EqualityComparable:
template <class T, class U>
concept bool WeaklyEqualityComparableWith = @\seebelow@;
template <class T>
concept bool EqualityComparable = @\seebelow@;
template <class T, class U>
concept bool EqualityComparableWith = @\seebelow@;
// \ref{concepts.lib.compare.stricttotallyordered}, StrictTotallyOrdered:
template <class T>
concept bool StrictTotallyOrdered = @\seebelow@;
template <class T, class U>
concept bool StrictTotallyOrderedWith = @\seebelow@;
// \ref{concepts.lib.object}, object concepts:
// \ref{concepts.lib.object.movable}, Movable:
template <class T>
concept bool Movable = @\seebelow@;
// \ref{concepts.lib.object.copyable}, Copyable:
template <class T>
concept bool Copyable = @\seebelow@;
// \ref{concepts.lib.object.semiregular}, Semiregular:
template <class T>
concept bool Semiregular = @\seebelow@;
// \ref{concepts.lib.object.regular}, Regular:
template <class T>
concept bool Regular = @\seebelow@;
// \ref{concepts.lib.callable}, callable concepts:
// \ref{concepts.lib.callable.invocable}, Invocable:
template <class F, class... Args>
concept bool Invocable = @\seebelow@;
// \ref{concepts.lib.callable.regularinvocable}, RegularInvocable:
template <class F, class... Args>
concept bool RegularInvocable = @\seebelow@;
// \ref{concepts.lib.callable.predicate}, Predicate:
template <class F, class... Args>
concept bool Predicate = @\seebelow@;
// \ref{concepts.lib.callable.relation}, Relation:
template <class R, class T, class U>
concept bool Relation = @\seebelow@;
// \ref{concepts.lib.callable.strictweakorder}, StrictWeakOrder:
template <class R, class T, class U>
concept bool StrictWeakOrder = @\seebelow@;
}}}}
\end{codeblock}
\rSec1[concepts.lib.corelang]{Core language concepts}
\rSec2[concepts.lib.corelang.general]{General}
\pnum
This section contains the definition of concepts corresponding to language features.
These concepts express relationships between types, type classifications, and
fundamental type properties.
\rSec2[concepts.lib.corelang.same]{Concept \tcode{Same}}
\indexlibrary{\idxcode{Same}}%
\begin{itemdecl}
template <class T, class U>
concept bool Same = is_same<T, U>::value; // \seebelow
\end{itemdecl}
\begin{itemdescr}
\pnum
There need not be any subsumption relationship between \tcode{Same<T, U>} and
\tcode{is_same<T, U>::value}.
\pnum
\remarks For the purposes of constraint checking, \tcode{Same<T, U>} implies
\tcode{Same<U, T>}.
\end{itemdescr}
\rSec2[concepts.lib.corelang.derived]{Concept \tcode{DerivedFrom}}
\indexlibrary{\idxcode{DerivedFrom}}%
\begin{itemdecl}
template <class T, class U>
concept bool DerivedFrom =
is_base_of<U, T>::value &&
is_convertible<remove_cv_t<T>*, remove_cv_t<U>*>::value; // \seebelow
\end{itemdecl}
\begin{itemdescr}
\pnum
There need not be any subsumption relationship between \tcode{DerivedFrom<T, U>}
and either \tcode{is_base_of<U, T>::value} or
\tcode{is_convertible<remove_cv_t<T>*, remove_cv_t<U>*>::value}.
\pnum
\enternote \tcode{DerivedFrom<T, U>} is satisfied if and only if \tcode{T} is
publicly and unambiguously derived from \tcode{U}, or \tcode{T} and \tcode{U}
are the same class type ignoring \cv-qualifiers. \exitnote
\end{itemdescr}
\rSec2[concepts.lib.corelang.convertibleto]{Concept \tcode{ConvertibleTo}}
\indexlibrary{\idxcode{ConvertibleTo}}%
\begin{itemdecl}
template <class T, class U>
concept bool ConvertibleTo =
is_convertible<From, To>::value && // \seebelow
requires(From (&f)()) {
static_cast<To>(f());
};
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{test} be the invented function:
\begin{codeblock}
To test(From (&f)()) {
return f();
}
\end{codeblock}
and let \tcode{f} be a function with no arguments and return type \tcode{From}
such that \tcode{f()} is equality preserving.
\tcode{ConvertibleTo<From, To>} is satisfied only if:
\begin{itemize}
\item
\tcode{To} is not an object or reference-to-object type, or
\tcode{static_cast<To>(f())} is equal to \tcode{test(f)}.
\item
\tcode{From} is not a reference-to-object type, or
\begin{itemize}
\item
If \tcode{From} is an rvalue reference to a non const-qualified type, the resulting state of the object referenced by \tcode{f()} after either above expression is valid but unspecified~(\cxxref{lib.types.movedfrom}).
\item
Otherwise, the object referred to by \tcode{f()} is not modified by either above expression.
\end{itemize}
\end{itemize}
\pnum
There need not be any subsumption relationship between \tcode{ConvertibleTo<From, To>}
and \tcode{is_\-convert\-ible\brk{}<From,\brk{} To>::value}.
\end{itemdescr}
\rSec2[concepts.lib.corelang.commonref]{Concept \tcode{CommonReference}}
\pnum
For two types \tcode{T} and \tcode{U}, if \tcode{common_reference_t<T, U>}
is well-formed and denotes a type \tcode{C} such that both
\tcode{ConvertibleTo<T, C>} and \tcode{ConvertibleTo<U, C>} are
satisfied, then \tcode{T} and \tcode{U} share a \techterm{common reference
type}, \tcode{C}. \enternote \tcode{C} could be the same as \tcode{T}, or
\tcode{U}, or it could be a different type. \tcode{C} may be a reference
type. \tcode{C} need not be unique.\exitnote
\indexlibrary{\idxcode{CommonReference}}%
\begin{itemdecl}
template <class T, class U>
concept bool CommonReference =
Same<common_reference_t<T, U>, common_reference_t<U, T>> &&
ConvertibleTo<T, common_reference_t<T, U>> &&
ConvertibleTo<U, common_reference_t<T, U>>;
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{C} be \tcode{common_reference_t<T, U>}. Let \tcode{t} be a
function whose return type is \tcode{T}, and let \tcode{u} be a function
whose return type is \tcode{U}. \tcode{CommonReference<T, U>} is satisfied
only if:
\begin{itemize}
\item \tcode{C(t())} equals \tcode{C(t())} if and only if \tcode{t()} is an
equality preserving expression~(\ref{concepts.lib.general.equality}).
\item \tcode{C(u())} equals \tcode{C(u())} if and only if \tcode{u()} is an
equality preserving expression.
\end{itemize}
\pnum
\enternote Users can customize the behavior of \tcode{CommonReference} by specializing the
\tcode{basic_common_reference} class template~(\ref{meta.trans.other}).\exitnote
\end{itemdescr}
\rSec2[concepts.lib.corelang.common]{Concept \tcode{Common}}
\pnum
If \tcode{T} and \tcode{U} can both be explicitly converted to some third type,
\tcode{C}, then \tcode{T} and \tcode{U} share a \techterm{common type},
\tcode{C}. \enternote \tcode{C} could be the same as \tcode{T}, or \tcode{U}, or
it could be a different type. \tcode{C} may not be unique.\exitnote
\indexlibrary{\idxcode{Common}}%
\begin{itemdecl}
template <class T, class U>
concept bool Common =
Same<common_type_t<T, U>, common_type_t<U, T>> &&
ConvertibleTo<T, common_type_t<T, U>> &&
ConvertibleTo<U, common_type_t<T, U>> &&
CommonReference<
add_lvalue_reference_t<const T>,
add_lvalue_reference_t<const U>> &&
CommonReference<
add_lvalue_reference_t<common_type_t<T, U>>,
common_reference_t<
add_lvalue_reference_t<const T>,
add_lvalue_reference_t<const U>>>;
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{C} be \tcode{common_type_t<T, U>}. Let
\tcode{t} be a function whose return type is \tcode{T}, and let \tcode{u} be a function
whose return type is \tcode{U}. \tcode{Common<T, U>} is satisfied only if:
\begin{itemize}
\item \tcode{C(t())} equals \tcode{C(t())} if and only if
\tcode{t()} is an equality preserving
expression~(\ref{concepts.lib.general.equality}).
\item \tcode{C(u())} equals \tcode{C(u())} if and only if
\tcode{u()} is an equality preserving
expression~(\ref{concepts.lib.general.equality}).
\end{itemize}
\pnum
\enternote Users can customize the behavior of \tcode{Common} by specializing the \tcode{common_type}
class template~(\ref{meta.unary.prop}).\exitnote
\end{itemdescr}
\rSec2[concepts.lib.corelang.integral]{Concept \tcode{Integral}}
\indexlibrary{\idxcode{Integral}}%
\begin{itemdecl}
template <class T>
concept bool Integral = is_integral<T>::value; // \seebelow
\end{itemdecl}
\begin{itemdescr}
\pnum
There need not be any subsumption relationship between \tcode{Integral<T>} and
\tcode{is_integral<T>\colcol{}value}.
\end{itemdescr}
\rSec2[concepts.lib.corelang.signedintegral]{Concept \tcode{SignedIntegral}}
\indexlibrary{\idxcode{SignedIntegral}}%
\begin{itemdecl}
template <class T>
concept bool SignedIntegral = Integral<T> && is_signed<T>::value; // \seebelow
\end{itemdecl}
\begin{itemdescr}
\pnum
There need not be any subsumption relationship between \tcode{SignedIntegral<T>} and
\tcode{is_signed<T>::\brk{}value}.
\pnum
\enternote \tcode{SignedIntegral<T>} may be satisfied even for
types that are not signed integral types~(\cxxref{basic.fundamental});
for example, \tcode{char}.
\exitnote
\end{itemdescr}
\rSec2[concepts.lib.corelang.unsignedintegral]{Concept \tcode{UnsignedIntegral}}
\indexlibrary{\idxcode{UnsignedIntegral}}%
\begin{itemdecl}
template <class T>
concept bool UnsignedIntegral = Integral<T> && !SignedIntegral<T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\enternote \tcode{UnsignedIntegral<T>} may be satisfied even for
types that are not unsigned integral types~(\cxxref{basic.fundamental});
for example, \tcode{char}.
\exitnote
\end{itemdescr}
\rSec2[concepts.lib.corelang.assignable]{Concept \tcode{Assignable}}
\indexlibrary{\idxcode{Assignable}}%
\begin{itemdecl}
template <class T, class U>
concept bool Assignable =
is_lvalue_reference<T>::value && // \seebelow
CommonReference<
const remove_reference_t<T>&,
const remove_reference_t<U>&> &&
requires(T t, U&& u) {
{ t = std::forward<U>(u) } -> Same<T>&&;
};
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{t} be an lvalue that refers to an object \tcode{o} such that
\tcode{decltype((t))} is \tcode{T}, and \tcode{u} an expression such that
\tcode{decltype((u))} is \tcode{U}. Let \tcode{u2} be a distinct object that is
equal to \tcode{u}. \tcode{Assignable<T, U>} is satisfied only if
\begin{itemize}
\item \tcode{addressof(t = u) == addressof(o)}.
\item After evaluating \tcode{t = u}:
\begin{itemize}
\item \tcode{t} is equal to \tcode{u2}, unless \tcode{u} is a non-const xvalue that refers to \tcode{o}.
\item If \tcode{u} is a non-\tcode{const} xvalue, the resulting state of the
object to which it refers is valid but unspecified~(\cxxref{lib.types.movedfrom}).
\item Otherwise, if \tcode{u} is a glvalue, the object to which it refers is not
modified.
\end{itemize}
\end{itemize}
\pnum
There need not be any subsumption relationship between \tcode{Assignable<T, U>}
and \tcode{is_\-lval\-ue_\-ref\-er\-ence<T>::\brk{}value}.
\pnum
\enternote Assignment need not be a total function~(\ref{structure.requirements});
in particular, if assignment to an object \tcode{x} can result in a modification
of some other object \tcode{y}, then \tcode{x = y} is likely not in the domain
of \tcode{=}. \exitnote
\end{itemdescr}
\rSec2[concepts.lib.corelang.swappable]{Concept \tcode{Swappable}}
\indexlibrary{\idxcode{Swappable}}%
\begin{itemdecl}
template <class T>
concept bool Swappable =
requires(T& a, T& b) {
ranges::swap(a, b);
};
template <class T, class U>
concept bool SwappableWith =
CommonReference<
const remove_reference_t<T>&,
const remove_reference_t<U>&> &&
requires(T&& t, U&& u) {
ranges::swap(std::forward<T>(t), std::forward<T>(t));
ranges::swap(std::forward<U>(u), std::forward<U>(u));
ranges::swap(std::forward<T>(t), std::forward<U>(u));
ranges::swap(std::forward<U>(u), std::forward<T>(t));
};
\end{itemdecl}
\begin{itemdescr}
\pnum
This subclause provides definitions for swappable types and expressions. In these
definitions, let \tcode{t} denote an expression of type \tcode{T}, and let \tcode{u}
denote an expression of type \tcode{U}.
\pnum
An object \tcode{t} is \defn{swappable with} an object \tcode{u} if and only if
\tcode{SwappableWith<T, U>} is satisfied. \tcode{Swappable\-With<T, U>} is satisfied
only if given distinct objects \tcode{t2} equal to \tcode{t}
and \tcode{u2} equal to \tcode{u}, after evaluating either
\tcode{ranges::swap(t, u)} or \tcode{ranges::swap(u, t)}, \tcode{t2} is equal to
\tcode{u} and \tcode{u2} is equal to \tcode{t}.
\pnum
An rvalue or lvalue \tcode{t} is \defn{swappable} if and only if \tcode{t} is
swappable with any rvalue or lvalue, respectively, of type \tcode{T}.
\enterexample User code can ensure that the evaluation of \tcode{swap} calls
is performed in an appropriate context under the various conditions as follows:
\begin{codeblock}
#include <utility>
// Requires: \tcode{std::forward<T>(t)} shall be swappable with \tcode{std::forward<U>(u)}.
template <class T, class U>
void value_swap(T&& t, U&& u) {
using std::experimental::ranges::swap;
swap(std::forward<T>(t), std::forward<U>(u)); // OK: uses ``swappable with'' conditions
// for rvalues and lvalues
}
// Requires: lvalues of \tcode{T} shall be swappable.
template <class T>
void lv_swap(T& t1, T& t2) {
using std::experimental::ranges::swap;
swap(t1, t2); // OK: uses swappable conditions for
} // lvalues of type \tcode{T}
namespace N {
struct A { int m; };
struct Proxy { A* a; };
Proxy proxy(A& a) { return Proxy{ &a }; }
void swap(A& x, Proxy p) {
std::experimental::ranges::swap(x.m, p.a->m); // OK: uses context equivalent to swappable
// conditions for fundamental types
}
void swap(Proxy p, A& x) { swap(x, p); } // satisfy symmetry constraint
}
int main() {
int i = 1, j = 2;
lv_swap(i, j);
assert(i == 2 && j == 1);
N::A a1 = { 5 }, a2 = { -5 };
value_swap(a1, proxy(a2));
assert(a1.m == -5 && a2.m == 5);
}
\end{codeblock}
\exitexample
\end{itemdescr}
\rSec2[concepts.lib.corelang.destructible]{Concept \tcode{Destructible}}
\pnum
The \tcode{Destructible} concept specifies properties of all types, instances of
which can be destroyed at the end of their lifetime, or reference types.
\indexlibrary{\idxcode{Destructible}}%
\begin{itemdecl}
template <class T>
concept bool Destructible = is_nothrow_destructible<T>::value; // \seebelow
\end{itemdecl}
\begin{itemdescr}
\pnum
There need not be any subsumption relationship between \tcode{Destructible<T>}
and \tcode{is_\-no\-throw_\-destruct\-ible<T>::value}.
\pnum
\enternote Unlike the \tcode{Destructible} library concept in the \Cpp
Standard~(\cxxref{utility.arg.requirements}), this concept forbids destructors
that are \tcode{noexcept(false)}, even if non-throwing. \exitnote
\end{itemdescr}
\rSec2[concepts.lib.corelang.constructible]{Concept \tcode{Constructible}}
\pnum
The \tcode{Constructible} concept constrains the initialization of a variable of
a type with a given set of argument types.
\indexlibrary{\idxcode{Constructible}}%
\begin{itemdecl}
template <class T, class... Args>
concept bool Constructible =
Destructible<T> && is_constructible<T, Args...>::value; // \seebelow
\end{itemdecl}
\begin{itemdescr}
\pnum
There need not be any subsumption relationship between \tcode{Constructible<T, Args...>}
and \tcode{is_constructible<T, Args...>::value}.
\end{itemdescr}
\rSec2[concepts.lib.corelang.defaultconstructible]{Concept \tcode{DefaultConstructible}}
\indexlibrary{\idxcode{DefaultConstructible}}%
\begin{itemdecl}
template <class T>
concept bool DefaultConstructible = Constructible<T>;
\end{itemdecl}
\rSec2[concepts.lib.corelang.moveconstructible]{Concept \tcode{MoveConstructible}}
\indexlibrary{\idxcode{MoveConstructible}}%
\begin{itemdecl}
template <class T>
concept bool MoveConstructible =
Constructible<T, T> && ConvertibleTo<T, T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
If \tcode{T} is an object type, then let \tcode{rv} be an rvalue of type \tcode{T}
and \tcode{u2} a distinct object of type \tcode{T} equal to \tcode{rv}.
\tcode{MoveConstructible<T>} is satisfied only if
\begin{itemize}
\item After the definition \tcode{T u = rv;}, \tcode{u} is equal to \tcode{u2}.
\item \tcode{T\{rv\}} is equal to \tcode{u2}.
\item If \tcode{T} is not \tcode{const}, \tcode{rv}'s resulting state is valid
but unspecified~(\cxxref{lib.types.movedfrom}); otherwise, it is unchanged.
\end{itemize}
\end{itemdescr}
\rSec2[concepts.lib.corelang.copyconstructible]{Concept \tcode{CopyConstructible}}
\indexlibrary{\idxcode{CopyConstructible}}%
\begin{itemdecl}
template <class T>
concept bool CopyConstructible =
MoveConstructible<T> &&
Constructible<T, T&> && ConvertibleTo<T&, T> &&
Constructible<T, const T&> && ConvertibleTo<const T&, T> &&
Constructible<T, const T> && ConvertibleTo<const T, T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
If \tcode{T} is an object type, then let \tcode{v} be an lvalue of type (possibly
\tcode{const}) \tcode{T} or an rvalue of type \tcode{const T}.
\tcode{CopyConstructible<T>} is satisfied only if
\begin{itemize}
\item After the definition \tcode{T u = v;}, \tcode{u} is equal to \tcode{v}.
\item \tcode{T\{v\}} is equal to \tcode{v}.
\end{itemize}
\end{itemdescr}
\rSec1[concepts.lib.compare]{Comparison concepts}
\rSec2[concepts.lib.compare.general]{General}
\pnum
This section describes concepts that establish relationships and orderings
on values of possibly differing object types.
\rSec2[concepts.lib.compare.boolean]{Concept \tcode{Boolean}}
\pnum
The \tcode{Boolean} concept specifies the requirements on a type that is usable in Boolean contexts.
\indexlibrary{\idxcode{Boolean}}%
\begin{itemdecl}
template <class B>
concept bool Boolean =
Movable<decay_t<B>> && // (see \ref{concepts.lib.object.movable})
requires(const remove_reference_t<B>& b1,
const remove_reference_t<B>& b2, const bool a) {
{ b1 } -> ConvertibleTo<bool>&&;
{ !b1 } -> ConvertibleTo<bool>&&;
{ b1 && a } -> Same<bool>&&;
{ b1 || a } -> Same<bool>&&;
{ b1 && b2 } -> Same<bool>&&;
{ a && b2 } -> Same<bool>&&;
{ b1 || b2 } -> Same<bool>&&;
{ a || b2 } -> Same<bool>&&;
{ b1 == b2 } -> ConvertibleTo<bool>&&;
{ b1 == a } -> ConvertibleTo<bool>&&;
{ a == b2 } -> ConvertibleTo<bool>&&;
{ b1 != b2 } -> ConvertibleTo<bool>&&;
{ b1 != a } -> ConvertibleTo<bool>&&;
{ a != b2 } -> ConvertibleTo<bool>&&;
};
\end{itemdecl}
\pnum
Given \tcode{const} lvalues \tcode{b1} and \tcode{b2} of type
\tcode{remove_reference_t<B>}, then \tcode{Boolean<B>} is satisfied only if
\begin{itemize}
\item \tcode{bool(b1) == !bool(!b1)}.
\item \tcode{(b1 \&\& b2)}, \tcode{(b1 \&\& bool(b2))}, and
\tcode{(bool(b1) \&\& b2)} are all equal to
\tcode{(bool(b1) \&\& bool(b2))}, and have the same short-circuit evaluation.
\item \tcode{(b1 || b2)}, \tcode{(b1 || bool(b2))}, and
\tcode{(bool(b1) || b2)} are all equal to
\tcode{(bool(b1) || bool(b2))}, and have the same short-circuit evaluation.
\item \tcode{bool(b1 == b2)}, \tcode{bool(b1 == bool(b2))}, and
\tcode{bool(bool(b1) == b2)} are all equal to \tcode{(bool(b1) == bool(b2))}.
\item \tcode{bool(b1 != b2)}, \tcode{bool(b1 != bool(b2))}, and
\tcode{bool(bool(b1) != b2)} are all equal to \tcode{(bool(b1) != bool(b2))}.
\end{itemize}
\pnum \enterexample The types \tcode{bool}, \tcode{std::true_type}, and
\tcode{std::bitset<$N$>::reference} are \tcode{Boolean} types.
Pointers, smart pointers, and types with explicit conversions to \tcode{bool} are
not \tcode{Boolean} types.\exitexample
\rSec2[concepts.lib.compare.equalitycomparable]{Concept \tcode{EqualityComparable}}
\indexlibrary{\idxcode{WeaklyEqualityComparableWith}}%
\begin{itemdecl}
template <class T, class U>
concept bool WeaklyEqualityComparableWith =
requires(const remove_reference_t<T>& t,
const remove_reference_t<U>& u) {
{ t == u } -> Boolean&&;
{ t != u } -> Boolean&&;
{ u == t } -> Boolean&&;
{ u != t } -> Boolean&&;
};
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{t} and \tcode{u} be \tcode{const} lvalues of types
\tcode{remove_reference_t<T>} and \tcode{remove_reference_t<U>} respectively.
\tcode{Weakly\-Equality\-Comparable\-With<T, U>} is satisfied only if:
\begin{itemize}
\item \tcode{t == u}, \tcode{u == t}, \tcode{t != u}, and \tcode{u != t}
have the same domain.
\item \tcode{bool(u == t) == bool(t == u)}.
\item \tcode{bool(t != u) == !bool(t == u)}.
\item \tcode{bool(u != t) == bool(t != u)}.
\end{itemize}
\end{itemdescr}
\indexlibrary{\idxcode{EqualityComparable}}%
\begin{itemdecl}
template <class T>
concept bool EqualityComparable = WeaklyEqualityComparableWith<T, T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{a} and \tcode{b} be objects of type \tcode{T}.
\tcode{EqualityComparable<T>} is satisfied only if:
\begin{itemize}
\item \tcode{bool(a == b)} if and only if \tcode{a} is equal to \tcode{b}.
\end{itemize}
\pnum
\enternote The requirement that the expression \tcode{a == b} is equality preserving
implies that \tcode{==} is reflexive, transitive, and symmetric.\exitnote
\end{itemdescr}
\indexlibrary{\idxcode{EqualityComparableWith}}%
\begin{itemdecl}
template <class T, class U>
concept bool EqualityComparableWith =
EqualityComparable<T> &&
EqualityComparable<U> &&
CommonReference<
const remove_reference_t<T>&,
const remove_reference_t<U>&> &&
EqualityComparable<
common_reference_t<
const remove_reference_t<T>&,
const remove_reference_t<U>&>> &&
WeaklyEqualityComparableWith<T, U>;
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{t} be a \tcode{const} lvalue of type \tcode{remove_reference_t<T>}, \tcode{u} be a
\tcode{const} lvalue of type \tcode{remove_reference_t<U>}, and \tcode{C} be:
\begin{codeblock}
common_reference_t<const remove_reference_t<T>&, const remove_reference_t<U>&>
\end{codeblock}
\tcode{EqualityComparableWith<T, U>} is satisfied only if:
\begin{itemize}
\item \tcode{bool(t == u) == bool(C(t) == C(u))}.
\end{itemize}
\end{itemdescr}
\rSec2[concepts.lib.compare.stricttotallyordered]{Concept \tcode{StrictTotallyOrdered}}
\indexlibrary{\idxcode{StrictTotallyOrdered}}%
\begin{itemdecl}
template <class T>
concept bool StrictTotallyOrdered =
EqualityComparable<T> &&
requires(const remove_reference_t<T>& a,
const remove_reference_t<T>& b) {
{ a < b } -> Boolean&&;
{ a > b } -> Boolean&&;
{ a <= b } -> Boolean&&;
{ a >= b } -> Boolean&&;
};
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{a}, \tcode{b}, and \tcode{c} be \tcode{const} lvalues of type \tcode{remove_reference_t<T>}.
\tcode{StrictTotallyOrdered<T>} is satisfied only if
\begin{itemize}
\item Exactly one of \tcode{bool(a < b)}, \tcode{bool(a > b)}, or
\tcode{bool(a == b)} is \tcode{true}.
\item If \tcode{bool(a < b)} and \tcode{bool(b < c)}, then
\tcode{bool(a < c)}.
\item \tcode{bool(a > b) == bool(b < a)}.
\item \tcode{bool(a <= b) == !bool(b < a)}.
\item \tcode{bool(a >= b) == !bool(a < b)}.
\end{itemize}
\end{itemdescr}
\begin{itemdecl}
template <class T, class U>
concept bool StrictTotallyOrderedWith =
StrictTotallyOrdered<T> &&
StrictTotallyOrdered<U> &&
CommonReference<
const remove_reference_t<T>&,
const remove_reference_t<U>&> &&
StrictTotallyOrdered<
common_reference_t<
const remove_reference_t<T>&,
const remove_reference_t<U>&>> &&
EqualityComparableWith<T, U> &&
requires(const remove_reference_t<T>& t,
const remove_reference_t<U>& u) {
{ t < u } -> Boolean&&;
{ t > u } -> Boolean&&;
{ t <= u } -> Boolean&&;
{ t >= u } -> Boolean&&;
{ u < t } -> Boolean&&;
{ u > t } -> Boolean&&;
{ u <= t } -> Boolean&&;
{ u >= t } -> Boolean&&;
};
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{t} be a \tcode{const} lvalue of type \tcode{remove_reference_t<T>},
\tcode{u} be a \tcode{const} lvalue of type \tcode{remove_reference_t<U>},
and \tcode{C} be:
\begin{codeblock}
common_reference_t<const remove_reference_t<T>&, const remove_reference_t<U>&>
\end{codeblock}
\tcode{StrictTotallyOrderedWith<T, U>} is satisfied only if
\begin{itemize}
\item \tcode{bool(t < u) == bool(C(t) < C(u)).}
\item \tcode{bool(t > u) == bool(C(t) > C(u)).}
\item \tcode{bool(t <= u) == bool(C(t) <= C(u)).}
\item \tcode{bool(t >= u) == bool(C(t) >= C(u)).}
\item \tcode{bool(u < t) == bool(C(u) < C(t)).}
\item \tcode{bool(u > t) == bool(C(u) > C(t)).}
\item \tcode{bool(u <= t) == bool(C(u) <= C(t)).}
\item \tcode{bool(u >= t) == bool(C(u) >= C(t)).}
\end{itemize}
\end{itemdescr}
\rSec1[concepts.lib.object]{Object concepts}
\pnum
This section describes concepts that specify the basis of the
value-oriented programming style on which the library is based.
%% object types (See the rationale in Appendix~\ref{decomposition}).}
\rSec2[concepts.lib.object.movable]{Concept \tcode{Movable}}
\indexlibrary{\idxcode{Movable}}%
\begin{itemdecl}
template <class T>
concept bool Movable =
is_object<T>::value &&
MoveConstructible<T> &&
Assignable<T&, T> &&
Swappable<T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
There need not be any subsumption relationship between \tcode{Movable<T>} and
\tcode{is_object<T>::value}.
\end{itemdescr}
\rSec2[concepts.lib.object.copyable]{Concept \tcode{Copyable}}
\indexlibrary{\idxcode{Copyable}}%
\begin{itemdecl}
template <class T>
concept bool Copyable =
CopyConstructible<T> &&
Movable<T> &&
Assignable<T&, const T&>;
\end{itemdecl}
\rSec2[concepts.lib.object.semiregular]{Concept \tcode{Semiregular}}
\indexlibrary{\idxcode{Semiregular}}%
\begin{itemdecl}
template <class T>
concept bool Semiregular =
Copyable<T> &&
DefaultConstructible<T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\enternote The \tcode{Semiregular} concept is satisfied by types that
behave similarly to built-in types like \tcode{int}, except that they may not be
comparable with \tcode{==}.\exitnote
\end{itemdescr}
\rSec2[concepts.lib.object.regular]{Concept \tcode{Regular}}
\indexlibrary{\idxcode{Regular}}%
\begin{itemdecl}
template <class T>