forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcepts.tex
1370 lines (1173 loc) · 45.7 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]{Concepts library}
\rSec1[concepts.general]{General}
\pnum
This Clause describes library components that \Cpp{} programs may use to perform
compile-time validation of template arguments 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 language-related concepts, comparison
concepts, object concepts, and callable concepts as summarized in
\tref{concepts.summary}.
\begin{libsumtab}{Fundamental concepts library summary}{concepts.summary}
\ref{concepts.equality} & Equality preservation & \\ \hline
\ref{concepts.lang} & Language-related concepts & \tcode{<concepts>} \\
\ref{concepts.compare} & Comparison concepts & \\
\ref{concepts.object} & Object concepts & \\
\ref{concepts.callable} & Callable concepts & \\
\end{libsumtab}
\rSec1[concepts.equality]{Equality preservation}
\pnum
An expression is \defnx{equality-preserving}{expression!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.
For the purposes of this subclause,
the operands of an expression are the largest subexpressions that include only:
\begin{itemize}
\item
an \grammarterm{id-expression}\iref{expr.prim.id}, and
\item
invocations of the library function templates
\tcode{std::move},
\tcode{std::forward}, and
\tcode{std::declval}\iref{forward,declval}.
\end{itemize}
\begin{example}
The operands of the expression \tcode{a = std::move(b)} are
\tcode{a} and \tcode{std::move(b)}.
\end{example}
\pnum
Not all input values need be valid for a given expression.
\begin{example}
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.
\end{example}
The \defnx{domain}{expression!domain} of an expression is the set of
input values for which the expression is required to be well-defined.
\pnum
Expressions required to be equality-preserving are further
required to be stable: two evaluations of such an expression with the same input
objects are required to have equal outputs absent any explicit intervening
modification of those input objects.
\begin{note}
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.
\end{note}
\pnum
Expressions declared in a \grammarterm{requires-expression} in the library clauses 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.
The library clauses use 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 are
required to not be modified.
For the purposes of this subclause,
the cv-qualification and value category of each operand
are determined by assuming
that each template type parameter
denotes a cv-unqualified complete non-array object type.
\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
\term{implicit expression variations} are required to meet the semantic
requirements of the declared expression. The extent to which an implementation
validates the syntax of the variations is unspecified.
\pnum
\begin{example}
\begin{codeblock}
template<class T> concept 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}
For the above example:
\begin{itemize}
\item
Expression \#1 does not modify either of its operands, \#2 modifies both of its
operands, and \#3 modifies only its first operand \tcode{a}.
\item
Expression \#1 implicitly requires additional expression variations that meet
the requirements for \tcode{c == d} (including non-modification), as if the
expressions
\begin{codeblock}
c == b;
c == std::move(d); c == std::move(b);
std::move(c) == d; std::move(c) == b;
std::move(c) == std::move(d); std::move(c) == std::move(b);
a == d; a == b;
a == std::move(d); a == std::move(b);
std::move(a) == d; std::move(a) == b;
std::move(a) == std::move(d); std::move(a) == std::move(b);
\end{codeblock}
had been declared as well.
\item
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 = std::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.
\end{itemize}
\end{example}
\pnum
\begin{example}
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{T} satisfies but does not model \tcode{C}.
Since implementations are not required to validate the syntax
of implicit requirements, it is unspecified whether an implementation diagnoses
as ill-formed a program that requires \tcode{C<T>}.
\end{example}
\rSec1[concepts.syn]{Header \tcode{<concepts>} synopsis}
\indexheader{concepts}%
\begin{codeblock}
// all freestanding
namespace std {
// \ref{concepts.lang}, language-related concepts
// \ref{concept.same}, concept \libconcept{same_as}
template<class T, class U>
concept same_as = @\seebelow@;
// \ref{concept.derived}, concept \libconcept{derived_from}
template<class Derived, class Base>
concept derived_from = @\seebelow@;
// \ref{concept.convertible}, concept \libconcept{convertible_to}
template<class From, class To>
concept convertible_to = @\seebelow@;
// \ref{concept.commonref}, concept \libconcept{common_reference_with}
template<class T, class U>
concept common_reference_with = @\seebelow@;
// \ref{concept.common}, concept \libconcept{common_with}
template<class T, class U>
concept common_with = @\seebelow@;
// \ref{concepts.arithmetic}, arithmetic concepts
template<class T>
concept integral = @\seebelow@;
template<class T>
concept signed_integral = @\seebelow@;
template<class T>
concept unsigned_integral = @\seebelow@;
template<class T>
concept floating_point = @\seebelow@;
// \ref{concept.assignable}, concept \libconcept{assignable_from}
template<class LHS, class RHS>
concept assignable_from = @\seebelow@;
// \ref{concept.swappable}, concept \libconcept{swappable}
namespace ranges {
inline namespace @\unspec@ {
inline constexpr @\unspec@ swap = @\unspec@;
}
}
template<class T>
concept swappable = @\seebelow@;
template<class T, class U>
concept swappable_with = @\seebelow@;
// \ref{concept.destructible}, concept \libconcept{destructible}
template<class T>
concept destructible = @\seebelow@;
// \ref{concept.constructible}, concept \libconcept{constructible_from}
template<class T, class... Args>
concept constructible_from = @\seebelow@;
// \ref{concept.default.init}, concept \libconcept{default_initializable}
template<class T>
concept default_initializable = @\seebelow@;
// \ref{concept.moveconstructible}, concept \libconcept{move_constructible}
template<class T>
concept move_constructible = @\seebelow@;
// \ref{concept.copyconstructible}, concept \libconcept{copy_constructible}
template<class T>
concept copy_constructible = @\seebelow@;
// \ref{concepts.compare}, comparison concepts
// \ref{concept.equalitycomparable}, concept \libconcept{equality_comparable}
template<class T>
concept equality_comparable = @\seebelow@;
template<class T, class U>
concept equality_comparable_with = @\seebelow@;
// \ref{concept.totallyordered}, concept \libconcept{totally_ordered}
template<class T>
concept totally_ordered = @\seebelow@;
template<class T, class U>
concept totally_ordered_with = @\seebelow@;
// \ref{concepts.object}, object concepts
template<class T>
concept movable = @\seebelow@;
template<class T>
concept copyable = @\seebelow@;
template<class T>
concept semiregular = @\seebelow@;
template<class T>
concept regular = @\seebelow@;
// \ref{concepts.callable}, callable concepts
// \ref{concept.invocable}, concept \libconcept{invocable}
template<class F, class... Args>
concept invocable = @\seebelow@;
// \ref{concept.regularinvocable}, concept \libconcept{regular_invocable}
template<class F, class... Args>
concept regular_invocable = @\seebelow@;
// \ref{concept.predicate}, concept \libconcept{predicate}
template<class F, class... Args>
concept predicate = @\seebelow@;
// \ref{concept.relation}, concept \libconcept{relation}
template<class R, class T, class U>
concept relation = @\seebelow@;
// \ref{concept.equiv}, concept \libconcept{equivalence_relation}
template<class R, class T, class U>
concept equivalence_relation = @\seebelow@;
// \ref{concept.strictweakorder}, concept \libconcept{strict_weak_order}
template<class R, class T, class U>
concept strict_weak_order = @\seebelow@;
}
\end{codeblock}
\rSec1[concepts.lang]{Language-related concepts}
\rSec2[concepts.lang.general]{General}
\pnum
Subclause \ref{concepts.lang} contains the definition of concepts corresponding to language
features. These concepts express relationships between types, type
classifications, and fundamental type properties.
\rSec2[concept.same]{Concept \cname{same_as}}
\begin{itemdecl}
template<class T, class U>
concept @\defexposconcept{same-as-impl}@ = is_same_v<T, U>; // \expos
template<class T, class U>
concept @\deflibconcept{same_as}@ = @\exposconcept{same-as-impl}@<T, U> && @\exposconcept{same-as-impl}@<U, T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\begin{note}
\tcode{\libconcept{same_as}<T, U>} subsumes \tcode{\libconcept{same_as}<U, T>} and
vice versa.
\end{note}
\end{itemdescr}
\rSec2[concept.derived]{Concept \cname{derived_from}}
\begin{itemdecl}
template<class Derived, class Base>
concept @\deflibconcept{derived_from}@ =
is_base_of_v<Base, Derived> &&
is_convertible_v<const volatile Derived*, const volatile Base*>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\begin{note}
\tcode{\libconcept{derived_from}<Derived, Base>} is satisfied if and only if
\tcode{Derived} is publicly and unambiguously derived from \tcode{Base}, or
\tcode{Derived} and \tcode{Base} are the same class type ignoring cv-qualifiers.
\end{note}
\end{itemdescr}
\rSec2[concept.convertible]{Concept \cname{convertible_to}}
\pnum
Given types \tcode{From} and \tcode{To} and
an expression \tcode{E}
whose type and value category are the same as those of \tcode{declval<From>()},
\tcode{\libconcept{convertible_to}<From, To>} requires \tcode{E}
to be both implicitly and explicitly convertible to type \tcode{To}.
The implicit and explicit conversions are required to produce equal
results.
\begin{itemdecl}
template<class From, class To>
concept @\deflibconcept{convertible_to}@ =
is_convertible_v<From, To> &&
requires {
static_cast<To>(declval<From>());
};
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{FromR} be \tcode{add_rvalue_reference_t<From>} and
\tcode{test} be the invented function:
\begin{codeblock}
To test(FromR (&f)()) {
return f();
}
\end{codeblock}
and let \tcode{f} be a function with no arguments and return type \tcode{FromR}
such that \tcode{f()} is equality-preserving.
Types \tcode{From} and \tcode{To} model \tcode{\libconcept{convertible_to}<From, To>}
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{FromR} is not a reference-to-object type, or
\begin{itemize}
\item
If \tcode{FromR} 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\iref{lib.types.movedfrom}.
\item
Otherwise, the object referred to by \tcode{f()} is not modified by either above
expression.
\end{itemize}
\end{itemize}
\end{itemdescr}
\rSec2[concept.commonref]{Concept \cname{common_reference_with}}
\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{\libconcept{convertible_to}<T, C>}
and
\tcode{\libconcept{convertible_to}<U, C>}
are modeled, then \tcode{T} and \tcode{U} share a
\term{common reference type}, \tcode{C}.
\begin{note}
\tcode{C} can be the same as \tcode{T} or \tcode{U}, or can be a
different type. \tcode{C} can be a reference type.
\end{note}
\begin{itemdecl}
template<class T, class U>
concept @\deflibconcept{common_reference_with}@ =
@\libconcept{same_as}@<common_reference_t<T, U>, common_reference_t<U, T>> &&
@\libconcept{convertible_to}@<T, common_reference_t<T, U>> &&
@\libconcept{convertible_to}@<U, common_reference_t<T, U>>;
\end{itemdecl}
\begin{itemdescr}
\pnum
Let \tcode{C} be \tcode{common_reference_t<T, U>}.
Let \tcode{t1} and \tcode{t2} be equality-preserving
expressions\iref{concepts.equality} such that
\tcode{decltype((t1))} and \tcode{decltype((t2))} are each \tcode{T}, and
let \tcode{u1} and \tcode{u2} be equality-preserving expressions such that
\tcode{decltype((u1))} and \tcode{decltype((u2))} are each \tcode{U}.
\tcode{T} and \tcode{U} model \tcode{\libconcept{common_reference_with}<T, U>}
only if
\begin{itemize}
\item \tcode{C(t1)} equals \tcode{C(t2)} if and only if
\tcode{t1} equals \tcode{t2}, and
\item \tcode{C(u1)} equals \tcode{C(u2)} if and only if
\tcode{u1} equals \tcode{u2}.
\end{itemize}
\pnum
\begin{note}
Users can customize the behavior of \libconcept{common_reference_with} by specializing
the \tcode{basic_common_reference} class template\iref{meta.trans.other}.
\end{note}
\end{itemdescr}
\rSec2[concept.common]{Concept \cname{common_with}}
\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 \term{common type},
\tcode{C}.
\begin{note}
\tcode{C} can be the same as \tcode{T} or \tcode{U}, or can be a
different type. \tcode{C} is not necessarily unique.
\end{note}
\begin{itemdecl}
template<class T, class U>
concept @\deflibconcept{common_with}@ =
@\libconcept{same_as}@<common_type_t<T, U>, common_type_t<U, T>> &&
requires {
static_cast<common_type_t<T, U>>(declval<T>());
static_cast<common_type_t<T, U>>(declval<U>());
} &&
@\libconcept{common_reference_with}@<
add_lvalue_reference_t<const T>,
add_lvalue_reference_t<const U>> &&
@\libconcept{common_reference_with}@<
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{t1} and \tcode{t2} be
equality-preserving expressions\iref{concepts.equality} such that
\tcode{decltype((t1))} and \tcode{decltype((t2))} are each \tcode{T}, and
let \tcode{u1} and \tcode{u2} be equality-preserving expressions such that
\tcode{decltype((u1))} and \tcode{decltype((u2))} are each \tcode{U}.
\tcode{T} and \tcode{U} model \tcode{\libconcept{common_with}<T, U>}
only if
\begin{itemize}
\item \tcode{C(t1)} equals \tcode{C(t2)} if and only if
\tcode{t1} equals \tcode{t2}, and
\item \tcode{C(u1)} equals \tcode{C(u2)} if and only if
\tcode{u1} equals \tcode{u2}.
\end{itemize}
\pnum
\begin{note}
Users can customize the behavior of \libconcept{common_with} by specializing the
\tcode{common_type} class template\iref{meta.trans.other}.
\end{note}
\end{itemdescr}
\rSec2[concepts.arithmetic]{Arithmetic concepts}
\begin{itemdecl}
template<class T>
concept @\deflibconcept{integral}@ = is_integral_v<T>;
template<class T>
concept @\deflibconcept{signed_integral}@ = @\libconcept{integral}@<T> && is_signed_v<T>;
template<class T>
concept @\deflibconcept{unsigned_integral}@ = @\libconcept{integral}@<T> && !@\libconcept{signed_integral}@<T>;
template<class T>
concept @\deflibconcept{floating_point}@ = is_floating_point_v<T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\begin{note}
\libconcept{signed_integral} can be modeled even by types that are
not signed integer types\iref{basic.fundamental}; for example, \tcode{char}.
\end{note}
\pnum
\begin{note}
\libconcept{unsigned_integral} can be modeled even by types that are
not unsigned integer types\iref{basic.fundamental}; for example, \tcode{bool}.
\end{note}
\end{itemdescr}
\rSec2[concept.assignable]{Concept \cname{assignable_from}}
\begin{itemdecl}
template<class LHS, class RHS>
concept @\deflibconcept{assignable_from}@ =
is_lvalue_reference_v<LHS> &&
@\libconcept{common_reference_with}@<const remove_reference_t<LHS>&, const remove_reference_t<RHS>&> &&
requires(LHS lhs, RHS&& rhs) {
{ lhs = std::forward<RHS>(rhs) } -> @\libconcept{same_as}@<LHS>;
};
\end{itemdecl}
\begin{itemdescr}
\pnum
Let:
\begin{itemize}
\item \tcode{lhs} be an lvalue that refers to an object \tcode{lcopy} such that
\tcode{decltype((lhs))} is \tcode{LHS},
\item \tcode{rhs} be an expression such that \tcode{decltype((rhs))} is
\tcode{RHS}, and
\item \tcode{rcopy} be a distinct object that is equal to \tcode{rhs}.
\end{itemize}
\tcode{LHS} and \tcode{RHS} model
\tcode{\libconcept{assignable_from}<LHS, RHS>} only if
\begin{itemize}
\item \tcode{addressof(lhs = rhs) == addressof(lcopy)}.
\item After evaluating \tcode{lhs = rhs}:
\begin{itemize}
\item \tcode{lhs} is equal to \tcode{rcopy}, unless \tcode{rhs} is a non-const
xvalue that refers to \tcode{lcopy}.
\item If \tcode{rhs} is a non-\keyword{const} xvalue, the resulting state of the
object to which it refers is valid but unspecified\iref{lib.types.movedfrom}.
\item Otherwise, if \tcode{rhs} is a glvalue, the object to which it refers is
not modified.
\end{itemize}
\end{itemize}
\pnum
\begin{note}
Assignment need not be a total function\iref{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{=}.
\end{note}
\end{itemdescr}
\rSec2[concept.swappable]{Concept \cname{swappable}}
\pnum
Let \tcode{t1} and \tcode{t2} be equality-preserving expressions that denote
distinct equal objects of type \tcode{T}, and let \tcode{u1} and \tcode{u2}
similarly denote distinct equal objects of type \tcode{U}.
\begin{note}
\tcode{t1} and \tcode{u1} can denote distinct objects, or the same object.
\end{note}
An operation
\term{exchanges the values} denoted by \tcode{t1} and \tcode{u1} if and only
if the operation modifies neither \tcode{t2} nor \tcode{u2} and:
\begin{itemize}
\item If \tcode{T} and \tcode{U} are the same type, the result of the operation
is that \tcode{t1} equals \tcode{u2} and \tcode{u1} equals \tcode{t2}.
\item If \tcode{T} and \tcode{U} are different types and
\tcode{\libconcept{common_reference_with}<decltype((t1)), decltype((u1))>}
is modeled,
the result of the operation is that
\tcode{C(t1)} equals \tcode{C(u2)}
and
\tcode{C(u1)} equals \tcode{C(t2)}
where \tcode{C} is \tcode{common_reference_t<decltype((t1)), decltype((u1))>}.
\end{itemize}
\pnum
\indexlibraryglobal{swap}%
The name \tcode{ranges::swap} denotes a customization point
object\iref{customization.point.object}. The expression
\tcode{ranges::swap(E1, E2)} for subexpressions \tcode{E1}
and \tcode{E2} is expression-equivalent to an expression
\tcode{S} determined as follows:
\begin{itemize}
\item
\tcode{S} is \tcode{(void)swap(E1, E2)}
\begin{footnote}
The name \tcode{swap} is used
here unqualified.
\end{footnote}
if \tcode{E1} or \tcode{E2}
has class or enumeration type\iref{basic.compound} and that expression is valid, with
overload resolution performed in a context that includes the declaration
\begin{codeblock}
template<class T>
void swap(T&, T&) = delete;
\end{codeblock}
and does not include a declaration of \tcode{ranges::swap}.
If the function selected by overload resolution does not
exchange the values denoted by
\tcode{E1} and \tcode{E2},
the program is ill-formed, no diagnostic required.
\begin{note}
This precludes calling unconstrained program-defined overloads of
\tcode{swap}. When the deleted overload is viable, program-defined overloads
need to be more specialized\iref{temp.func.order} to be selected.
\end{note}
\item
Otherwise, if \tcode{E1} and \tcode{E2}
are lvalues of array types\iref{basic.compound}
with equal extent and \tcode{ranges::swap(*E1, *E2)}
is a valid expression,
\tcode{S} is \tcode{(void)ranges::swap_ranges(E1, E2)},
except that
\tcode{noexcept(S)} is equal to
\tcode{noexcept(\brk{}ranges::swap(*E1, *E2))}.
\item
Otherwise, if \tcode{E1} and \tcode{E2} are lvalues of the
same type \tcode{T} that models \tcode{\libconcept{move_constructible}<T>} and
\tcode{\libconcept{assignable_from}<T\&, T>},
\tcode{S} is an expression that exchanges the denoted values.
\tcode{S} is a constant expression if
\begin{itemize}
\item \tcode{T} is a literal type\iref{term.literal.type},
\item both \tcode{E1 = std::move(E2)} and \tcode{E2 = std::move(E1)} are
constant subexpressions\iref{defns.const.subexpr}, and
\item the full-expressions of the initializers in the declarations
\begin{codeblock}
T t1(std::move(E1));
T t2(std::move(E2));
\end{codeblock}
are constant subexpressions.
\end{itemize}
\tcode{noexcept(S)} is equal to
\tcode{is_nothrow_move_constructible_v<T> \&\& is_nothrow_move_assignable\-_v<T>}.
\item
Otherwise, \tcode{ranges::swap(E1, E2)} is ill-formed.
\begin{note}
This case can result in substitution failure when \tcode{ranges::swap(E1, E2)}
appears in the immediate context of a template instantiation.
\end{note}
\end{itemize}
\pnum
\begin{note}
Whenever \tcode{ranges::swap(E1, E2)} is a valid expression, it
exchanges the values denoted by
\tcode{E1} and \tcode{E2} and has type \keyword{void}.
\end{note}
\begin{itemdecl}
template<class T>
concept @\deflibconcept{swappable}@ = requires(T& a, T& b) { ranges::swap(a, b); };
\end{itemdecl}
\begin{itemdecl}
template<class T, class U>
concept @\deflibconcept{swappable_with}@ =
@\libconcept{common_reference_with}@<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}
\pnum
\begin{note}
The semantics of the \libconcept{swappable} and \libconcept{swappable_with}
concepts are fully defined by the \tcode{ranges::swap} customization point object.
\end{note}
\pnum
\begin{example}
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 <cassert>
#include <concepts>
#include <utility>
namespace ranges = std::ranges;
template<class T, std::@\libconcept{swappable_with}@<T> U>
void value_swap(T&& t, U&& u) {
ranges::swap(std::forward<T>(t), std::forward<U>(u));
}
template<std::@\libconcept{swappable}@ T>
void lv_swap(T& t1, T& t2) {
ranges::swap(t1, t2);
}
namespace N {
struct A { int m; };
struct Proxy {
A* a;
Proxy(A& a) : a{&a} {}
friend void swap(Proxy x, Proxy y) {
ranges::swap(*x.a, *y.a);
}
};
Proxy proxy(A& a) { return Proxy{ a }; }
}
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}
\end{example}
\rSec2[concept.destructible]{Concept \cname{destructible}}
\pnum
The \libconcept{destructible} concept specifies properties of all types,
instances of which can be destroyed at the end of their lifetime, or reference
types.
\begin{itemdecl}
template<class T>
concept @\deflibconcept{destructible}@ = is_nothrow_destructible_v<T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
\begin{note}
Unlike the \oldconcept{Destructible} requirements~(\tref{cpp17.destructible}), this
concept forbids destructors that are potentially throwing, even if a particular
invocation of the destructor does not actually throw.
\end{note}
\end{itemdescr}
\rSec2[concept.constructible]{Concept \cname{constructible_from}}
\pnum
The \libconcept{constructible_from} concept constrains the initialization of a
variable of a given type with a particular set of argument types.
\begin{itemdecl}
template<class T, class... Args>
concept @\deflibconcept{constructible_from}@ = @\libconcept{destructible}@<T> && is_constructible_v<T, Args...>;
\end{itemdecl}
\rSec2[concept.default.init]{Concept \cname{default_initializable}}
\begin{itemdecl}
template<class T>
constexpr bool @\exposid{is-default-initializable}@ = @\seebelow@; // \expos
template<class T>
concept @\deflibconcept{default_initializable}@ = @\libconcept{constructible_from}@<T> &&
requires { T{}; } &&
@\exposid{is-default-initializable}@<T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
For a type \tcode{T}, \tcode{\exposid{is-default-initializable}<T>} is \tcode{true}
if and only if the variable definition
\begin{codeblock}
T t;
\end{codeblock}
is well-formed for some invented variable \tcode{t};
otherwise it is \tcode{false}.
Access checking is performed as if in a context unrelated to \tcode{T}.
Only the validity of the immediate context of the variable initialization is considered.
\end{itemdescr}
\rSec2[concept.moveconstructible]{Concept \cname{move_constructible}}
\begin{itemdecl}
template<class T>
concept @\deflibconcept{move_constructible}@ = @\libconcept{constructible_from}@<T, T> && @\libconcept{convertible_to}@<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{T} models \libconcept{move_constructible} 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 \keyword{const}, \tcode{rv}'s resulting state is valid
but unspecified\iref{lib.types.movedfrom}; otherwise, it is unchanged.
\end{itemize}
\end{itemdescr}
\rSec2[concept.copyconstructible]{Concept \cname{copy_constructible}}
\begin{itemdecl}
template<class T>
concept @\deflibconcept{copy_constructible}@ =
@\libconcept{move_constructible}@<T> &&
@\libconcept{constructible_from}@<T, T&> && @\libconcept{convertible_to}@<T&, T> &&
@\libconcept{constructible_from}@<T, const T&> && @\libconcept{convertible_to}@<const T&, T> &&
@\libconcept{constructible_from}@<T, const T> && @\libconcept{convertible_to}@<const T, T>;
\end{itemdecl}
\begin{itemdescr}
\pnum
If \tcode{T} is an object type, then let \tcode{v} be an lvalue of type
\tcode{T} or \tcode{\keyword{const} T} or an rvalue of type \tcode{\keyword{const} T}.
\tcode{T} models \libconcept{copy_constructible} only if
\begin{itemize}
\item After the definition \tcode{T u = v;},
\tcode{u} is equal to \tcode{v}\iref{concepts.equality} and
\tcode{v} is not modified.
\item \tcode{T(v)} is equal to \tcode{v} and does not modify \tcode{v}.
\end{itemize}
\end{itemdescr}
\rSec1[concepts.compare]{Comparison concepts}
\rSec2[concepts.compare.general]{General}
\pnum
Subclause \ref{concepts.compare} describes concepts that establish relationships and orderings
on values of possibly differing object types.
\pnum
Given an expression \tcode{E} and a type \tcode{C},
let \tcode{\exposid{CONVERT_TO_LVALUE}<C>(E)} be:
\begin{itemize}
\item
\tcode{static_cast<const C\&>(as_const(E))} if that is a valid expression, and
\item
\tcode{static_cast<const C\&>(std::move(E))} otherwise.
\end{itemize}
\rSec2[concept.booleantestable]{Boolean testability}
\pnum
The exposition-only \exposconcept{boolean-testable} concept
specifies the requirements on expressions
that are convertible to \tcode{bool} and
for which the logical operators\iref{expr.log.and,expr.log.or,expr.unary.op}
have the conventional semantics.
\begin{itemdecl}
template<class T>
concept @\defexposconcept{boolean-testable-impl}@ = @\libconcept{convertible_to}@<T, bool>; // \expos
\end{itemdecl}
\pnum
Let \tcode{e} be an expression such that
\tcode{decltype((e))} is \tcode{T}.
\tcode{T} models \exposconcept{boolean-testable-impl} only if
\begin{itemize}
\item
either \tcode{remove_cvref_t<T>} is not a class type, or
a search for the names \tcode{operator\&\&} and \tcode{operator||}
in the scope of \tcode{remove_cvref_t<T>}
finds nothing; and
\item
argument-dependent lookup\iref{basic.lookup.argdep}
for the names \tcode{operator\&\&} and \tcode{operator||}
with \tcode{T} as the only argument type
finds no disqualifying declaration (defined below).
\end{itemize}
\pnum
A \defnadj{disqualifying}{parameter}
is a function parameter whose declared type \tcode{P}
\begin{itemize}
\item
is not dependent on a template parameter, and
there exists an implicit conversion sequence\iref{over.best.ics}
from \tcode{e} to \tcode{P}; or
\item
is dependent on one or more template parameters, and either
\begin{itemize}
\item
\tcode{P} contains no template parameter that
participates in template argument deduction\iref{temp.deduct.type}, or
\item
template argument deduction
using the rules for deducing template arguments
in a function call\iref{temp.deduct.call} and
\tcode{e} as the argument succeeds.
\end{itemize}
\end{itemize}
\pnum
\indextext{template!function!key parameter of}%
A \defnadj{key}{parameter} of a function template \tcode{D}
is a function parameter of type \cv{} \tcode{X} or reference thereto,
where \tcode{X} names a specialization of a class template that
has the same innermost enclosing non-inline namespace as \tcode{D}, and
\tcode{X} contains at least one template parameter that
participates in template argument deduction.
\begin{example}
In
\begin{codeblock}
namespace Z {
template<class> struct C {};
template<class T>
void operator&&(C<T> x, T y);
template<class T>
void operator||(C<type_identity_t<T>> x, T y);
}
\end{codeblock}
the declaration of \tcode{Z::operator\&\&}
contains one key parameter, \tcode{C<T> x}, and
the declaration of \tcode{Z::operator||}
contains no key parameters.
\end{example}
\pnum
A \defnadj{disqualifying}{declaration} is
\begin{itemize}
\item
a (non-template) function declaration that
contains at least one disqualifying parameter; or
\item
a function template declaration that
contains at least one disqualifying parameter, where
\begin{itemize}
\item at least one disqualifying parameter is a key parameter; or
\item the declaration contains no key parameters; or
\item the declaration declares a function template
to which no name is bound\iref{dcl.meaning}.
\end{itemize}
\end{itemize}
\pnum
\begin{note}
The intention is to ensure that
given two types \tcode{T1} and \tcode{T2}
that each model \exposconcept{boolean-testable-impl},
the \tcode{\&\&} and \tcode{||} operators within the expressions
\tcode{declval<T1>() \&\& declval<T2>()} and
\tcode{declval<T1>() || declval<T2>()}
resolve to the corresponding built-in operators.
\end{note}
\begin{itemdecl}
template<class T>
concept @\defexposconcept{boolean-testable}@ = // \expos
@\exposconcept{boolean-testable-impl}@<T> && requires(T&& t) {
{ !std::forward<T>(t) } -> @\exposconcept{boolean-testable-impl}@;
};
\end{itemdecl}
\pnum
Let \tcode{e} be an expression such that
\tcode{decltype((e))} is \tcode{T}.
\tcode{T} models \exposconcept{boolean-testable} only if
\tcode{bool(e) == !bool(!e)}.
\pnum
\begin{example}
The types
\tcode{bool},
\tcode{true_type}\iref{meta.type.synop},
\tcode{int*}, and
\tcode{bitset<N>::reference}\iref{template.bitset}
model \exposconcept{boolean-testable}.