1// -*- C++ -*-
2//===------------------------------ variant -------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VARIANT
11#define _LIBCPP_VARIANT
12
13/*
14   variant synopsis
15
16namespace std {
17
18  // 20.7.2, class template variant
19  template <class... Types>
20  class variant {
21  public:
22
23    // 20.7.2.1, constructors
24    constexpr variant() noexcept(see below);
25    variant(const variant&);                // constexpr in C++20
26    variant(variant&&) noexcept(see below); // constexpr in C++20
27
28    template <class T> constexpr variant(T&&) noexcept(see below);
29
30    template <class T, class... Args>
31    constexpr explicit variant(in_place_type_t<T>, Args&&...);
32
33    template <class T, class U, class... Args>
34    constexpr explicit variant(
35        in_place_type_t<T>, initializer_list<U>, Args&&...);
36
37    template <size_t I, class... Args>
38    constexpr explicit variant(in_place_index_t<I>, Args&&...);
39
40    template <size_t I, class U, class... Args>
41    constexpr explicit variant(
42        in_place_index_t<I>, initializer_list<U>, Args&&...);
43
44    // 20.7.2.2, destructor
45    ~variant();
46
47    // 20.7.2.3, assignment
48    variant& operator=(const variant&);                // constexpr in C++20
49    variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
50
51    template <class T> variant& operator=(T&&) noexcept(see below);
52
53    // 20.7.2.4, modifiers
54    template <class T, class... Args>
55    T& emplace(Args&&...);
56
57    template <class T, class U, class... Args>
58    T& emplace(initializer_list<U>, Args&&...);
59
60    template <size_t I, class... Args>
61    variant_alternative_t<I, variant>& emplace(Args&&...);
62
63    template <size_t I, class U, class...  Args>
64    variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
65
66    // 20.7.2.5, value status
67    constexpr bool valueless_by_exception() const noexcept;
68    constexpr size_t index() const noexcept;
69
70    // 20.7.2.6, swap
71    void swap(variant&) noexcept(see below);
72  };
73
74  // 20.7.3, variant helper classes
75  template <class T> struct variant_size; // undefined
76
77  template <class T>
78  inline constexpr size_t variant_size_v = variant_size<T>::value;
79
80  template <class T> struct variant_size<const T>;
81  template <class T> struct variant_size<volatile T>;
82  template <class T> struct variant_size<const volatile T>;
83
84  template <class... Types>
85  struct variant_size<variant<Types...>>;
86
87  template <size_t I, class T> struct variant_alternative; // undefined
88
89  template <size_t I, class T>
90  using variant_alternative_t = typename variant_alternative<I, T>::type;
91
92  template <size_t I, class T> struct variant_alternative<I, const T>;
93  template <size_t I, class T> struct variant_alternative<I, volatile T>;
94  template <size_t I, class T> struct variant_alternative<I, const volatile T>;
95
96  template <size_t I, class... Types>
97  struct variant_alternative<I, variant<Types...>>;
98
99  inline constexpr size_t variant_npos = -1;
100
101  // 20.7.4, value access
102  template <class T, class... Types>
103  constexpr bool holds_alternative(const variant<Types...>&) noexcept;
104
105  template <size_t I, class... Types>
106  constexpr variant_alternative_t<I, variant<Types...>>&
107  get(variant<Types...>&);
108
109  template <size_t I, class... Types>
110  constexpr variant_alternative_t<I, variant<Types...>>&&
111  get(variant<Types...>&&);
112
113  template <size_t I, class... Types>
114  constexpr variant_alternative_t<I, variant<Types...>> const&
115  get(const variant<Types...>&);
116
117  template <size_t I, class... Types>
118  constexpr variant_alternative_t<I, variant<Types...>> const&&
119  get(const variant<Types...>&&);
120
121  template <class T, class...  Types>
122  constexpr T& get(variant<Types...>&);
123
124  template <class T, class... Types>
125  constexpr T&& get(variant<Types...>&&);
126
127  template <class T, class... Types>
128  constexpr const T& get(const variant<Types...>&);
129
130  template <class T, class... Types>
131  constexpr const T&& get(const variant<Types...>&&);
132
133  template <size_t I, class... Types>
134  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
135  get_if(variant<Types...>*) noexcept;
136
137  template <size_t I, class... Types>
138  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
139  get_if(const variant<Types...>*) noexcept;
140
141  template <class T, class... Types>
142  constexpr add_pointer_t<T>
143  get_if(variant<Types...>*) noexcept;
144
145  template <class T, class... Types>
146  constexpr add_pointer_t<const T>
147  get_if(const variant<Types...>*) noexcept;
148
149  // 20.7.5, relational operators
150  template <class... Types>
151  constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
152
153  template <class... Types>
154  constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
155
156  template <class... Types>
157  constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
158
159  template <class... Types>
160  constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
161
162  template <class... Types>
163  constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
164
165  template <class... Types>
166  constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
167
168  // 20.7.6, visitation
169  template <class Visitor, class... Variants>
170  constexpr see below visit(Visitor&&, Variants&&...);
171
172  // 20.7.7, class monostate
173  struct monostate;
174
175  // 20.7.8, monostate relational operators
176  constexpr bool operator<(monostate, monostate) noexcept;
177  constexpr bool operator>(monostate, monostate) noexcept;
178  constexpr bool operator<=(monostate, monostate) noexcept;
179  constexpr bool operator>=(monostate, monostate) noexcept;
180  constexpr bool operator==(monostate, monostate) noexcept;
181  constexpr bool operator!=(monostate, monostate) noexcept;
182
183  // 20.7.9, specialized algorithms
184  template <class... Types>
185  void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
186
187  // 20.7.10, class bad_variant_access
188  class bad_variant_access;
189
190  // 20.7.11, hash support
191  template <class T> struct hash;
192  template <class... Types> struct hash<variant<Types...>>;
193  template <> struct hash<monostate>;
194
195} // namespace std
196
197*/
198
199#include <__config>
200#include <__availability>
201#include <__tuple>
202#include <array>
203#include <exception>
204#include <functional>
205#include <initializer_list>
206#include <new>
207#include <tuple>
208#include <type_traits>
209#include <utility>
210#include <limits>
211#include <version>
212
213#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
214#pragma GCC system_header
215#endif
216
217_LIBCPP_PUSH_MACROS
218#include <__undef_macros>
219
220namespace std { // explicitly not using versioning namespace
221
222class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
223public:
224  virtual const char* what() const _NOEXCEPT;
225};
226
227} // namespace std
228
229_LIBCPP_BEGIN_NAMESPACE_STD
230
231// TODO: GCC 5 lies about its support for C++17 (it says it supports it but it
232//       really doesn't). That breaks variant, which uses some C++17 features.
233//       Remove this once we drop support for GCC 5.
234#if _LIBCPP_STD_VER > 14 && !(defined(_LIBCPP_COMPILER_GCC) && _GNUC_VER_NEW < 6000)
235
236_LIBCPP_NORETURN
237inline _LIBCPP_INLINE_VISIBILITY
238_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
239void __throw_bad_variant_access() {
240#ifndef _LIBCPP_NO_EXCEPTIONS
241        throw bad_variant_access();
242#else
243        _VSTD::abort();
244#endif
245}
246
247template <class... _Types>
248class _LIBCPP_TEMPLATE_VIS variant;
249
250template <class _Tp>
251struct _LIBCPP_TEMPLATE_VIS variant_size;
252
253template <class _Tp>
254_LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
255
256template <class _Tp>
257struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
258
259template <class _Tp>
260struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
261
262template <class _Tp>
263struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
264    : variant_size<_Tp> {};
265
266template <class... _Types>
267struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
268    : integral_constant<size_t, sizeof...(_Types)> {};
269
270template <size_t _Ip, class _Tp>
271struct _LIBCPP_TEMPLATE_VIS variant_alternative;
272
273template <size_t _Ip, class _Tp>
274using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
275
276template <size_t _Ip, class _Tp>
277struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
278    : add_const<variant_alternative_t<_Ip, _Tp>> {};
279
280template <size_t _Ip, class _Tp>
281struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
282    : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
283
284template <size_t _Ip, class _Tp>
285struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
286    : add_cv<variant_alternative_t<_Ip, _Tp>> {};
287
288template <size_t _Ip, class... _Types>
289struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
290  static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
291  using type = __type_pack_element<_Ip, _Types...>;
292};
293
294_LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
295
296constexpr int __choose_index_type(unsigned int __num_elem) {
297  if (__num_elem < numeric_limits<unsigned char>::max())
298    return 0;
299  if (__num_elem < numeric_limits<unsigned short>::max())
300    return 1;
301  return 2;
302}
303
304template <size_t _NumAlts>
305using __variant_index_t =
306#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
307  unsigned int;
308#else
309  std::tuple_element_t<
310      __choose_index_type(_NumAlts),
311      std::tuple<unsigned char, unsigned short, unsigned int>
312  >;
313#endif
314
315template <class _IndexType>
316constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
317
318namespace __find_detail {
319
320template <class _Tp, class... _Types>
321inline _LIBCPP_INLINE_VISIBILITY
322constexpr size_t __find_index() {
323  constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
324  size_t __result = __not_found;
325  for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
326    if (__matches[__i]) {
327      if (__result != __not_found) {
328        return __ambiguous;
329      }
330      __result = __i;
331    }
332  }
333  return __result;
334}
335
336template <size_t _Index>
337struct __find_unambiguous_index_sfinae_impl
338    : integral_constant<size_t, _Index> {};
339
340template <>
341struct __find_unambiguous_index_sfinae_impl<__not_found> {};
342
343template <>
344struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
345
346template <class _Tp, class... _Types>
347struct __find_unambiguous_index_sfinae
348    : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
349
350} // namespace __find_detail
351
352namespace __variant_detail {
353
354struct __valueless_t {};
355
356enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
357
358template <typename _Tp,
359          template <typename> class _IsTriviallyAvailable,
360          template <typename> class _IsAvailable>
361constexpr _Trait __trait =
362    _IsTriviallyAvailable<_Tp>::value
363        ? _Trait::_TriviallyAvailable
364        : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
365
366inline _LIBCPP_INLINE_VISIBILITY
367constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
368  _Trait __result = _Trait::_TriviallyAvailable;
369  for (_Trait __t : __traits) {
370    if (static_cast<int>(__t) > static_cast<int>(__result)) {
371      __result = __t;
372    }
373  }
374  return __result;
375}
376
377template <typename... _Types>
378struct __traits {
379  static constexpr _Trait __copy_constructible_trait =
380      __common_trait({__trait<_Types,
381                              is_trivially_copy_constructible,
382                              is_copy_constructible>...});
383
384  static constexpr _Trait __move_constructible_trait =
385      __common_trait({__trait<_Types,
386                              is_trivially_move_constructible,
387                              is_move_constructible>...});
388
389  static constexpr _Trait __copy_assignable_trait = __common_trait(
390      {__copy_constructible_trait,
391       __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
392
393  static constexpr _Trait __move_assignable_trait = __common_trait(
394      {__move_constructible_trait,
395       __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
396
397  static constexpr _Trait __destructible_trait = __common_trait(
398      {__trait<_Types, is_trivially_destructible, is_destructible>...});
399};
400
401namespace __access {
402
403struct __union {
404  template <class _Vp>
405  inline _LIBCPP_INLINE_VISIBILITY
406  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
407    return _VSTD::forward<_Vp>(__v).__head;
408  }
409
410  template <class _Vp, size_t _Ip>
411  inline _LIBCPP_INLINE_VISIBILITY
412  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
413    return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
414  }
415};
416
417struct __base {
418  template <size_t _Ip, class _Vp>
419  inline _LIBCPP_INLINE_VISIBILITY
420  static constexpr auto&& __get_alt(_Vp&& __v) {
421    return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
422                              in_place_index<_Ip>);
423  }
424};
425
426struct __variant {
427  template <size_t _Ip, class _Vp>
428  inline _LIBCPP_INLINE_VISIBILITY
429  static constexpr auto&& __get_alt(_Vp&& __v) {
430    return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
431  }
432};
433
434} // namespace __access
435
436namespace __visitation {
437
438struct __base {
439  template <class _Visitor, class... _Vs>
440  inline _LIBCPP_INLINE_VISIBILITY
441  static constexpr decltype(auto)
442  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
443    constexpr auto __fdiagonal =
444        __make_fdiagonal<_Visitor&&,
445                         decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
446    return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
447                                _VSTD::forward<_Vs>(__vs).__as_base()...);
448  }
449
450  template <class _Visitor, class... _Vs>
451  inline _LIBCPP_INLINE_VISIBILITY
452  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
453                                              _Vs&&... __vs) {
454    constexpr auto __fmatrix =
455        __make_fmatrix<_Visitor&&,
456                       decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
457    return __at(__fmatrix, __vs.index()...)(
458        _VSTD::forward<_Visitor>(__visitor),
459        _VSTD::forward<_Vs>(__vs).__as_base()...);
460  }
461
462private:
463  template <class _Tp>
464  inline _LIBCPP_INLINE_VISIBILITY
465  static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
466
467  template <class _Tp, size_t _Np, typename... _Indices>
468  inline _LIBCPP_INLINE_VISIBILITY
469  static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
470                               size_t __index, _Indices... __indices) {
471    return __at(__elems[__index], __indices...);
472  }
473
474  template <class _Fp, class... _Fs>
475  static constexpr void __std_visit_visitor_return_type_check() {
476    static_assert(
477        __all<is_same_v<_Fp, _Fs>...>::value,
478        "`std::visit` requires the visitor to have a single return type.");
479  }
480
481  template <class... _Fs>
482  inline _LIBCPP_INLINE_VISIBILITY
483  static constexpr auto __make_farray(_Fs&&... __fs) {
484    __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
485    using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
486    return __result{{_VSTD::forward<_Fs>(__fs)...}};
487  }
488
489  template <size_t... _Is>
490  struct __dispatcher {
491    template <class _Fp, class... _Vs>
492    inline _LIBCPP_INLINE_VISIBILITY
493    static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
494        return _VSTD::__invoke_constexpr(
495            static_cast<_Fp>(__f),
496            __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
497    }
498  };
499
500  template <class _Fp, class... _Vs, size_t... _Is>
501  inline _LIBCPP_INLINE_VISIBILITY
502  static constexpr auto __make_dispatch(index_sequence<_Is...>) {
503    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
504  }
505
506  template <size_t _Ip, class _Fp, class... _Vs>
507  inline _LIBCPP_INLINE_VISIBILITY
508  static constexpr auto __make_fdiagonal_impl() {
509    return __make_dispatch<_Fp, _Vs...>(
510        index_sequence<(__identity<_Vs>{}, _Ip)...>{});
511  }
512
513  template <class _Fp, class... _Vs, size_t... _Is>
514  inline _LIBCPP_INLINE_VISIBILITY
515  static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
516    return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
517  }
518
519  template <class _Fp, class _Vp, class... _Vs>
520  inline _LIBCPP_INLINE_VISIBILITY
521  static constexpr auto __make_fdiagonal() {
522    constexpr size_t _Np = __uncvref_t<_Vp>::__size();
523    static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
524    return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
525  }
526
527  template <class _Fp, class... _Vs, size_t... _Is>
528  inline _LIBCPP_INLINE_VISIBILITY
529  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
530    return __make_dispatch<_Fp, _Vs...>(__is);
531  }
532
533  template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
534  inline _LIBCPP_INLINE_VISIBILITY
535  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
536                                            index_sequence<_Js...>,
537                                            _Ls... __ls) {
538    return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
539        index_sequence<_Is..., _Js>{}, __ls...)...);
540  }
541
542  template <class _Fp, class... _Vs>
543  inline _LIBCPP_INLINE_VISIBILITY
544  static constexpr auto __make_fmatrix() {
545    return __make_fmatrix_impl<_Fp, _Vs...>(
546        index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
547  }
548};
549
550struct __variant {
551  template <class _Visitor, class... _Vs>
552  inline _LIBCPP_INLINE_VISIBILITY
553  static constexpr decltype(auto)
554  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
555    return __base::__visit_alt_at(__index,
556                                  _VSTD::forward<_Visitor>(__visitor),
557                                  _VSTD::forward<_Vs>(__vs).__impl...);
558  }
559
560  template <class _Visitor, class... _Vs>
561  inline _LIBCPP_INLINE_VISIBILITY
562  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
563                                              _Vs&&... __vs) {
564    return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
565                               _VSTD::forward<_Vs>(__vs).__impl...);
566  }
567
568  template <class _Visitor, class... _Vs>
569  inline _LIBCPP_INLINE_VISIBILITY
570  static constexpr decltype(auto)
571  __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
572    return __visit_alt_at(
573        __index,
574        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
575        _VSTD::forward<_Vs>(__vs)...);
576  }
577
578  template <class _Visitor, class... _Vs>
579  inline _LIBCPP_INLINE_VISIBILITY
580  static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
581                                                _Vs&&... __vs) {
582    return __visit_alt(
583        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
584        _VSTD::forward<_Vs>(__vs)...);
585  }
586
587private:
588  template <class _Visitor, class... _Values>
589  static constexpr void __std_visit_exhaustive_visitor_check() {
590    static_assert(is_invocable_v<_Visitor, _Values...>,
591                  "`std::visit` requires the visitor to be exhaustive.");
592  }
593
594  template <class _Visitor>
595  struct __value_visitor {
596    template <class... _Alts>
597    inline _LIBCPP_INLINE_VISIBILITY
598    constexpr decltype(auto) operator()(_Alts&&... __alts) const {
599      __std_visit_exhaustive_visitor_check<
600          _Visitor,
601          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
602      return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
603                                _VSTD::forward<_Alts>(__alts).__value...);
604    }
605    _Visitor&& __visitor;
606  };
607
608  template <class _Visitor>
609  inline _LIBCPP_INLINE_VISIBILITY
610  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
611    return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
612  }
613};
614
615} // namespace __visitation
616
617template <size_t _Index, class _Tp>
618struct _LIBCPP_TEMPLATE_VIS __alt {
619  using __value_type = _Tp;
620
621  template <class... _Args>
622  inline _LIBCPP_INLINE_VISIBILITY
623  explicit constexpr __alt(in_place_t, _Args&&... __args)
624      : __value(_VSTD::forward<_Args>(__args)...) {}
625
626  __value_type __value;
627};
628
629template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
630union _LIBCPP_TEMPLATE_VIS __union;
631
632template <_Trait _DestructibleTrait, size_t _Index>
633union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
634
635#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor)                  \
636  template <size_t _Index, class _Tp, class... _Types>                         \
637  union _LIBCPP_TEMPLATE_VIS __union<destructible_trait,                      \
638                                      _Index,                                  \
639                                      _Tp,                                     \
640                                      _Types...> {                             \
641  public:                                                                      \
642    inline _LIBCPP_INLINE_VISIBILITY                                           \
643    explicit constexpr __union(__valueless_t) noexcept : __dummy{} {}          \
644                                                                               \
645    template <class... _Args>                                                  \
646    inline _LIBCPP_INLINE_VISIBILITY                                           \
647    explicit constexpr __union(in_place_index_t<0>, _Args&&... __args)         \
648        : __head(in_place, _VSTD::forward<_Args>(__args)...) {}                \
649                                                                               \
650    template <size_t _Ip, class... _Args>                                      \
651    inline _LIBCPP_INLINE_VISIBILITY                                           \
652    explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args)       \
653        : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
654                                                                               \
655    __union(const __union&) = default;                                         \
656    __union(__union&&) = default;                                              \
657                                                                               \
658    destructor                                                                 \
659                                                                               \
660    __union& operator=(const __union&) = default;                              \
661    __union& operator=(__union&&) = default;                                   \
662                                                                               \
663  private:                                                                     \
664    char __dummy;                                                              \
665    __alt<_Index, _Tp> __head;                                                 \
666    __union<destructible_trait, _Index + 1, _Types...> __tail;                 \
667                                                                               \
668    friend struct __access::__union;                                           \
669  }
670
671_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
672_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
673_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
674
675#undef _LIBCPP_VARIANT_UNION
676
677template <_Trait _DestructibleTrait, class... _Types>
678class _LIBCPP_TEMPLATE_VIS __base {
679public:
680  using __index_t = __variant_index_t<sizeof...(_Types)>;
681
682  inline _LIBCPP_INLINE_VISIBILITY
683  explicit constexpr __base(__valueless_t tag) noexcept
684      : __data(tag), __index(__variant_npos<__index_t>) {}
685
686  template <size_t _Ip, class... _Args>
687  inline _LIBCPP_INLINE_VISIBILITY
688  explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
689      :
690        __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
691        __index(_Ip) {}
692
693  inline _LIBCPP_INLINE_VISIBILITY
694  constexpr bool valueless_by_exception() const noexcept {
695    return index() == variant_npos;
696  }
697
698  inline _LIBCPP_INLINE_VISIBILITY
699  constexpr size_t index() const noexcept {
700    return __index == __variant_npos<__index_t> ? variant_npos : __index;
701  }
702
703protected:
704  inline _LIBCPP_INLINE_VISIBILITY
705  constexpr auto&& __as_base() & { return *this; }
706
707  inline _LIBCPP_INLINE_VISIBILITY
708  constexpr auto&& __as_base() && { return _VSTD::move(*this); }
709
710  inline _LIBCPP_INLINE_VISIBILITY
711  constexpr auto&& __as_base() const & { return *this; }
712
713  inline _LIBCPP_INLINE_VISIBILITY
714  constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
715
716  inline _LIBCPP_INLINE_VISIBILITY
717  static constexpr size_t __size() { return sizeof...(_Types); }
718
719  __union<_DestructibleTrait, 0, _Types...> __data;
720  __index_t __index;
721
722  friend struct __access::__base;
723  friend struct __visitation::__base;
724};
725
726template <class _Traits, _Trait = _Traits::__destructible_trait>
727class _LIBCPP_TEMPLATE_VIS __dtor;
728
729#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy)    \
730  template <class... _Types>                                                   \
731  class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>,                       \
732                                    destructible_trait>                        \
733      : public __base<destructible_trait, _Types...> {                         \
734    using __base_type = __base<destructible_trait, _Types...>;                 \
735    using __index_t = typename __base_type::__index_t;                         \
736                                                                               \
737  public:                                                                      \
738    using __base_type::__base_type;                                            \
739    using __base_type::operator=;                                              \
740                                                                               \
741    __dtor(const __dtor&) = default;                                           \
742    __dtor(__dtor&&) = default;                                                \
743    destructor                                                                 \
744    __dtor& operator=(const __dtor&) = default;                                \
745    __dtor& operator=(__dtor&&) = default;                                     \
746                                                                               \
747  protected:                                                                   \
748    inline _LIBCPP_INLINE_VISIBILITY                                           \
749    destroy                                                                    \
750  }
751
752_LIBCPP_VARIANT_DESTRUCTOR(
753    _Trait::_TriviallyAvailable,
754    ~__dtor() = default;,
755    void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
756
757_LIBCPP_VARIANT_DESTRUCTOR(
758    _Trait::_Available,
759    ~__dtor() { __destroy(); },
760    void __destroy() noexcept {
761      if (!this->valueless_by_exception()) {
762        __visitation::__base::__visit_alt(
763            [](auto& __alt) noexcept {
764              using __alt_type = __uncvref_t<decltype(__alt)>;
765              __alt.~__alt_type();
766            },
767            *this);
768      }
769      this->__index = __variant_npos<__index_t>;
770    });
771
772_LIBCPP_VARIANT_DESTRUCTOR(
773    _Trait::_Unavailable,
774    ~__dtor() = delete;,
775    void __destroy() noexcept = delete;);
776
777#undef _LIBCPP_VARIANT_DESTRUCTOR
778
779template <class _Traits>
780class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
781  using __base_type = __dtor<_Traits>;
782
783public:
784  using __base_type::__base_type;
785  using __base_type::operator=;
786
787protected:
788  template <size_t _Ip, class _Tp, class... _Args>
789  inline _LIBCPP_INLINE_VISIBILITY
790  static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
791    ::new ((void*)_VSTD::addressof(__a))
792        __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
793    return __a.__value;
794  }
795
796  template <class _Rhs>
797  inline _LIBCPP_INLINE_VISIBILITY
798  static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
799    __lhs.__destroy();
800    if (!__rhs.valueless_by_exception()) {
801      __visitation::__base::__visit_alt_at(
802          __rhs.index(),
803          [](auto& __lhs_alt, auto&& __rhs_alt) {
804            __construct_alt(
805                __lhs_alt,
806                _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
807          },
808          __lhs, _VSTD::forward<_Rhs>(__rhs));
809      __lhs.__index = __rhs.index();
810    }
811  }
812};
813
814template <class _Traits, _Trait = _Traits::__move_constructible_trait>
815class _LIBCPP_TEMPLATE_VIS __move_constructor;
816
817#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait,             \
818                                         move_constructor)                     \
819  template <class... _Types>                                                   \
820  class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>,           \
821                                                move_constructible_trait>      \
822      : public __ctor<__traits<_Types...>> {                                   \
823    using __base_type = __ctor<__traits<_Types...>>;                           \
824                                                                               \
825  public:                                                                      \
826    using __base_type::__base_type;                                            \
827    using __base_type::operator=;                                              \
828                                                                               \
829    __move_constructor(const __move_constructor&) = default;                   \
830    move_constructor                                                           \
831    ~__move_constructor() = default;                                           \
832    __move_constructor& operator=(const __move_constructor&) = default;        \
833    __move_constructor& operator=(__move_constructor&&) = default;             \
834  }
835
836_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
837    _Trait::_TriviallyAvailable,
838    __move_constructor(__move_constructor&& __that) = default;);
839
840_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
841    _Trait::_Available,
842    __move_constructor(__move_constructor&& __that) noexcept(
843        __all<is_nothrow_move_constructible_v<_Types>...>::value)
844        : __move_constructor(__valueless_t{}) {
845      this->__generic_construct(*this, _VSTD::move(__that));
846    });
847
848_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
849    _Trait::_Unavailable,
850    __move_constructor(__move_constructor&&) = delete;);
851
852#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
853
854template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
855class _LIBCPP_TEMPLATE_VIS __copy_constructor;
856
857#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait,             \
858                                         copy_constructor)                     \
859  template <class... _Types>                                                   \
860  class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>,          \
861                                                 copy_constructible_trait>     \
862      : public __move_constructor<__traits<_Types...>> {                       \
863    using __base_type = __move_constructor<__traits<_Types...>>;               \
864                                                                               \
865  public:                                                                      \
866    using __base_type::__base_type;                                            \
867    using __base_type::operator=;                                              \
868                                                                               \
869    copy_constructor                                                           \
870    __copy_constructor(__copy_constructor&&) = default;                        \
871    ~__copy_constructor() = default;                                           \
872    __copy_constructor& operator=(const __copy_constructor&) = default;        \
873    __copy_constructor& operator=(__copy_constructor&&) = default;             \
874  }
875
876_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
877    _Trait::_TriviallyAvailable,
878    __copy_constructor(const __copy_constructor& __that) = default;);
879
880_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
881    _Trait::_Available,
882    __copy_constructor(const __copy_constructor& __that)
883        : __copy_constructor(__valueless_t{}) {
884      this->__generic_construct(*this, __that);
885    });
886
887_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
888    _Trait::_Unavailable,
889    __copy_constructor(const __copy_constructor&) = delete;);
890
891#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
892
893template <class _Traits>
894class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
895  using __base_type = __copy_constructor<_Traits>;
896
897public:
898  using __base_type::__base_type;
899  using __base_type::operator=;
900
901  template <size_t _Ip, class... _Args>
902  inline _LIBCPP_INLINE_VISIBILITY
903  auto& __emplace(_Args&&... __args) {
904    this->__destroy();
905    auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
906                          _VSTD::forward<_Args>(__args)...);
907    this->__index = _Ip;
908    return __res;
909  }
910
911protected:
912  template <size_t _Ip, class _Tp, class _Arg>
913  inline _LIBCPP_INLINE_VISIBILITY
914  void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
915    if (this->index() == _Ip) {
916      __a.__value = _VSTD::forward<_Arg>(__arg);
917    } else {
918      struct {
919        void operator()(true_type) const {
920          __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
921        }
922        void operator()(false_type) const {
923          __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
924        }
925        __assignment* __this;
926        _Arg&& __arg;
927      } __impl{this, _VSTD::forward<_Arg>(__arg)};
928      __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
929                           !is_nothrow_move_constructible_v<_Tp>>{});
930    }
931  }
932
933  template <class _That>
934  inline _LIBCPP_INLINE_VISIBILITY
935  void __generic_assign(_That&& __that) {
936    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
937      // do nothing.
938    } else if (__that.valueless_by_exception()) {
939      this->__destroy();
940    } else {
941      __visitation::__base::__visit_alt_at(
942          __that.index(),
943          [this](auto& __this_alt, auto&& __that_alt) {
944            this->__assign_alt(
945                __this_alt,
946                _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
947          },
948          *this, _VSTD::forward<_That>(__that));
949    }
950  }
951};
952
953template <class _Traits, _Trait = _Traits::__move_assignable_trait>
954class _LIBCPP_TEMPLATE_VIS __move_assignment;
955
956#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait,                 \
957                                        move_assignment)                       \
958  template <class... _Types>                                                   \
959  class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>,           \
960                                                move_assignable_trait>         \
961      : public __assignment<__traits<_Types...>> {                             \
962    using __base_type = __assignment<__traits<_Types...>>;                     \
963                                                                               \
964  public:                                                                      \
965    using __base_type::__base_type;                                            \
966    using __base_type::operator=;                                              \
967                                                                               \
968    __move_assignment(const __move_assignment&) = default;                     \
969    __move_assignment(__move_assignment&&) = default;                          \
970    ~__move_assignment() = default;                                            \
971    __move_assignment& operator=(const __move_assignment&) = default;          \
972    move_assignment                                                            \
973  }
974
975_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
976    _Trait::_TriviallyAvailable,
977    __move_assignment& operator=(__move_assignment&& __that) = default;);
978
979_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
980    _Trait::_Available,
981    __move_assignment& operator=(__move_assignment&& __that) noexcept(
982        __all<(is_nothrow_move_constructible_v<_Types> &&
983               is_nothrow_move_assignable_v<_Types>)...>::value) {
984      this->__generic_assign(_VSTD::move(__that));
985      return *this;
986    });
987
988_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
989    _Trait::_Unavailable,
990    __move_assignment& operator=(__move_assignment&&) = delete;);
991
992#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
993
994template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
995class _LIBCPP_TEMPLATE_VIS __copy_assignment;
996
997#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait,                 \
998                                        copy_assignment)                       \
999  template <class... _Types>                                                   \
1000  class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>,           \
1001                                                copy_assignable_trait>         \
1002      : public __move_assignment<__traits<_Types...>> {                        \
1003    using __base_type = __move_assignment<__traits<_Types...>>;                \
1004                                                                               \
1005  public:                                                                      \
1006    using __base_type::__base_type;                                            \
1007    using __base_type::operator=;                                              \
1008                                                                               \
1009    __copy_assignment(const __copy_assignment&) = default;                     \
1010    __copy_assignment(__copy_assignment&&) = default;                          \
1011    ~__copy_assignment() = default;                                            \
1012    copy_assignment                                                            \
1013    __copy_assignment& operator=(__copy_assignment&&) = default;               \
1014  }
1015
1016_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1017    _Trait::_TriviallyAvailable,
1018    __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1019
1020_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1021    _Trait::_Available,
1022    __copy_assignment& operator=(const __copy_assignment& __that) {
1023      this->__generic_assign(__that);
1024      return *this;
1025    });
1026
1027_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1028    _Trait::_Unavailable,
1029    __copy_assignment& operator=(const __copy_assignment&) = delete;);
1030
1031#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1032
1033template <class... _Types>
1034class _LIBCPP_TEMPLATE_VIS __impl
1035    : public __copy_assignment<__traits<_Types...>> {
1036  using __base_type = __copy_assignment<__traits<_Types...>>;
1037
1038public:
1039  using __base_type::__base_type;
1040  using __base_type::operator=;
1041
1042  template <size_t _Ip, class _Arg>
1043  inline _LIBCPP_INLINE_VISIBILITY
1044  void __assign(_Arg&& __arg) {
1045    this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
1046                       _VSTD::forward<_Arg>(__arg));
1047  }
1048
1049  inline _LIBCPP_INLINE_VISIBILITY
1050  void __swap(__impl& __that)  {
1051    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1052      // do nothing.
1053    } else if (this->index() == __that.index()) {
1054      __visitation::__base::__visit_alt_at(
1055          this->index(),
1056          [](auto& __this_alt, auto& __that_alt) {
1057            using _VSTD::swap;
1058            swap(__this_alt.__value, __that_alt.__value);
1059          },
1060          *this,
1061          __that);
1062    } else {
1063      __impl* __lhs = this;
1064      __impl* __rhs = _VSTD::addressof(__that);
1065      if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1066        _VSTD::swap(__lhs, __rhs);
1067      }
1068      __impl __tmp(_VSTD::move(*__rhs));
1069#ifndef _LIBCPP_NO_EXCEPTIONS
1070      if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
1071        this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1072      } else {
1073        // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1074        // and `__tmp` is nothrow move constructible then we move `__tmp` back
1075        // into `__rhs` and provide the strong exception safety guarantee.
1076        try {
1077          this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1078        } catch (...) {
1079          if (__tmp.__move_nothrow()) {
1080            this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1081          }
1082          throw;
1083        }
1084      }
1085#else
1086      // this isn't consolidated with the `if constexpr` branch above due to
1087      // `throw` being ill-formed with exceptions disabled even when discarded.
1088      this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1089#endif
1090      this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1091    }
1092  }
1093
1094private:
1095  inline _LIBCPP_INLINE_VISIBILITY
1096  bool __move_nothrow() const {
1097    constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1098    return this->valueless_by_exception() || __results[this->index()];
1099  }
1100};
1101
1102struct __no_narrowing_check {
1103  template <class _Dest, class _Source>
1104  using _Apply = __identity<_Dest>;
1105};
1106
1107struct __narrowing_check {
1108  template <class _Dest>
1109  static auto __test_impl(_Dest (&&)[1]) -> __identity<_Dest>;
1110  template <class _Dest, class _Source>
1111  using _Apply _LIBCPP_NODEBUG_TYPE = decltype(__test_impl<_Dest>({_VSTD::declval<_Source>()}));
1112};
1113
1114template <class _Dest, class _Source>
1115using __check_for_narrowing _LIBCPP_NODEBUG_TYPE =
1116  typename _If<
1117#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
1118    false &&
1119#endif
1120    is_arithmetic<_Dest>::value,
1121    __narrowing_check,
1122    __no_narrowing_check
1123  >::template _Apply<_Dest, _Source>;
1124
1125template <class _Tp, size_t _Idx>
1126struct __overload {
1127  template <class _Up>
1128  auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
1129};
1130
1131template <class _Tp, size_t>
1132struct __overload_bool  {
1133  template <class _Up, class _Ap = __uncvref_t<_Up>>
1134  auto operator()(bool, _Up&&) const
1135      -> enable_if_t<is_same_v<_Ap, bool>, __identity<_Tp>>;
1136};
1137
1138template <size_t _Idx>
1139struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
1140template <size_t _Idx>
1141struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
1142template <size_t _Idx>
1143struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
1144template <size_t _Idx>
1145struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
1146
1147template <class ..._Bases>
1148struct __all_overloads : _Bases... {
1149  void operator()() const;
1150  using _Bases::operator()...;
1151};
1152
1153template <class IdxSeq>
1154struct __make_overloads_imp;
1155
1156template <size_t ..._Idx>
1157struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1158  template <class ..._Types>
1159  using _Apply _LIBCPP_NODEBUG_TYPE = __all_overloads<__overload<_Types, _Idx>...>;
1160};
1161
1162template <class ..._Types>
1163using _MakeOverloads _LIBCPP_NODEBUG_TYPE = typename __make_overloads_imp<
1164    __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
1165
1166template <class _Tp, class... _Types>
1167using __best_match_t =
1168    typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
1169
1170} // __variant_detail
1171
1172template <class... _Types>
1173class _LIBCPP_TEMPLATE_VIS variant
1174    : private __sfinae_ctor_base<
1175          __all<is_copy_constructible_v<_Types>...>::value,
1176          __all<is_move_constructible_v<_Types>...>::value>,
1177      private __sfinae_assign_base<
1178          __all<(is_copy_constructible_v<_Types> &&
1179                 is_copy_assignable_v<_Types>)...>::value,
1180          __all<(is_move_constructible_v<_Types> &&
1181                 is_move_assignable_v<_Types>)...>::value> {
1182  static_assert(0 < sizeof...(_Types),
1183                "variant must consist of at least one alternative.");
1184
1185  static_assert(__all<!is_array_v<_Types>...>::value,
1186                "variant can not have an array type as an alternative.");
1187
1188  static_assert(__all<!is_reference_v<_Types>...>::value,
1189                "variant can not have a reference type as an alternative.");
1190
1191  static_assert(__all<!is_void_v<_Types>...>::value,
1192                "variant can not have a void type as an alternative.");
1193
1194  using __first_type = variant_alternative_t<0, variant>;
1195
1196public:
1197  template <bool _Dummy = true,
1198            enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1199                                         _Dummy>::value,
1200                        int> = 0>
1201  inline _LIBCPP_INLINE_VISIBILITY
1202  constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1203      : __impl(in_place_index<0>) {}
1204
1205  variant(const variant&) = default;
1206  variant(variant&&) = default;
1207
1208  template <
1209      class _Arg,
1210      enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1211      enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
1212      enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
1213      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1214      size_t _Ip =
1215          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1216      enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1217  inline _LIBCPP_INLINE_VISIBILITY
1218  constexpr variant(_Arg&& __arg) noexcept(
1219      is_nothrow_constructible_v<_Tp, _Arg>)
1220      : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1221
1222  template <size_t _Ip, class... _Args,
1223            class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1224            class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1225            enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1226  inline _LIBCPP_INLINE_VISIBILITY
1227  explicit constexpr variant(
1228      in_place_index_t<_Ip>,
1229      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1230      : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1231
1232  template <
1233      size_t _Ip,
1234      class _Up,
1235      class... _Args,
1236      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1237      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1238      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1239                  int> = 0>
1240  inline _LIBCPP_INLINE_VISIBILITY
1241  explicit constexpr variant(
1242      in_place_index_t<_Ip>,
1243      initializer_list<_Up> __il,
1244      _Args&&... __args) noexcept(
1245      is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1246      : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1247
1248  template <
1249      class _Tp,
1250      class... _Args,
1251      size_t _Ip =
1252          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1253      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1254  inline _LIBCPP_INLINE_VISIBILITY
1255  explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1256      is_nothrow_constructible_v<_Tp, _Args...>)
1257      : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1258
1259  template <
1260      class _Tp,
1261      class _Up,
1262      class... _Args,
1263      size_t _Ip =
1264          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1265      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1266                  int> = 0>
1267  inline _LIBCPP_INLINE_VISIBILITY
1268  explicit constexpr variant(
1269      in_place_type_t<_Tp>,
1270      initializer_list<_Up> __il,
1271      _Args&&... __args) noexcept(
1272      is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1273      : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1274
1275  ~variant() = default;
1276
1277  variant& operator=(const variant&) = default;
1278  variant& operator=(variant&&) = default;
1279
1280  template <
1281      class _Arg,
1282      enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1283      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1284      size_t _Ip =
1285          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1286      enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1287                  int> = 0>
1288  inline _LIBCPP_INLINE_VISIBILITY
1289  variant& operator=(_Arg&& __arg) noexcept(
1290      is_nothrow_assignable_v<_Tp&, _Arg> &&
1291      is_nothrow_constructible_v<_Tp, _Arg>) {
1292    __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1293    return *this;
1294  }
1295
1296  template <
1297      size_t _Ip,
1298      class... _Args,
1299      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1300      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1301      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1302  inline _LIBCPP_INLINE_VISIBILITY
1303  _Tp& emplace(_Args&&... __args) {
1304    return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1305  }
1306
1307  template <
1308      size_t _Ip,
1309      class _Up,
1310      class... _Args,
1311      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1312      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1313      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1314                  int> = 0>
1315  inline _LIBCPP_INLINE_VISIBILITY
1316  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1317    return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1318  }
1319
1320  template <
1321      class _Tp,
1322      class... _Args,
1323      size_t _Ip =
1324          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1325      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1326  inline _LIBCPP_INLINE_VISIBILITY
1327  _Tp& emplace(_Args&&... __args) {
1328    return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1329  }
1330
1331  template <
1332      class _Tp,
1333      class _Up,
1334      class... _Args,
1335      size_t _Ip =
1336          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1337      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1338                  int> = 0>
1339  inline _LIBCPP_INLINE_VISIBILITY
1340  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1341    return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1342  }
1343
1344  inline _LIBCPP_INLINE_VISIBILITY
1345  constexpr bool valueless_by_exception() const noexcept {
1346    return __impl.valueless_by_exception();
1347  }
1348
1349  inline _LIBCPP_INLINE_VISIBILITY
1350  constexpr size_t index() const noexcept { return __impl.index(); }
1351
1352  template <
1353      bool _Dummy = true,
1354      enable_if_t<
1355          __all<(
1356              __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1357              __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1358          int> = 0>
1359  inline _LIBCPP_INLINE_VISIBILITY
1360  void swap(variant& __that) noexcept(
1361      __all<(is_nothrow_move_constructible_v<_Types> &&
1362             is_nothrow_swappable_v<_Types>)...>::value) {
1363    __impl.__swap(__that.__impl);
1364  }
1365
1366private:
1367  __variant_detail::__impl<_Types...> __impl;
1368
1369  friend struct __variant_detail::__access::__variant;
1370  friend struct __variant_detail::__visitation::__variant;
1371};
1372
1373template <size_t _Ip, class... _Types>
1374inline _LIBCPP_INLINE_VISIBILITY
1375constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1376  return __v.index() == _Ip;
1377}
1378
1379template <class _Tp, class... _Types>
1380inline _LIBCPP_INLINE_VISIBILITY
1381constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1382  return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1383}
1384
1385template <size_t _Ip, class _Vp>
1386inline _LIBCPP_INLINE_VISIBILITY
1387_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1388constexpr auto&& __generic_get(_Vp&& __v) {
1389  using __variant_detail::__access::__variant;
1390  if (!__holds_alternative<_Ip>(__v)) {
1391    __throw_bad_variant_access();
1392  }
1393  return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1394}
1395
1396template <size_t _Ip, class... _Types>
1397inline _LIBCPP_INLINE_VISIBILITY
1398_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1399constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1400    variant<_Types...>& __v) {
1401  static_assert(_Ip < sizeof...(_Types));
1402  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1403  return __generic_get<_Ip>(__v);
1404}
1405
1406template <size_t _Ip, class... _Types>
1407inline _LIBCPP_INLINE_VISIBILITY
1408_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1409constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1410    variant<_Types...>&& __v) {
1411  static_assert(_Ip < sizeof...(_Types));
1412  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1413  return __generic_get<_Ip>(_VSTD::move(__v));
1414}
1415
1416template <size_t _Ip, class... _Types>
1417inline _LIBCPP_INLINE_VISIBILITY
1418_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1419constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1420    const variant<_Types...>& __v) {
1421  static_assert(_Ip < sizeof...(_Types));
1422  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1423  return __generic_get<_Ip>(__v);
1424}
1425
1426template <size_t _Ip, class... _Types>
1427inline _LIBCPP_INLINE_VISIBILITY
1428_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1429constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1430    const variant<_Types...>&& __v) {
1431  static_assert(_Ip < sizeof...(_Types));
1432  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1433  return __generic_get<_Ip>(_VSTD::move(__v));
1434}
1435
1436template <class _Tp, class... _Types>
1437inline _LIBCPP_INLINE_VISIBILITY
1438_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1439constexpr _Tp& get(variant<_Types...>& __v) {
1440  static_assert(!is_void_v<_Tp>);
1441  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1442}
1443
1444template <class _Tp, class... _Types>
1445inline _LIBCPP_INLINE_VISIBILITY
1446_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1447constexpr _Tp&& get(variant<_Types...>&& __v) {
1448  static_assert(!is_void_v<_Tp>);
1449  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1450      _VSTD::move(__v));
1451}
1452
1453template <class _Tp, class... _Types>
1454inline _LIBCPP_INLINE_VISIBILITY
1455_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1456constexpr const _Tp& get(const variant<_Types...>& __v) {
1457  static_assert(!is_void_v<_Tp>);
1458  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1459}
1460
1461template <class _Tp, class... _Types>
1462inline _LIBCPP_INLINE_VISIBILITY
1463_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1464constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1465  static_assert(!is_void_v<_Tp>);
1466  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1467      _VSTD::move(__v));
1468}
1469
1470template <size_t _Ip, class _Vp>
1471inline _LIBCPP_INLINE_VISIBILITY
1472constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1473  using __variant_detail::__access::__variant;
1474  return __v && __holds_alternative<_Ip>(*__v)
1475             ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1476             : nullptr;
1477}
1478
1479template <size_t _Ip, class... _Types>
1480inline _LIBCPP_INLINE_VISIBILITY
1481constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1482get_if(variant<_Types...>* __v) noexcept {
1483  static_assert(_Ip < sizeof...(_Types));
1484  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1485  return __generic_get_if<_Ip>(__v);
1486}
1487
1488template <size_t _Ip, class... _Types>
1489inline _LIBCPP_INLINE_VISIBILITY
1490constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1491get_if(const variant<_Types...>* __v) noexcept {
1492  static_assert(_Ip < sizeof...(_Types));
1493  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1494  return __generic_get_if<_Ip>(__v);
1495}
1496
1497template <class _Tp, class... _Types>
1498inline _LIBCPP_INLINE_VISIBILITY
1499constexpr add_pointer_t<_Tp>
1500get_if(variant<_Types...>* __v) noexcept {
1501  static_assert(!is_void_v<_Tp>);
1502  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1503}
1504
1505template <class _Tp, class... _Types>
1506inline _LIBCPP_INLINE_VISIBILITY
1507constexpr add_pointer_t<const _Tp>
1508get_if(const variant<_Types...>* __v) noexcept {
1509  static_assert(!is_void_v<_Tp>);
1510  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1511}
1512
1513template <class _Operator>
1514struct __convert_to_bool {
1515  template <class _T1, class _T2>
1516  _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1517    static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
1518        "the relational operator does not return a type which is implicitly convertible to bool");
1519    return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
1520  }
1521};
1522
1523template <class... _Types>
1524inline _LIBCPP_INLINE_VISIBILITY
1525constexpr bool operator==(const variant<_Types...>& __lhs,
1526                          const variant<_Types...>& __rhs) {
1527  using __variant_detail::__visitation::__variant;
1528  if (__lhs.index() != __rhs.index()) return false;
1529  if (__lhs.valueless_by_exception()) return true;
1530  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1531}
1532
1533template <class... _Types>
1534inline _LIBCPP_INLINE_VISIBILITY
1535constexpr bool operator!=(const variant<_Types...>& __lhs,
1536                          const variant<_Types...>& __rhs) {
1537  using __variant_detail::__visitation::__variant;
1538  if (__lhs.index() != __rhs.index()) return true;
1539  if (__lhs.valueless_by_exception()) return false;
1540  return __variant::__visit_value_at(
1541      __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1542}
1543
1544template <class... _Types>
1545inline _LIBCPP_INLINE_VISIBILITY
1546constexpr bool operator<(const variant<_Types...>& __lhs,
1547                         const variant<_Types...>& __rhs) {
1548  using __variant_detail::__visitation::__variant;
1549  if (__rhs.valueless_by_exception()) return false;
1550  if (__lhs.valueless_by_exception()) return true;
1551  if (__lhs.index() < __rhs.index()) return true;
1552  if (__lhs.index() > __rhs.index()) return false;
1553  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1554}
1555
1556template <class... _Types>
1557inline _LIBCPP_INLINE_VISIBILITY
1558constexpr bool operator>(const variant<_Types...>& __lhs,
1559                         const variant<_Types...>& __rhs) {
1560  using __variant_detail::__visitation::__variant;
1561  if (__lhs.valueless_by_exception()) return false;
1562  if (__rhs.valueless_by_exception()) return true;
1563  if (__lhs.index() > __rhs.index()) return true;
1564  if (__lhs.index() < __rhs.index()) return false;
1565  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1566}
1567
1568template <class... _Types>
1569inline _LIBCPP_INLINE_VISIBILITY
1570constexpr bool operator<=(const variant<_Types...>& __lhs,
1571                          const variant<_Types...>& __rhs) {
1572  using __variant_detail::__visitation::__variant;
1573  if (__lhs.valueless_by_exception()) return true;
1574  if (__rhs.valueless_by_exception()) return false;
1575  if (__lhs.index() < __rhs.index()) return true;
1576  if (__lhs.index() > __rhs.index()) return false;
1577  return __variant::__visit_value_at(
1578      __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1579}
1580
1581template <class... _Types>
1582inline _LIBCPP_INLINE_VISIBILITY
1583constexpr bool operator>=(const variant<_Types...>& __lhs,
1584                          const variant<_Types...>& __rhs) {
1585  using __variant_detail::__visitation::__variant;
1586  if (__rhs.valueless_by_exception()) return true;
1587  if (__lhs.valueless_by_exception()) return false;
1588  if (__lhs.index() > __rhs.index()) return true;
1589  if (__lhs.index() < __rhs.index()) return false;
1590  return __variant::__visit_value_at(
1591      __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1592}
1593
1594template <class _Visitor, class... _Vs>
1595inline _LIBCPP_INLINE_VISIBILITY
1596_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1597constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1598  using __variant_detail::__visitation::__variant;
1599  bool __results[] = {__vs.valueless_by_exception()...};
1600  for (bool __result : __results) {
1601    if (__result) {
1602      __throw_bad_variant_access();
1603    }
1604  }
1605  return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1606                                  _VSTD::forward<_Vs>(__vs)...);
1607}
1608
1609struct _LIBCPP_TEMPLATE_VIS monostate {};
1610
1611inline _LIBCPP_INLINE_VISIBILITY
1612constexpr bool operator<(monostate, monostate) noexcept { return false; }
1613
1614inline _LIBCPP_INLINE_VISIBILITY
1615constexpr bool operator>(monostate, monostate) noexcept { return false; }
1616
1617inline _LIBCPP_INLINE_VISIBILITY
1618constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1619
1620inline _LIBCPP_INLINE_VISIBILITY
1621constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1622
1623inline _LIBCPP_INLINE_VISIBILITY
1624constexpr bool operator==(monostate, monostate) noexcept { return true; }
1625
1626inline _LIBCPP_INLINE_VISIBILITY
1627constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1628
1629template <class... _Types>
1630inline _LIBCPP_INLINE_VISIBILITY
1631auto swap(variant<_Types...>& __lhs,
1632          variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1633    -> decltype(__lhs.swap(__rhs)) {
1634  __lhs.swap(__rhs);
1635}
1636
1637template <class... _Types>
1638struct _LIBCPP_TEMPLATE_VIS hash<
1639    __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1640  using argument_type = variant<_Types...>;
1641  using result_type = size_t;
1642
1643  inline _LIBCPP_INLINE_VISIBILITY
1644  result_type operator()(const argument_type& __v) const {
1645    using __variant_detail::__visitation::__variant;
1646    size_t __res =
1647        __v.valueless_by_exception()
1648               ? 299792458 // Random value chosen by the universe upon creation
1649               : __variant::__visit_alt(
1650                     [](const auto& __alt) {
1651                       using __alt_type = __uncvref_t<decltype(__alt)>;
1652                       using __value_type = remove_const_t<
1653                         typename __alt_type::__value_type>;
1654                       return hash<__value_type>{}(__alt.__value);
1655                     },
1656                     __v);
1657    return __hash_combine(__res, hash<size_t>{}(__v.index()));
1658  }
1659};
1660
1661template <>
1662struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
1663  using argument_type = monostate;
1664  using result_type = size_t;
1665
1666  inline _LIBCPP_INLINE_VISIBILITY
1667  result_type operator()(const argument_type&) const _NOEXCEPT {
1668    return 66740831; // return a fundamentally attractive random value.
1669  }
1670};
1671
1672#endif  // _LIBCPP_STD_VER > 14
1673
1674_LIBCPP_END_NAMESPACE_STD
1675
1676_LIBCPP_POP_MACROS
1677
1678#endif  // _LIBCPP_VARIANT
1679