-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathP0080R0.latex
3049 lines (2400 loc) · 104 KB
/
P0080R0.latex
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
\documentclass[11pt]{article}
\usepackage{color}
\usepackage{xcolor}
\usepackage{fullpage}
\usepackage[colorlinks, allcolors=blue]{hyperref}
\usepackage{listings}
\usepackage{parskip}
\usepackage{url}
\usepackage{courier}
\usepackage[T1]{fontenc}
% \usepackage{libertine}%% Only as example for the romans/sans fonts
\usepackage[scaled=0.85]{beramono}
\usepackage{changepage}
\usepackage{nameref}
% Solarized colour scheme for listings
\definecolor{solarized@base03}{HTML}{002B36}
\definecolor{solarized@base02}{HTML}{073642}
\definecolor{solarized@base01}{HTML}{586e75}
\definecolor{solarized@base00}{HTML}{657b83}
\definecolor{solarized@base0}{HTML}{839496}
\definecolor{solarized@base1}{HTML}{93a1a1}
\definecolor{solarized@base2}{HTML}{EEE8D5}
\definecolor{solarized@base3}{HTML}{FDF6E3}
\definecolor{solarized@yellow}{HTML}{B58900}
\definecolor{solarized@orange}{HTML}{CB4B16}
\definecolor{solarized@red}{HTML}{DC322F}
\definecolor{solarized@magenta}{HTML}{D33682}
\definecolor{solarized@violet}{HTML}{6C71C4}
\definecolor{solarized@blue}{HTML}{268BD2}
\definecolor{solarized@cyan}{HTML}{2AA198}
\definecolor{solarized@green}{HTML}{859900}
\lstset{
basicstyle=\footnotesize\ttfamily,
}
% Define C++ syntax highlighting colour scheme
\lstdefinelanguage{cpp}{
language=C++,
basicstyle=\footnotesize\ttfamily,
numbers=left,
numberstyle=\footnotesize,
tabsize=2,
breaklines=true,
escapeinside={@}{@},
numberstyle=\tiny\color{solarized@base01},
keywordstyle=\color{solarized@green},
stringstyle=\color{solarized@cyan}\ttfamily,
identifierstyle=\color{solarized@blue},
commentstyle=\color{solarized@base01},
emphstyle=\color{solarized@red},
frame=single,
rulecolor=\color{solarized@base2},
rulesepcolor=\color{solarized@base2},
showstringspaces=false,
}
\lstdefinelanguage{diff}{
morecomment=[f][\color{blue}]{@@}, % group identifier
morecomment=[f][\color{red}]{-}, % deleted lines
morecomment=[f][\color{green!50!black}]{+}, % added lines
morecomment=[f][\color{magenta}]{---}, % diff header lines
morecomment=[f][\color{magenta}]{+++},
}
\lstdefinelanguage{plus}{
basicstyle=\footnotesize\ttfamily\color{green!50!black},
emph={see,below,TypeSwitch,unspecified},
emphstyle=\itshape
}
\lstdefinelanguage{signature}{
basicstyle=\ttfamily\color{green!50!black},
emph={see,below,TypeSwitch,unspecified},
emphstyle=\itshape
}
\newcommand{\desc}[1]{\textit{#1}}
\newcommand{\requires}{\desc{Requires}}
\newcommand{\effects}{\desc{Effects}}
\newcommand{\precondition}{\desc{Precondition}}
\newcommand{\postcondition}{\desc{Postcondition}}
\newcommand{\throws}{\desc{Throws}}
\newcommand{\returns}{\desc{Returns}}
\newcommand{\remarks}{\desc{Remarks}}
\newcommand{\exceptionsafety}{\desc{Exception Safety}}
\newcommand{\fullref}[1]{\ref{#1} \nameref{#1}}
\date{}
\title{Variant: Discriminated Union with Value Semantics}
\newcommand{\emailaddress}{[email protected]}
\newcommand{\email}{\href{mailto:\emailaddress}{\emailaddress}}
\begin{document}
\maketitle\vspace{-2cm}
\begin{flushright}
\begin{tabular}{ll}
Document \#:&P0080\\
Date: &\date{2015-07-28}\\
Project: &Programming Language C++\\
&Library Evolution Group\\
Reply-to: &\author{Michael Park}\\
&\textless\email\textgreater
\end{tabular}
\end{flushright}
{\hypersetup{linkcolor=black}
\tableofcontents
}
\section{Introduction}
This paper is an alternative proposal to \cite{N4542}, which proposes a design
for a discriminated union. This topic has been discussed \textbf{extensively}
within the ISO C++ Standards Committee as well as \textbf{std-proposals} mailing
list. The goal of this paper is to identify the concerns of the polarizing groups,
and to propose a solution that would address them with minimal compromise.
\section{Motivation and Scope}
Due to the lack of discriminated union with value semantics in C++, they are
typically handled in one of 3 ways:
\begin{itemize}
\item \texttt{union}-like class, with an \texttt{enum} as the
discriminator
\item Class inheritance hierarchy
\item \texttt{enum}, in the special case where all alternatives are simply
unit types.
\end{itemize}
\subsection{\texttt{union}-like Class}
A \texttt{union}-like class typically consists of the \texttt{union} which
contains one of the possible alternatives, and an \texttt{enum} that represents
the discriminator.
\begin{lstlisting}[language=cpp]
struct Token {
Token(int i) : int_(i), type(Int) {}
Token(std::string s) : string_(std::move(s)), type(String) {}
~Token() {
switch (type) {
case Int:
break;
case String:
using std::string;
string_.~string();
break;
}
}
enum { Int, String } type;
union {
int int_;
std::string string_;
};
};
\end{lstlisting}
Note that after all of this code, this class still only supports construction
and destruction. Even more code would need to be written to support other
operations such as element access, assignment, visitation, etc.
Furthermore, it still only handles exactly one set of types: \texttt{int} and
\texttt{std::string}. A whole new class definition would need to be written for
a different set of types consisting of more or less the exact same code.
\subsection{Class Inheritance Hierarchy}
A class inheritance is a discriminated union in the sense that a pointer to an
abstract base class can only be pointing to an instance of one of the derived
classes.
\begin{lstlisting}[language=cpp]
struct Token {};
struct Int : Token { int value; };
struct String : Token { std::string value; };
\end{lstlisting}
There are several disadvantages to this approach:
\begin{itemize}
\item \texttt{Token} no longer has value semantics. It must be passed around
by pointer or reference in order to avoid object slicing.
\item \texttt{Int}, and \texttt{String} classes needed to be introduced since
\texttt{int} and \texttt{std::string} cannot inherit from \texttt{Token}.
\item If a type needs to be a member of multiple discriminated unions,
multiple inheritance needs to be introduced.
\item The use of virtual functions typically scatters the individual cases
for a single algorithm into different parts of the codebase which becomes
an engineering burden. The visitor pattern is a popular solution to this
problem and also makes it possible for multi-visitation, but it introduces
non-trivial amount of boilerplate code only to support exactly one set of
types.
\end{itemize}
\subsection{\texttt{enum}}
In the special case where the members of a discriminated union do not have
associated data, an \texttt{enum} is typically used to achieve the behavior.
\begin{lstlisting}[language=cpp]
enum class Color { Blue, Green, Red };
\end{lstlisting}
This paper proposes a library solution for a generic, type-safe discriminated
union with value semantics with the class template \texttt{variant<Types...>}.
The following is an example of how the library may be used:
\begin{lstlisting}[language=cpp]
#include <iostream>
#include <string>
#include <variant>
struct custom_equal {
template <class T>
bool operator()(const T& lhs, const T& rhs) const { return lhs == rhs; }
template <class T, class U>
bool operator()(const T&, const U&) const { return false; }
};
int main() {
using namespace std::string_literals;
// direct initialization
std::variant<int, std::string> v("hello world!"s);
// direct access
const std::string& s = std::get<std::string>(v);
assert(s == "hello world!"s);
try {
int i = std::get<int>(v); // throws: std::bad_variant_access
} catch (const std::bad_variant_access& ex) {
// handle exception
}
// copy (and move) construction
std::variant<int, std::string> w(v);
// assignment
try {
v = 42;
} catch (const std::bad_variant_assign& ex) {
// `v` is in an indeterminate state. The exception thrown by the move
// constructor is available as `ex.nested_ptr`.
} catch (const std::exception& ex) {
// v is valid.
}
// visitation
std::type_switch (v) (
[](const auto& value) { std::cout << value; }
); // prints: 42
std::type_switch (w) (
[](int value ) { std::cout << "int: " << value; },
[](const std::string& value) { std::cout << "string: " << value; }
); // prints: string: hello world!
bool result = std::type_switch (v, w) (custom_equal{});
assert(!result);
std::type_switch (v, w) (
[](int , int ) { std::cout << "(int, int)" ; },
[](int , const std::string&) { std::cout << "(int, string)" ; },
[](const std::string&, int ) { std::cout << "(string, int)" ; },
[](const std::string&, const std::string&) { std::cout << "(string, string)"; }
); // prints: (int, string)
\end{lstlisting}
The support for discriminated unions are common in many other languages such as
Haskell, ML, Rust, Swift, and F\#.
\section{Impact on the Standard}
This proposal is a pure library extension, and does not require any new language
features.
\section{Proposed Wording}
Make the following changes to the experimental \texttt{<optional>} header:
\begin{lstlisting}[language=diff]
namespace std {
- constexpr in_place_t in_place{};
+ in_place_t in_place() { return {}; }
- template <class... Args> constexpr explicit optional(in_place_t, Args&&...);
+ template <class... Args> constexpr explicit optional(in_place_t (&)(), Args&&...);
template <class U, class... Args>
- constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
+ constexpr explicit optional(in_place_t (&)(), initializer_list<U>, Args&&...);
}
\end{lstlisting}
Add a new subclause \texttt{20.N Variants} under \texttt{20 General utilities library:}
{\color{green!50!black}
\paragraph{20.N Variants}\hfill\textbf{[variant]}
\paragraph{20.N.1 In general}\hfill\textbf{[variant.general]}
This subclause describes the \texttt{variant} library that provides a
discriminated union as the class template \texttt{variant}. Each template argument
specifies the type of an element that can be stored in the \texttt{variant}
object.
Header \texttt{<variant>} synopsis:
}
\begin{lstlisting}[language=plus]
namespace std {
// 20.N.2 class template variant:
template <class... Types> class variant;
// 20.N.3 class bad_variant_access:
class bad_variant_access;
// 20.N.4 class bad_variant_assign:
class bad_variant_assign;
// 20.N.5 in-place construction:
template <size_t I> in_place_t in_place(integral_constant<size_t, I>) { return {}; }
template <class T> in_place_t in_place(T) { return {}; }
// 20.N.6 nullvar:
struct nullvar_t {};
constexpr nullvar_t nullvar{};
// 20.N.7 element access:
template <size_t I, class... Types>
constexpr remove_reference_t<tuple_element_t<I, tuple<Types...>>>* get(
variant<Types...>*) noexcept;
template <size_t I, class... Types>
constexpr remove_reference_t<const tuple_element_t<I, tuple<Types...>>>* get(
const variant<Types...>*) noexcept;
template <class T, class... Types> // only if see below
constexpr remove_reference_t<T>* get(variant<Types...>*) noexcept;
template <class T, class... Types> // only if see below
constexpr remove_reference_t<const T>* get(const variant<Types...>*) noexcept;
template <size_t I, class... Types>
constexpr tuple_element_t<I, tuple<Types...>>& get(variant<Types...>&);
template <size_t I, class... Types>
constexpr const tuple_element_t<I, tuple<Types...>>& get(const variant<Types...>&);
template <size_t I, class... Types>
constexpr tuple_element_t<I, tuple<Types...>>&& get(variant<Types...>&&);
template <size_t I, class... Types>
constexpr const tuple_element_t<I, tuple<Types...>>&& get(const variant<Types...>&&);
template <class T, class... Types>
constexpr T& get(variant<Types...>&); // only if see below
template <class T, class... Types>
constexpr const T& get(const variant<Types...>&); // only if see below
template <class T, class... Types>
constexpr T&& get(variant<Types...>&&); // only if see below
template <class T, class... Types>
constexpr const T&& get(const variant<Types...>&&); // only if see below
// 20.N.8 relational operators:
constexpr bool operator==(nullvar_t, nullvar_t) { return true; }
constexpr bool operator!=(nullvar_t, nullvar_t) { return false; }
constexpr bool operator< (nullvar_t, nullvar_t) { return false; }
constexpr bool operator> (nullvar_t, nullvar_t) { return false; }
constexpr bool operator<=(nullvar_t, nullvar_t) { return true; }
constexpr bool operator>=(nullvar_t, nullvar_t) { return true; }
template <class... Types>
constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
template <class... Types>
constexpr bool operator!=(const variant<Types...>&lhs,
const variant<Types...>&rhs) { return !(lhs == rhs); }
template <class... Types>
constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
template <class... Types>
constexpr bool operator>(const variant<Types...>&lhs,
const variant<Types...>&rhs) { return rhs < lhs; }
template <class... Types>
constexpr bool operator<=(const variant<Types...>&lhs,
const variant<Types...>&rhs) { return !(lhs > rhs); }
template <class... Types>
constexpr bool operator>=(const variant<Types...>&lhs,
const variant<Types...>&rhs) { return !(lhs < rhs); }
// 20.N.9 specialized algorithms:
template <class... Types>
void swap(variant<Types...>& lhs,
variant<Types...>& rhs) noexcept(noexcept(lhs.swap(rhs)));
// 20.N.10 hash support:
template <> struct hash<nullvar_t>;
template <class... Types> struct hash<variant<Types...>>;
// 20.N.11 visitation:
template <class R = unspecified:deduce_tag, class... Variants>
unspecified:TypeSwitch<R, Variants...> type_switch(Variants&&...);
}
\end{lstlisting}
{\color{green!50!black}
\paragraph{20.N.2 Class template \texttt{variant}}\hfill\textbf{[variant.variant]}
}
\begin{lstlisting}[language=plus]
namespace std {
template <class... Types>
class variant {
public:
// 20.N.2.1 variant construction:
constexpr variant() noexcept;
template <size_t I, class... Args>
explicit constexpr variant(in_place_t (&)(integral_constant<size_t, I>),
Args&&...);
template <size_t I, class U, class... Args> // only if see below
explicit constexpr variant(in_place_t (&)(integral_constant<size_t, I>),
initializer_list<U>,
Args&&...);
template <class T, class... Args>
explicit constexpr variant(in_place_t (&)(T), Args&&...);
template <class T, class U, class... Args> // only if see below
explicit constexpr variant(in_place_t (&)(T), initializer_list<U>, Args&&...);
template <class U> constexpr variant(U&&); // only if see below
variant(const variant&);
variant(variant&&) noexcept(see below);
// 20.N.2.2 variant destruction:
~variant();
// 20.N.2.3 variant assignment:
template <size_t I, class... Args> void emplace(Args&&...);
template <size_t I, class U, class... Args>
void emplace(initializer_list<U>, Args&&...); // only if see below
template <class T, class... Args> void emplace(Args&&...);
template <class T, class U, class... Args>
void emplace(initializer_list<U>, Args&&...); // only if see below
template <class U> variant& operator=(U&&); // only if see below
variant& operator=(const variant&);
variant& operator=(variant&&) noexcept(see below);
// 20.N.2.4 variant observers:
constexpr int index() const noexcept;
constexpr const type_info& type() const noexcept;
// 20.N.2.5 variant swap:
void swap(variant&) noexcept(see below);
};
}
\end{lstlisting}
{\color{green!50!black}
\paragraph{20.N.2.1 Construction}\hfill\textbf{[variant.cnstr]}
For each \texttt{variant} constructor, an exception is thrown only if the
construction of one of the types in \texttt{Types...} throws an exception.
}
\begin{lstlisting}[language=signature]
constexpr variant() noexcept;
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
\effects: Constructs the \texttt{variant} object in an indeterminate state.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <size_t I, class... Args>
explicit constexpr variant(in_place_t (&)(integral_constant<size_t, I>),
Args&&... args);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{T} be the type \texttt{tuple\_element\_t<I, tuple<Types...>{}>}.
\requires: \texttt{I < sizeof...(Types) \&\& is\_constructible\_v<T, Args\&\&...>} is
\texttt{true}
\effects: Initializes the contained value of the \texttt{variant} as if constructing an
object of type \texttt{T} with \texttt{forward<Args>(args)...}.
\postcondition: \texttt{index() == I}
\throws: Any exception thrown by the selected constructor of \texttt{T}.
\remarks: If the selected constructor of \texttt{T} is a \texttt{constexpr} constructor,
this constructor shall be a \texttt{constexpr} constructor.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <size_t I, class U, class... Args> // only if see below
explicit constexpr variant(in_place_t (&ip)(integral_constant<size_t, I>),
initializer_list<U> init,
Args&&... args);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
\effects: Equivalent to
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
template <size_t I, class... Args>
explicit constexpr variant(
in_place_t (&)(integral_constant<size_t, I>), Args&&...);
\end{lstlisting}
\end{adjustwidth}
}
with arguments \texttt{ip, init, forward<Args>(args)...}
\remarks: This constructor shall not participate in overload resolution unless\\
\texttt{is\_constructible\_v< tuple\_element\_t<I, tuple<Types...>{}>,\\
initializer\_list<U>\&, Args\&\&...>} is \texttt{true}. If the selected constructor of
\texttt{T} is a \texttt{constexpr} constructor, this constructor shall be a
\texttt{constexpr} constructor.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <class T, class... Args>
explicit constexpr variant(in_place_t (&)(T), Args&&... args);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the index of \texttt{T} in \texttt{Types...}
\requires: \texttt{T} is in \texttt{Types... \&\& T} is unique within
\texttt{Types....}
\effects: Equivalent to \texttt{variant(in\_place<I>, forward<Args>(args)...);}
\remarks: If the selected constructor of \texttt{T} is a \texttt{constexpr} constructor,
this constructor shall be a \texttt{constexpr} constructor.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <class T, class U, class... Args> // only if see below
explicit constexpr variant(in_place_t (&ip)(T),
initializer_list<U> init,
Args&&... args);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
\effects: Equivalent to
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
template <class T, class... Args>
explicit constexpr variant(in_place_t (&)(T), Args&&...);
\end{lstlisting}
\end{adjustwidth}
}
with arguments \texttt{ip, init, forward<Args>(args)...}
\remarks: This constructor shall not participate in overload resolution unless\\
\texttt{is\_constructible\_v<T, initializer\_list<U>\&, Args\&\&...>} is \texttt{true}.
If the selected constructor of \texttt{T} is a \texttt{constexpr} constructor,
this constructor shall be a \texttt{constexpr} constructor.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <class U>
constexpr variant(U&& u); // only if see below
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{T} be one of the types in \texttt{Types...} for which \texttt{U\&\&} is
unambiguously convertible to by standard overload resolution rules.
\effects: Equivalent to \texttt{variant(in\_place<T>, forward<U>(u));}
\remarks: This constructor shall not participate in overload resolution unless
there is a type \texttt{T} in \texttt{Types...} for which \texttt{U\&\&} is
unambiguously convertible to by standard overload resolution rules. If the selected
constructor of \texttt{T} is a \texttt{constexpr} constructor, this constructor shall
be a \texttt{constexpr} constructor.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
variant(const variant& that);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the constant expression of \texttt{that.index()}.
Let \texttt{T} be \texttt{tuple\_element\_t<I, tuple<Types...>{}>}.
\requires: \texttt{is\_copy\_constructible\_v<U>} is \texttt{true} for all
\texttt{U} in \texttt{Types...}
\effects: Initializes the contained value of the \texttt{variant} as if constructing
an object of type \texttt{T} with \texttt{get<I>(that)}.
\postcondition: \texttt{index() == I}
\throws: Any exception thrown by the selected constructor of \texttt{T}.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
variant(variant&& that) noexcept(see below);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the constant expression of \texttt{that.index()}
Let \texttt{T} be \texttt{tuple\_element\_t<I, tuple<Types...>{}>}
\requires: \texttt{is\_move\_constructible\_v<U>} is \texttt{true} for all
\texttt{U} in \texttt{Types...}
\effects: Initializes the contained value of the \texttt{variant} as if
constructing an object of type \texttt{T} with \texttt{get<I>(std::move(that))}.
\postcondition: \texttt{index() == I}
\throws: Any exception thrown by the selected constructor of \texttt{T}.
\remarks: The expression inside \texttt{noexcept} is equivalent to the logical
\texttt{AND} of\\
\texttt{is\_nothrow\_move\_constructible\_v<U>} for all \texttt{U} in \texttt{Types...}
\end{adjustwidth}
}
{\color{green!50!black}
\paragraph{20.N.2.2 Destruction}\hfill\textbf{[variant.dtor]}
}
\begin{lstlisting}[language=signature]
~variant();
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the constant expression of \texttt{index()}
Let \texttt{T} be \texttt{tuple\_element\_t<I, tuple<Types...>{}>}
\effects: If the \texttt{variant} is initialized, calls
\texttt{get<I>(*this).$_{\widetilde{~}}$T();}
\end{adjustwidth}
}
{\color{green!50!black}
\paragraph{20.N.2.3 Assignment}\hfill\textbf{[variant.assign]}
For each \texttt{variant} assignment operation, an exception is thrown only if the
construction of one of the types in \texttt{Types...} throws an exception.
}
\begin{lstlisting}[language=signature]
template <size_t I, class... Args> void emplace(Args&&...);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{T} be the type \texttt{tuple\_element\_t<I, tuple<Types...>{}>}.
\requires: \texttt{is\_constructible\_v<T, Args\&\&...>} is \texttt{true}
\effects: Destructs the value in \texttt{*this}, then initializes the contained
value of \texttt{*this} as if constructing an object of type \texttt{T} with the
arguments \texttt{forward<Args>(args)...}
\postcondition: \texttt{index() == I}
\throws: \texttt{bad\_variant\_assign} or any exception thrown by the selected
constructor of \texttt{T}.
\exceptionsafety: If an exception \texttt{E} is thrown during the initialization
of the value in \texttt{*this}, \texttt{E} is caught and \texttt{bad\_variant\_assign}
is thrown with \texttt{E} nested within it. In this case, no initialization takes place
and \texttt{variant} is left in an indeterminate state. Otherwise, there are no effects.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <size_t I, class U, class... Args>
void emplace(initializer_list<U> init, Args&&... args); // only if see below
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
\effects: Equivalent to
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
template <size_t I, class... Args> void emplace(Args&&...);
\end{lstlisting}
\end{adjustwidth}
}
with arguments \texttt{init, forward<Args>(args)...}
\remarks: Let \texttt{T} be the type \texttt{tuple\_element\_t<I,
tuple<Types...>{}>}. This function shall not participate in overload resolution unless
\texttt{is\_constructible\_v<T, initializer\_list<U>\&, Args\&\&...>} is
\texttt{true}.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <class T, class... Args> void emplace(Args&&...);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the index of \texttt{T} in \texttt{Types...}
\requires: \texttt{T} is in \texttt{Types... \&\& T} is unique within \texttt{Types...}.
\effects: Equivalent to \texttt{emplace(in\_place<I>, forward<Args>(args)...)};
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <class T, class U, class... Args>
void emplace(initializer_list<U> init, Args&&...); // only if see below
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
\effects: Equivalent to
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
template <class T, class... Args> void emplace(Args&&...);
\end{lstlisting}
\end{adjustwidth}
}
with arguments \texttt{init, forward<Args>(args)...}
\remarks: This constructor shall not participate in overload resolution unless\\
\texttt{is\_constructible\_v<T, initializer\_list<U>\&, Args\&\&...>} is \texttt{true}.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
template <class U> variant& operator=(U&&); // only if see below
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{T} be one of the types in \texttt{Types...} for which
\texttt{U\&\&} is unambiguously convertible to by standard overload resolution rules.
\requires: \texttt{is\_constructible\_v<T, U\&\&>} is \texttt{true \&\&
is\_assignable\_v<T, U\&\&>} is \texttt{true}
\effects: If \texttt{*this} contains a value of type \texttt{T}, effectively performs
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
get<T>(*this) = forward<U>(u);
\end{lstlisting}
\end{adjustwidth}
}
Otherwise, if \texttt{is\_nothrow\_constructible\_v<T, U\&\&>} is \texttt{true}, performs
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
emplace<T>(forward<U>(u);
\end{lstlisting}
\end{adjustwidth}
}
Otherwise, performs
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
emplace<T>(T(forward<U>(u)); // Note the temporary construction.
\end{lstlisting}
\end{adjustwidth}
}
\returns: \texttt{*this}
\remarks: This constructor shall not participate in overload resolution unless
there is a type \texttt{T} in \texttt{Types...} for which \texttt{U\&\&} is
unambiguously convertible to by standard overload resolution rules.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
variant& operator=(const variant& that);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the constant expression of \texttt{that.index()}
Let \texttt{T} be \texttt{tuple\_element\_t<I, tuple<Types...>{}>}
\requires: \texttt{is\_copy\_constructible\_v<U>} is \texttt{true \&\&
is\_copy\_assignable\_v<U>} for all \texttt{U} in \texttt{Types...}
\effects: Equivalent to \texttt{return *this = get<I>(that);}
\returns: \texttt{*this}
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
variant& operator=(variant&& that) noexcept(see below);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the constant expression of \texttt{that.index()}
Let \texttt{T} be \texttt{tuple\_element\_t<I, tuple<Types...>{}>}
\requires: \texttt{is\_move\_constructible\_v<U>} is \texttt{true \&\&
is\_move\_assignable\_v<U>} for all \texttt{U} in \texttt{Types...}
\effects: Equivalent to \texttt{return *this = get<I>(std::move(that));}
\returns: \texttt{*this}
\remarks: The expression inside \texttt{noexcept} is equivalent to the logical
\texttt{AND} of\\
\texttt{is\_nothrow\_move\_constructible\_v<U> \&\& is\_nothrow\_move\_assignable\_v<U>}
for all \texttt{U} in \texttt{Types...}
\end{adjustwidth}
}
{\color{green!50!black}
\paragraph{20.N.2.4 Observers}\hfill\textbf{[variant.observers]}
}
\begin{lstlisting}[language=signature]
constexpr int index() const noexcept;
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
\effects: Returns the index of the currently active alternative.
\end{adjustwidth}
}
\begin{lstlisting}[language=signature]
constexpr const type_info& type() const noexcept;
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the constant expression of \texttt{index()}
Let \texttt{T} be \texttt{tuple\_element\_t<I, tuple<Types...>{}>}
\effects: Returns \texttt{typeid(T)}
\end{adjustwidth}
}
{\color{green!50!black}
\paragraph{20.N.2.5 Swap}\hfill\textbf{[variant.swap]}
}
\begin{lstlisting}[language=signature]
void swap(variant& that) noexcept(see below);
\end{lstlisting}
{
\begin{adjustwidth}{2em}{}
\color{green!50!black}
Let \texttt{I} be the constant expression of \texttt{index()}
Let \texttt{T} be \texttt{tuple\_element\_t<I, tuple<Types...>{}>}
\requires: \texttt{is\_move\_constructible\_v<U>} is \texttt{true} and \texttt{U}
shall satisfy the requirements of\\
\texttt{Swappable} for all \texttt{U} in \texttt{Types...}
\effects: If \texttt{index() == that.index()}, performs
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
using std::swap;
swap(get<I>(*this), get<I>(that));
\end{lstlisting}
\end{adjustwidth}
}
Otherwise, equivalent to calling
{
\begin{adjustwidth}{2em}{}
\begin{lstlisting}[language=signature]
template <class T> void swap(T&, T&);
\end{lstlisting}
\end{adjustwidth}
}
with arguments \texttt{*this, that}
\exceptionsafety: If an exception was thrown during the call to function
\texttt{swap(get<I>(*this), get<I>(that))}, the state of the value of