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