xref: /freebsd/contrib/llvm-project/libcxx/include/variant (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_VARIANT
110b57cec5SDimitry Andric#define _LIBCPP_VARIANT
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric   variant synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std {
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric  // 20.7.2, class template variant
190b57cec5SDimitry Andric  template <class... Types>
200b57cec5SDimitry Andric  class variant {
210b57cec5SDimitry Andric  public:
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric    // 20.7.2.1, constructors
240b57cec5SDimitry Andric    constexpr variant() noexcept(see below);
25bdd1243dSDimitry Andric    constexpr variant(const variant&);
26bdd1243dSDimitry Andric    constexpr variant(variant&&) noexcept(see below);
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric    template <class T> constexpr variant(T&&) noexcept(see below);
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric    template <class T, class... Args>
310b57cec5SDimitry Andric    constexpr explicit variant(in_place_type_t<T>, Args&&...);
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric    template <class T, class U, class... Args>
340b57cec5SDimitry Andric    constexpr explicit variant(
350b57cec5SDimitry Andric        in_place_type_t<T>, initializer_list<U>, Args&&...);
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric    template <size_t I, class... Args>
380b57cec5SDimitry Andric    constexpr explicit variant(in_place_index_t<I>, Args&&...);
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric    template <size_t I, class U, class... Args>
410b57cec5SDimitry Andric    constexpr explicit variant(
420b57cec5SDimitry Andric        in_place_index_t<I>, initializer_list<U>, Args&&...);
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric    // 20.7.2.2, destructor
450b57cec5SDimitry Andric    ~variant();
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric    // 20.7.2.3, assignment
48bdd1243dSDimitry Andric    constexpr variant& operator=(const variant&);
49bdd1243dSDimitry Andric    constexpr variant& operator=(variant&&) noexcept(see below);
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric    template <class T> variant& operator=(T&&) noexcept(see below);
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric    // 20.7.2.4, modifiers
540b57cec5SDimitry Andric    template <class T, class... Args>
550b57cec5SDimitry Andric    T& emplace(Args&&...);
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric    template <class T, class U, class... Args>
580b57cec5SDimitry Andric    T& emplace(initializer_list<U>, Args&&...);
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric    template <size_t I, class... Args>
610b57cec5SDimitry Andric    variant_alternative_t<I, variant>& emplace(Args&&...);
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric    template <size_t I, class U, class...  Args>
640b57cec5SDimitry Andric    variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric    // 20.7.2.5, value status
670b57cec5SDimitry Andric    constexpr bool valueless_by_exception() const noexcept;
680b57cec5SDimitry Andric    constexpr size_t index() const noexcept;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric    // 20.7.2.6, swap
710b57cec5SDimitry Andric    void swap(variant&) noexcept(see below);
720b57cec5SDimitry Andric  };
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric  // 20.7.3, variant helper classes
750b57cec5SDimitry Andric  template <class T> struct variant_size; // undefined
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric  template <class T>
780b57cec5SDimitry Andric  inline constexpr size_t variant_size_v = variant_size<T>::value;
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric  template <class T> struct variant_size<const T>;
810b57cec5SDimitry Andric  template <class T> struct variant_size<volatile T>;
820b57cec5SDimitry Andric  template <class T> struct variant_size<const volatile T>;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric  template <class... Types>
850b57cec5SDimitry Andric  struct variant_size<variant<Types...>>;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative; // undefined
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric  template <size_t I, class T>
900b57cec5SDimitry Andric  using variant_alternative_t = typename variant_alternative<I, T>::type;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const T>;
930b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, volatile T>;
940b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const volatile T>;
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric  template <size_t I, class... Types>
970b57cec5SDimitry Andric  struct variant_alternative<I, variant<Types...>>;
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric  inline constexpr size_t variant_npos = -1;
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric  // 20.7.4, value access
1020b57cec5SDimitry Andric  template <class T, class... Types>
1030b57cec5SDimitry Andric  constexpr bool holds_alternative(const variant<Types...>&) noexcept;
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric  template <size_t I, class... Types>
1060b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&
1070b57cec5SDimitry Andric  get(variant<Types...>&);
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric  template <size_t I, class... Types>
1100b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&&
1110b57cec5SDimitry Andric  get(variant<Types...>&&);
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric  template <size_t I, class... Types>
1140b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&
1150b57cec5SDimitry Andric  get(const variant<Types...>&);
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric  template <size_t I, class... Types>
1180b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&&
1190b57cec5SDimitry Andric  get(const variant<Types...>&&);
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric  template <class T, class...  Types>
1220b57cec5SDimitry Andric  constexpr T& get(variant<Types...>&);
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric  template <class T, class... Types>
1250b57cec5SDimitry Andric  constexpr T&& get(variant<Types...>&&);
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric  template <class T, class... Types>
1280b57cec5SDimitry Andric  constexpr const T& get(const variant<Types...>&);
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric  template <class T, class... Types>
1310b57cec5SDimitry Andric  constexpr const T&& get(const variant<Types...>&&);
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric  template <size_t I, class... Types>
1340b57cec5SDimitry Andric  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
1350b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric  template <size_t I, class... Types>
1380b57cec5SDimitry Andric  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
1390b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric  template <class T, class... Types>
1420b57cec5SDimitry Andric  constexpr add_pointer_t<T>
1430b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric  template <class T, class... Types>
1460b57cec5SDimitry Andric  constexpr add_pointer_t<const T>
1470b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric  // 20.7.5, relational operators
1500b57cec5SDimitry Andric  template <class... Types>
1510b57cec5SDimitry Andric  constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric  template <class... Types>
1540b57cec5SDimitry Andric  constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric  template <class... Types>
1570b57cec5SDimitry Andric  constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric  template <class... Types>
1600b57cec5SDimitry Andric  constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric  template <class... Types>
1630b57cec5SDimitry Andric  constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric  template <class... Types>
1660b57cec5SDimitry Andric  constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
1670b57cec5SDimitry Andric
168bdd1243dSDimitry Andric  template <class... Types> requires (three_way_comparable<Types> && ...)
169bdd1243dSDimitry Andric  constexpr common_comparison_category_t<compare_three_way_result_t<Types>...>
170bdd1243dSDimitry Andric    operator<=>(const variant<Types...>&, const variant<Types...>&);           // since C++20
171bdd1243dSDimitry Andric
1720b57cec5SDimitry Andric  // 20.7.6, visitation
1730b57cec5SDimitry Andric  template <class Visitor, class... Variants>
1740b57cec5SDimitry Andric  constexpr see below visit(Visitor&&, Variants&&...);
1750b57cec5SDimitry Andric
176e8d8bef9SDimitry Andric  template <class R, class Visitor, class... Variants>
177e8d8bef9SDimitry Andric  constexpr R visit(Visitor&&, Variants&&...); // since C++20
178e8d8bef9SDimitry Andric
1790b57cec5SDimitry Andric  // 20.7.7, class monostate
1800b57cec5SDimitry Andric  struct monostate;
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric  // 20.7.8, monostate relational operators
1830b57cec5SDimitry Andric  constexpr bool operator==(monostate, monostate) noexcept;
184bdd1243dSDimitry Andric  constexpr bool operator!=(monostate, monostate) noexcept;             // until C++20
185bdd1243dSDimitry Andric  constexpr bool operator<(monostate, monostate) noexcept;              // until C++20
186bdd1243dSDimitry Andric  constexpr bool operator>(monostate, monostate) noexcept;              // until C++20
187bdd1243dSDimitry Andric  constexpr bool operator<=(monostate, monostate) noexcept;             // until C++20
188bdd1243dSDimitry Andric  constexpr bool operator>=(monostate, monostate) noexcept;             // until C++20
189bdd1243dSDimitry Andric  constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric  // 20.7.9, specialized algorithms
1920b57cec5SDimitry Andric  template <class... Types>
1930b57cec5SDimitry Andric  void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric  // 20.7.10, class bad_variant_access
1960b57cec5SDimitry Andric  class bad_variant_access;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric  // 20.7.11, hash support
1990b57cec5SDimitry Andric  template <class T> struct hash;
2000b57cec5SDimitry Andric  template <class... Types> struct hash<variant<Types...>>;
2010b57cec5SDimitry Andric  template <> struct hash<monostate>;
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric} // namespace std
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric*/
2060b57cec5SDimitry Andric
20781ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
208e8d8bef9SDimitry Andric#include <__availability>
209bdd1243dSDimitry Andric#include <__compare/common_comparison_category.h>
210bdd1243dSDimitry Andric#include <__compare/compare_three_way_result.h>
211bdd1243dSDimitry Andric#include <__compare/three_way_comparable.h>
212fe6060f1SDimitry Andric#include <__config>
21306c3fb27SDimitry Andric#include <__exception/exception.h>
214fe6060f1SDimitry Andric#include <__functional/hash.h>
215bdd1243dSDimitry Andric#include <__functional/invoke.h>
21681ad6265SDimitry Andric#include <__functional/operations.h>
21781ad6265SDimitry Andric#include <__functional/unary_function.h>
21806c3fb27SDimitry Andric#include <__memory/addressof.h>
219bdd1243dSDimitry Andric#include <__type_traits/add_const.h>
220bdd1243dSDimitry Andric#include <__type_traits/add_cv.h>
221bdd1243dSDimitry Andric#include <__type_traits/add_pointer.h>
222bdd1243dSDimitry Andric#include <__type_traits/add_volatile.h>
223bdd1243dSDimitry Andric#include <__type_traits/dependent_type.h>
224bdd1243dSDimitry Andric#include <__type_traits/is_array.h>
225bdd1243dSDimitry Andric#include <__type_traits/is_destructible.h>
226bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_move_constructible.h>
227bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_assignable.h>
228bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_constructible.h>
229bdd1243dSDimitry Andric#include <__type_traits/is_trivially_destructible.h>
230bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_assignable.h>
231bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_constructible.h>
232bdd1243dSDimitry Andric#include <__type_traits/is_void.h>
233bdd1243dSDimitry Andric#include <__type_traits/remove_const.h>
234bdd1243dSDimitry Andric#include <__type_traits/type_identity.h>
235bdd1243dSDimitry Andric#include <__type_traits/void_t.h>
23606c3fb27SDimitry Andric#include <__utility/declval.h>
237fe6060f1SDimitry Andric#include <__utility/forward.h>
23881ad6265SDimitry Andric#include <__utility/in_place.h>
23981ad6265SDimitry Andric#include <__utility/move.h>
24081ad6265SDimitry Andric#include <__utility/swap.h>
241fe6060f1SDimitry Andric#include <__variant/monostate.h>
24206c3fb27SDimitry Andric#include <__verbose_abort>
2430b57cec5SDimitry Andric#include <initializer_list>
244fe6060f1SDimitry Andric#include <limits>
2450b57cec5SDimitry Andric#include <new>
2460b57cec5SDimitry Andric#include <tuple>
2470b57cec5SDimitry Andric#include <version>
2480b57cec5SDimitry Andric
24981ad6265SDimitry Andric// standard-mandated includes
250bdd1243dSDimitry Andric
251bdd1243dSDimitry Andric// [variant.syn]
25281ad6265SDimitry Andric#include <compare>
25381ad6265SDimitry Andric
2540b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2550b57cec5SDimitry Andric#  pragma GCC system_header
2560b57cec5SDimitry Andric#endif
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
2590b57cec5SDimitry Andric#include <__undef_macros>
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andricnamespace std { // explicitly not using versioning namespace
2620b57cec5SDimitry Andric
26306c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
2640b57cec5SDimitry Andricpublic:
265bdd1243dSDimitry Andric  const char* what() const _NOEXCEPT override;
2660b57cec5SDimitry Andric};
2670b57cec5SDimitry Andric
2680b57cec5SDimitry Andric} // namespace std
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2710b57cec5SDimitry Andric
27206c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
2730b57cec5SDimitry Andric
274fe6060f1SDimitry Andric// Light N-dimensional array of function pointers. Used in place of std::array to avoid
275fe6060f1SDimitry Andric// adding a dependency.
276fe6060f1SDimitry Andrictemplate <class _Tp, size_t _Size>
277fe6060f1SDimitry Andricstruct __farray {
278fe6060f1SDimitry Andric  static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
279fe6060f1SDimitry Andric  _Tp __buf_[_Size] = {};
280fe6060f1SDimitry Andric
281*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __n) const noexcept { return __buf_[__n]; }
282fe6060f1SDimitry Andric};
283fe6060f1SDimitry Andric
284*cb14a3feSDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS void
285*cb14a3feSDimitry Andric__throw_bad_variant_access() {
28606c3fb27SDimitry Andric#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2870b57cec5SDimitry Andric  throw bad_variant_access();
2880b57cec5SDimitry Andric#  else
28906c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode");
2900b57cec5SDimitry Andric#  endif
2910b57cec5SDimitry Andric}
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andrictemplate <class... _Types>
2940b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant;
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andrictemplate <class _Tp>
2970b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size;
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andrictemplate <class _Tp>
300349cc55cSDimitry Andricinline constexpr size_t variant_size_v = variant_size<_Tp>::value;
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andrictemplate <class _Tp>
3030b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andrictemplate <class _Tp>
3060b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
3070b57cec5SDimitry Andric
3080b57cec5SDimitry Andrictemplate <class _Tp>
309*cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> : variant_size<_Tp> {};
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andrictemplate <class... _Types>
312*cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {};
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3150b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative;
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3180b57cec5SDimitry Andricusing variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
321*cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {};
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
324*cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
327*cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> : add_cv<variant_alternative_t<_Ip, _Tp>> {};
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
3300b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
3310b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
3320b57cec5SDimitry Andric  using type = __type_pack_element<_Ip, _Types...>;
3330b57cec5SDimitry Andric};
3340b57cec5SDimitry Andric
335349cc55cSDimitry Andricinline constexpr size_t variant_npos = static_cast<size_t>(-1);
3360b57cec5SDimitry Andric
337bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int __choose_index_type(unsigned int __num_elem) {
338e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned char>::max())
3390b57cec5SDimitry Andric    return 0;
340e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned short>::max())
3410b57cec5SDimitry Andric    return 1;
3420b57cec5SDimitry Andric  return 2;
3430b57cec5SDimitry Andric}
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andrictemplate <size_t _NumAlts>
3460b57cec5SDimitry Andricusing __variant_index_t =
3470b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
3480b57cec5SDimitry Andric    unsigned int;
3490b57cec5SDimitry Andric#  else
350*cb14a3feSDimitry Andric    std::tuple_element_t< __choose_index_type(_NumAlts), std::tuple<unsigned char, unsigned short, unsigned int> >;
3510b57cec5SDimitry Andric#  endif
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andrictemplate <class _IndexType>
3540b57cec5SDimitry Andricconstexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
3550b57cec5SDimitry Andric
356fe6060f1SDimitry Andrictemplate <class... _Types>
357fe6060f1SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant;
358fe6060f1SDimitry Andric
359fe6060f1SDimitry Andrictemplate <class... _Types>
360*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept {
361fe6060f1SDimitry Andric  return __vs;
362fe6060f1SDimitry Andric}
363fe6060f1SDimitry Andric
364fe6060f1SDimitry Andrictemplate <class... _Types>
365*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& __as_variant(const variant<_Types...>& __vs) noexcept {
366fe6060f1SDimitry Andric  return __vs;
367fe6060f1SDimitry Andric}
368fe6060f1SDimitry Andric
369fe6060f1SDimitry Andrictemplate <class... _Types>
370*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& __as_variant(variant<_Types...>&& __vs) noexcept {
3715f757f3fSDimitry Andric  return std::move(__vs);
372fe6060f1SDimitry Andric}
373fe6060f1SDimitry Andric
374fe6060f1SDimitry Andrictemplate <class... _Types>
375*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& __as_variant(const variant<_Types...>&& __vs) noexcept {
3765f757f3fSDimitry Andric  return std::move(__vs);
377fe6060f1SDimitry Andric}
378fe6060f1SDimitry Andric
3790b57cec5SDimitry Andricnamespace __find_detail {
3800b57cec5SDimitry Andric
3810b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
382*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr size_t __find_index() {
3830b57cec5SDimitry Andric  constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
3840b57cec5SDimitry Andric  size_t __result            = __not_found;
3850b57cec5SDimitry Andric  for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
3860b57cec5SDimitry Andric    if (__matches[__i]) {
3870b57cec5SDimitry Andric      if (__result != __not_found) {
3880b57cec5SDimitry Andric        return __ambiguous;
3890b57cec5SDimitry Andric      }
3900b57cec5SDimitry Andric      __result = __i;
3910b57cec5SDimitry Andric    }
3920b57cec5SDimitry Andric  }
3930b57cec5SDimitry Andric  return __result;
3940b57cec5SDimitry Andric}
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andrictemplate <size_t _Index>
397*cb14a3feSDimitry Andricstruct __find_unambiguous_index_sfinae_impl : integral_constant<size_t, _Index> {};
3980b57cec5SDimitry Andric
3990b57cec5SDimitry Andrictemplate <>
4000b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__not_found> {};
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andrictemplate <>
4030b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
4040b57cec5SDimitry Andric
4050b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
406*cb14a3feSDimitry Andricstruct __find_unambiguous_index_sfinae : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
4070b57cec5SDimitry Andric
4080b57cec5SDimitry Andric} // namespace __find_detail
4090b57cec5SDimitry Andric
4100b57cec5SDimitry Andricnamespace __variant_detail {
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andricstruct __valueless_t {};
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andricenum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
4150b57cec5SDimitry Andric
416*cb14a3feSDimitry Andrictemplate <typename _Tp, template <typename> class _IsTriviallyAvailable, template <typename> class _IsAvailable>
4170b57cec5SDimitry Andricconstexpr _Trait __trait =
418*cb14a3feSDimitry Andric    _IsTriviallyAvailable<_Tp>::value ? _Trait::_TriviallyAvailable
419*cb14a3feSDimitry Andric    : _IsAvailable<_Tp>::value
420*cb14a3feSDimitry Andric        ? _Trait::_Available
421*cb14a3feSDimitry Andric        : _Trait::_Unavailable;
4220b57cec5SDimitry Andric
423*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
4240b57cec5SDimitry Andric  _Trait __result = _Trait::_TriviallyAvailable;
4250b57cec5SDimitry Andric  for (_Trait __t : __traits) {
4260b57cec5SDimitry Andric    if (static_cast<int>(__t) > static_cast<int>(__result)) {
4270b57cec5SDimitry Andric      __result = __t;
4280b57cec5SDimitry Andric    }
4290b57cec5SDimitry Andric  }
4300b57cec5SDimitry Andric  return __result;
4310b57cec5SDimitry Andric}
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andrictemplate <typename... _Types>
4340b57cec5SDimitry Andricstruct __traits {
4350b57cec5SDimitry Andric  static constexpr _Trait __copy_constructible_trait =
436*cb14a3feSDimitry Andric      __variant_detail::__common_trait({__trait<_Types, is_trivially_copy_constructible, is_copy_constructible>...});
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric  static constexpr _Trait __move_constructible_trait =
439*cb14a3feSDimitry Andric      __variant_detail::__common_trait({__trait<_Types, is_trivially_move_constructible, is_move_constructible>...});
4400b57cec5SDimitry Andric
441bdd1243dSDimitry Andric  static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
442*cb14a3feSDimitry Andric      {__copy_constructible_trait, __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
4430b57cec5SDimitry Andric
444bdd1243dSDimitry Andric  static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
445*cb14a3feSDimitry Andric      {__move_constructible_trait, __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
4460b57cec5SDimitry Andric
447*cb14a3feSDimitry Andric  static constexpr _Trait __destructible_trait =
448*cb14a3feSDimitry Andric      __variant_detail::__common_trait({__trait<_Types, is_trivially_destructible, is_destructible>...});
4490b57cec5SDimitry Andric};
4500b57cec5SDimitry Andric
4510b57cec5SDimitry Andricnamespace __access {
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andricstruct __union {
4540b57cec5SDimitry Andric  template <class _Vp>
455*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
4565f757f3fSDimitry Andric    return std::forward<_Vp>(__v).__head;
4570b57cec5SDimitry Andric  }
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andric  template <class _Vp, size_t _Ip>
460*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
4615f757f3fSDimitry Andric    return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
4620b57cec5SDimitry Andric  }
4630b57cec5SDimitry Andric};
4640b57cec5SDimitry Andric
4650b57cec5SDimitry Andricstruct __base {
4660b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
467*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
468*cb14a3feSDimitry Andric    return __union::__get_alt(std::forward<_Vp>(__v).__data, in_place_index<_Ip>);
4690b57cec5SDimitry Andric  }
4700b57cec5SDimitry Andric};
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andricstruct __variant {
4730b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
474*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
4755f757f3fSDimitry Andric    return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_);
4760b57cec5SDimitry Andric  }
4770b57cec5SDimitry Andric};
4780b57cec5SDimitry Andric
4790b57cec5SDimitry Andric} // namespace __access
4800b57cec5SDimitry Andric
4810b57cec5SDimitry Andricnamespace __visitation {
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andricstruct __base {
4840b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
485*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
4860b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
487*cb14a3feSDimitry Andric    constexpr auto __fdiagonal = __make_fdiagonal<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
488*cb14a3feSDimitry Andric    return __fdiagonal[__index](std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
4890b57cec5SDimitry Andric  }
4900b57cec5SDimitry Andric
4910b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
492*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
493*cb14a3feSDimitry Andric    constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
494*cb14a3feSDimitry Andric    return __at(__fmatrix, __vs.index()...)(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
4950b57cec5SDimitry Andric  }
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andricprivate:
4980b57cec5SDimitry Andric  template <class _Tp>
499*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr const _Tp& __at(const _Tp& __elem) {
500*cb14a3feSDimitry Andric    return __elem;
501*cb14a3feSDimitry Andric  }
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andric  template <class _Tp, size_t _Np, typename... _Indices>
504*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&&
505*cb14a3feSDimitry Andric  __at(const __farray<_Tp, _Np>& __elems, size_t __index, _Indices... __indices) {
5060b57cec5SDimitry Andric    return __at(__elems[__index], __indices...);
5070b57cec5SDimitry Andric  }
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric  template <class _Fp, class... _Fs>
51006c3fb27SDimitry Andric  static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() {
5110b57cec5SDimitry Andric    static_assert(
512*cb14a3feSDimitry Andric        __all<is_same_v<_Fp, _Fs>...>::value, "`std::visit` requires the visitor to have a single return type.");
5130b57cec5SDimitry Andric  }
5140b57cec5SDimitry Andric
5150b57cec5SDimitry Andric  template <class... _Fs>
516*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_farray(_Fs&&... __fs) {
517bdd1243dSDimitry Andric    __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
518bdd1243dSDimitry Andric    using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
5195f757f3fSDimitry Andric    return __result{{std::forward<_Fs>(__fs)...}};
5200b57cec5SDimitry Andric  }
5210b57cec5SDimitry Andric
522e8d8bef9SDimitry Andric  template <size_t... _Is>
5230b57cec5SDimitry Andric  struct __dispatcher {
5240b57cec5SDimitry Andric    template <class _Fp, class... _Vs>
525*cb14a3feSDimitry Andric    _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
526*cb14a3feSDimitry Andric      return std::__invoke(static_cast<_Fp>(__f), __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
5270b57cec5SDimitry Andric    }
5280b57cec5SDimitry Andric  };
5290b57cec5SDimitry Andric
5300b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
531*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_dispatch(index_sequence<_Is...>) {
5320b57cec5SDimitry Andric    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
5330b57cec5SDimitry Andric  }
5340b57cec5SDimitry Andric
5350b57cec5SDimitry Andric  template <size_t _Ip, class _Fp, class... _Vs>
536*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl() {
537*cb14a3feSDimitry Andric    return __make_dispatch<_Fp, _Vs...>(index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
5380b57cec5SDimitry Andric  }
5390b57cec5SDimitry Andric
5400b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
541*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
5420b57cec5SDimitry Andric    return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
5430b57cec5SDimitry Andric  }
5440b57cec5SDimitry Andric
5450b57cec5SDimitry Andric  template <class _Fp, class _Vp, class... _Vs>
546*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal() {
54706c3fb27SDimitry Andric    constexpr size_t __np = __remove_cvref_t<_Vp>::__size();
54806c3fb27SDimitry Andric    static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value);
54906c3fb27SDimitry Andric    return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{});
5500b57cec5SDimitry Andric  }
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
553*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
5540b57cec5SDimitry Andric    return __make_dispatch<_Fp, _Vs...>(__is);
5550b57cec5SDimitry Andric  }
5560b57cec5SDimitry Andric
5570b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
558*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto
559*cb14a3feSDimitry Andric  __make_fmatrix_impl(index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) {
560*cb14a3feSDimitry Andric    return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(index_sequence<_Is..., _Js>{}, __ls...)...);
5610b57cec5SDimitry Andric  }
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andric  template <class _Fp, class... _Vs>
564*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix() {
5650b57cec5SDimitry Andric    return __make_fmatrix_impl<_Fp, _Vs...>(
566bdd1243dSDimitry Andric        index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
5670b57cec5SDimitry Andric  }
5680b57cec5SDimitry Andric};
5690b57cec5SDimitry Andric
5700b57cec5SDimitry Andricstruct __variant {
5710b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
572*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
5730b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
574*cb14a3feSDimitry Andric    return __base::__visit_alt_at(__index, std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__impl_...);
5750b57cec5SDimitry Andric  }
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
578*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
579fe6060f1SDimitry Andric    return __base::__visit_alt(
580*cb14a3feSDimitry Andric        std::forward<_Visitor>(__visitor), std::__as_variant(std::forward<_Vs>(__vs)).__impl_...);
5810b57cec5SDimitry Andric  }
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
584*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
5850b57cec5SDimitry Andric  __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
586*cb14a3feSDimitry Andric    return __visit_alt_at(__index, __make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
5870b57cec5SDimitry Andric  }
5880b57cec5SDimitry Andric
5890b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
590*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
591*cb14a3feSDimitry Andric    return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
5920b57cec5SDimitry Andric  }
593fe6060f1SDimitry Andric
59406c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
595e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor, class... _Vs>
596*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr _Rp __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
597*cb14a3feSDimitry Andric    return __visit_alt(__make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
598e8d8bef9SDimitry Andric  }
599e8d8bef9SDimitry Andric#  endif
6000b57cec5SDimitry Andric
6010b57cec5SDimitry Andricprivate:
6020b57cec5SDimitry Andric  template <class _Visitor, class... _Values>
60306c3fb27SDimitry Andric  static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() {
604*cb14a3feSDimitry Andric    static_assert(is_invocable_v<_Visitor, _Values...>, "`std::visit` requires the visitor to be exhaustive.");
6050b57cec5SDimitry Andric  }
6060b57cec5SDimitry Andric
6070b57cec5SDimitry Andric  template <class _Visitor>
6080b57cec5SDimitry Andric  struct __value_visitor {
6090b57cec5SDimitry Andric    template <class... _Alts>
610*cb14a3feSDimitry Andric    _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Alts&&... __alts) const {
611*cb14a3feSDimitry Andric      __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
612*cb14a3feSDimitry Andric      return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
6130b57cec5SDimitry Andric    }
6140b57cec5SDimitry Andric    _Visitor&& __visitor;
6150b57cec5SDimitry Andric  };
6160b57cec5SDimitry Andric
61706c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
618e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
619e8d8bef9SDimitry Andric  struct __value_visitor_return_type {
620e8d8bef9SDimitry Andric    template <class... _Alts>
621*cb14a3feSDimitry Andric    _LIBCPP_HIDE_FROM_ABI constexpr _Rp operator()(_Alts&&... __alts) const {
622*cb14a3feSDimitry Andric      __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
623e8d8bef9SDimitry Andric      if constexpr (is_void_v<_Rp>) {
624*cb14a3feSDimitry Andric        std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
625*cb14a3feSDimitry Andric      } else {
626*cb14a3feSDimitry Andric        return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
627e8d8bef9SDimitry Andric      }
628e8d8bef9SDimitry Andric    }
629e8d8bef9SDimitry Andric
630e8d8bef9SDimitry Andric    _Visitor&& __visitor;
631e8d8bef9SDimitry Andric  };
632e8d8bef9SDimitry Andric#  endif
633e8d8bef9SDimitry Andric
6340b57cec5SDimitry Andric  template <class _Visitor>
635*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
6365f757f3fSDimitry Andric    return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)};
6370b57cec5SDimitry Andric  }
638e8d8bef9SDimitry Andric
63906c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
640e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
641*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
6425f757f3fSDimitry Andric    return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)};
643e8d8bef9SDimitry Andric  }
644e8d8bef9SDimitry Andric#  endif
6450b57cec5SDimitry Andric};
6460b57cec5SDimitry Andric
6470b57cec5SDimitry Andric} // namespace __visitation
6480b57cec5SDimitry Andric
6490b57cec5SDimitry Andrictemplate <size_t _Index, class _Tp>
6500b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __alt {
6510b57cec5SDimitry Andric  using __value_type = _Tp;
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andric  template <class... _Args>
654*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args)
6555f757f3fSDimitry Andric      : __value(std::forward<_Args>(__args)...) {}
6560b57cec5SDimitry Andric
6570b57cec5SDimitry Andric  __value_type __value;
6580b57cec5SDimitry Andric};
6590b57cec5SDimitry Andric
6600b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index, class... _Types>
6610b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union;
6620b57cec5SDimitry Andric
6630b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index>
6640b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
6650b57cec5SDimitry Andric
6660b57cec5SDimitry Andric#  define _LIBCPP_VARIANT_UNION(destructible_trait, destructor)                                                        \
6670b57cec5SDimitry Andric    template <size_t _Index, class _Tp, class... _Types>                                                               \
668*cb14a3feSDimitry Andric    union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, _Index, _Tp, _Types...> {                                   \
6690b57cec5SDimitry Andric    public:                                                                                                            \
670*cb14a3feSDimitry Andric      _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(__valueless_t) noexcept : __dummy{} {}                          \
6710b57cec5SDimitry Andric                                                                                                                       \
6720b57cec5SDimitry Andric      template <class... _Args>                                                                                        \
673*cb14a3feSDimitry Andric      _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<0>, _Args&&... __args)                         \
6745f757f3fSDimitry Andric          : __head(in_place, std::forward<_Args>(__args)...) {}                                                        \
6750b57cec5SDimitry Andric                                                                                                                       \
6760b57cec5SDimitry Andric      template <size_t _Ip, class... _Args>                                                                            \
677*cb14a3feSDimitry Andric      _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args)                       \
6785f757f3fSDimitry Andric          : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {}                                         \
6790b57cec5SDimitry Andric                                                                                                                       \
6800b57cec5SDimitry Andric      __union(const __union&) = default;                                                                               \
6810b57cec5SDimitry Andric      __union(__union&&)      = default;                                                                               \
6820b57cec5SDimitry Andric                                                                                                                       \
6830b57cec5SDimitry Andric      destructor                                                                                                       \
6840b57cec5SDimitry Andric                                                                                                                       \
685*cb14a3feSDimitry Andric          __union&                                                                                                     \
686*cb14a3feSDimitry Andric          operator=(const __union&) = default;                                                                         \
6870b57cec5SDimitry Andric      __union& operator=(__union&&) = default;                                                                         \
6880b57cec5SDimitry Andric                                                                                                                       \
6890b57cec5SDimitry Andric    private:                                                                                                           \
6900b57cec5SDimitry Andric      char __dummy;                                                                                                    \
6910b57cec5SDimitry Andric      __alt<_Index, _Tp> __head;                                                                                       \
6920b57cec5SDimitry Andric      __union<destructible_trait, _Index + 1, _Types...> __tail;                                                       \
6930b57cec5SDimitry Andric                                                                                                                       \
6940b57cec5SDimitry Andric      friend struct __access::__union;                                                                                 \
6950b57cec5SDimitry Andric    }
6960b57cec5SDimitry Andric
6970b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
6980b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union(){});
6990b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
7000b57cec5SDimitry Andric
7010b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_UNION
7020b57cec5SDimitry Andric
7030b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, class... _Types>
7040b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __base {
7050b57cec5SDimitry Andricpublic:
7060b57cec5SDimitry Andric  using __index_t = __variant_index_t<sizeof...(_Types)>;
7070b57cec5SDimitry Andric
708*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(__valueless_t __tag) noexcept
709753f127fSDimitry Andric      : __data(__tag), __index(__variant_npos<__index_t>) {}
7100b57cec5SDimitry Andric
7110b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
712*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
713*cb14a3feSDimitry Andric      : __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), __index(_Ip) {}
7140b57cec5SDimitry Andric
715*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { return index() == variant_npos; }
7160b57cec5SDimitry Andric
717*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept {
7180b57cec5SDimitry Andric    return __index == __variant_npos<__index_t> ? variant_npos : __index;
7190b57cec5SDimitry Andric  }
7200b57cec5SDimitry Andric
7210b57cec5SDimitry Andricprotected:
722*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() & { return *this; }
7230b57cec5SDimitry Andric
724*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() && { return std::move(*this); }
7250b57cec5SDimitry Andric
726*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const& { return *this; }
7270b57cec5SDimitry Andric
728*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const&& { return std::move(*this); }
7290b57cec5SDimitry Andric
730*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Types); }
7310b57cec5SDimitry Andric
7320b57cec5SDimitry Andric  __union<_DestructibleTrait, 0, _Types...> __data;
7330b57cec5SDimitry Andric  __index_t __index;
7340b57cec5SDimitry Andric
7350b57cec5SDimitry Andric  friend struct __access::__base;
7360b57cec5SDimitry Andric  friend struct __visitation::__base;
7370b57cec5SDimitry Andric};
7380b57cec5SDimitry Andric
7390b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__destructible_trait>
740e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __dtor;
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric#  define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy)                                          \
7430b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
744*cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, destructible_trait>                                         \
7450b57cec5SDimitry Andric        : public __base<destructible_trait, _Types...> {                                                               \
7460b57cec5SDimitry Andric      using __base_type = __base<destructible_trait, _Types...>;                                                       \
7470b57cec5SDimitry Andric      using __index_t   = typename __base_type::__index_t;                                                             \
7480b57cec5SDimitry Andric                                                                                                                       \
7490b57cec5SDimitry Andric    public:                                                                                                            \
7500b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
7510b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
7520b57cec5SDimitry Andric                                                                                                                       \
753e8d8bef9SDimitry Andric      __dtor(const __dtor&)                       = default;                                                           \
754e8d8bef9SDimitry Andric      __dtor(__dtor&&)                            = default;                                                           \
755*cb14a3feSDimitry Andric      destructor __dtor& operator=(const __dtor&) = default;                                                           \
756e8d8bef9SDimitry Andric      __dtor& operator=(__dtor&&)                 = default;                                                           \
7570b57cec5SDimitry Andric                                                                                                                       \
7580b57cec5SDimitry Andric    protected:                                                                                                         \
759*cb14a3feSDimitry Andric      inline _LIBCPP_HIDE_FROM_ABI destroy                                                                             \
7600b57cec5SDimitry Andric    }
7610b57cec5SDimitry Andric
7620b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
763*cb14a3feSDimitry Andric    _Trait::_TriviallyAvailable, ~__dtor() = default;
764*cb14a3feSDimitry Andric    , void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
7650b57cec5SDimitry Andric
7660b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
7670b57cec5SDimitry Andric    _Trait::_Available,
768e8d8bef9SDimitry Andric    ~__dtor() { __destroy(); },
7690b57cec5SDimitry Andric    void __destroy() noexcept {
7700b57cec5SDimitry Andric      if (!this->valueless_by_exception()) {
7710b57cec5SDimitry Andric        __visitation::__base::__visit_alt(
7720b57cec5SDimitry Andric            [](auto& __alt) noexcept {
773bdd1243dSDimitry Andric              using __alt_type = __remove_cvref_t<decltype(__alt)>;
7740b57cec5SDimitry Andric              __alt.~__alt_type();
7750b57cec5SDimitry Andric            },
7760b57cec5SDimitry Andric            *this);
7770b57cec5SDimitry Andric      }
7780b57cec5SDimitry Andric      this->__index = __variant_npos<__index_t>;
7790b57cec5SDimitry Andric    });
7800b57cec5SDimitry Andric
781*cb14a3feSDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable, ~__dtor() = delete;, void __destroy() noexcept = delete;);
7820b57cec5SDimitry Andric
7830b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_DESTRUCTOR
7840b57cec5SDimitry Andric
7850b57cec5SDimitry Andrictemplate <class _Traits>
786e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
787e8d8bef9SDimitry Andric  using __base_type = __dtor<_Traits>;
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andricpublic:
7900b57cec5SDimitry Andric  using __base_type::__base_type;
7910b57cec5SDimitry Andric  using __base_type::operator=;
7920b57cec5SDimitry Andric
7930b57cec5SDimitry Andricprotected:
7940b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class... _Args>
795*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
796*cb14a3feSDimitry Andric    ::new ((void*)std::addressof(__a)) __alt<_Ip, _Tp>(in_place, std::forward<_Args>(__args)...);
7970b57cec5SDimitry Andric    return __a.__value;
7980b57cec5SDimitry Andric  }
7990b57cec5SDimitry Andric
8000b57cec5SDimitry Andric  template <class _Rhs>
801*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
8020b57cec5SDimitry Andric    __lhs.__destroy();
8030b57cec5SDimitry Andric    if (!__rhs.valueless_by_exception()) {
8040b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
8050b57cec5SDimitry Andric          __rhs.index(),
8060b57cec5SDimitry Andric          [](auto& __lhs_alt, auto&& __rhs_alt) {
807*cb14a3feSDimitry Andric            __construct_alt(__lhs_alt, std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
8080b57cec5SDimitry Andric          },
809*cb14a3feSDimitry Andric          __lhs,
810*cb14a3feSDimitry Andric          std::forward<_Rhs>(__rhs));
8110b57cec5SDimitry Andric      __lhs.__index = __rhs.index();
8120b57cec5SDimitry Andric    }
8130b57cec5SDimitry Andric  }
8140b57cec5SDimitry Andric};
8150b57cec5SDimitry Andric
8160b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_constructible_trait>
8170b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_constructor;
8180b57cec5SDimitry Andric
819*cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, move_constructor)                                 \
8200b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
821*cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, move_constructible_trait>                       \
822e8d8bef9SDimitry Andric        : public __ctor<__traits<_Types...>> {                                                                         \
823e8d8bef9SDimitry Andric      using __base_type = __ctor<__traits<_Types...>>;                                                                 \
8240b57cec5SDimitry Andric                                                                                                                       \
8250b57cec5SDimitry Andric    public:                                                                                                            \
8260b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
8270b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
8280b57cec5SDimitry Andric                                                                                                                       \
8290b57cec5SDimitry Andric      __move_constructor(const __move_constructor&)            = default;                                              \
830*cb14a3feSDimitry Andric      move_constructor ~__move_constructor()                   = default;                                              \
8310b57cec5SDimitry Andric      __move_constructor& operator=(const __move_constructor&) = default;                                              \
8320b57cec5SDimitry Andric      __move_constructor& operator=(__move_constructor&&)      = default;                                              \
8330b57cec5SDimitry Andric    }
8340b57cec5SDimitry Andric
835*cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_TriviallyAvailable,
8360b57cec5SDimitry Andric                                 __move_constructor(__move_constructor&& __that) = default;);
8370b57cec5SDimitry Andric
8380b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
8390b57cec5SDimitry Andric    _Trait::_Available,
840*cb14a3feSDimitry Andric    __move_constructor(__move_constructor&& __that) noexcept(__all<is_nothrow_move_constructible_v<_Types>...>::value)
841*cb14a3feSDimitry Andric    : __move_constructor(__valueless_t{}) { this->__generic_construct(*this, std::move(__that)); });
8420b57cec5SDimitry Andric
843*cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_Unavailable, __move_constructor(__move_constructor&&) = delete;);
8440b57cec5SDimitry Andric
8450b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
8460b57cec5SDimitry Andric
8470b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_constructible_trait>
8480b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_constructor;
8490b57cec5SDimitry Andric
850*cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, copy_constructor)                                 \
8510b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
852*cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, copy_constructible_trait>                       \
8530b57cec5SDimitry Andric        : public __move_constructor<__traits<_Types...>> {                                                             \
8540b57cec5SDimitry Andric      using __base_type = __move_constructor<__traits<_Types...>>;                                                     \
8550b57cec5SDimitry Andric                                                                                                                       \
8560b57cec5SDimitry Andric    public:                                                                                                            \
8570b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
8580b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
8590b57cec5SDimitry Andric                                                                                                                       \
860*cb14a3feSDimitry Andric      copy_constructor __copy_constructor(__copy_constructor&&) = default;                                             \
8610b57cec5SDimitry Andric      ~__copy_constructor()                                     = default;                                             \
8620b57cec5SDimitry Andric      __copy_constructor& operator=(const __copy_constructor&)  = default;                                             \
8630b57cec5SDimitry Andric      __copy_constructor& operator=(__copy_constructor&&)       = default;                                             \
8640b57cec5SDimitry Andric    }
8650b57cec5SDimitry Andric
866*cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_TriviallyAvailable,
8670b57cec5SDimitry Andric                                 __copy_constructor(const __copy_constructor& __that) = default;);
8680b57cec5SDimitry Andric
8690b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
870*cb14a3feSDimitry Andric    _Trait::_Available, __copy_constructor(const __copy_constructor& __that)
871*cb14a3feSDimitry Andric    : __copy_constructor(__valueless_t{}) { this->__generic_construct(*this, __that); });
8720b57cec5SDimitry Andric
873*cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_Unavailable, __copy_constructor(const __copy_constructor&) = delete;);
8740b57cec5SDimitry Andric
8750b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
8760b57cec5SDimitry Andric
8770b57cec5SDimitry Andrictemplate <class _Traits>
8780b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
8790b57cec5SDimitry Andric  using __base_type = __copy_constructor<_Traits>;
8800b57cec5SDimitry Andric
8810b57cec5SDimitry Andricpublic:
8820b57cec5SDimitry Andric  using __base_type::__base_type;
8830b57cec5SDimitry Andric  using __base_type::operator=;
8840b57cec5SDimitry Andric
8850b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
886*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI auto& __emplace(_Args&&... __args) {
8870b57cec5SDimitry Andric    this->__destroy();
888*cb14a3feSDimitry Andric    auto& __res   = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Args>(__args)...);
8890b57cec5SDimitry Andric    this->__index = _Ip;
8900b57cec5SDimitry Andric    return __res;
8910b57cec5SDimitry Andric  }
8920b57cec5SDimitry Andric
8930b57cec5SDimitry Andricprotected:
8940b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class _Arg>
895*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
8960b57cec5SDimitry Andric    if (this->index() == _Ip) {
8975f757f3fSDimitry Andric      __a.__value = std::forward<_Arg>(__arg);
8980b57cec5SDimitry Andric    } else {
8990b57cec5SDimitry Andric      struct {
900*cb14a3feSDimitry Andric        _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const { __this->__emplace<_Ip>(std::forward<_Arg>(__arg)); }
90106c3fb27SDimitry Andric        _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const {
9025f757f3fSDimitry Andric          __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
9030b57cec5SDimitry Andric        }
9040b57cec5SDimitry Andric        __assignment* __this;
9050b57cec5SDimitry Andric        _Arg&& __arg;
9065f757f3fSDimitry Andric      } __impl{this, std::forward<_Arg>(__arg)};
907*cb14a3feSDimitry Andric      __impl(bool_constant < is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v < _Tp >> {});
9080b57cec5SDimitry Andric    }
9090b57cec5SDimitry Andric  }
9100b57cec5SDimitry Andric
9110b57cec5SDimitry Andric  template <class _That>
912*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __generic_assign(_That&& __that) {
9130b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
9140b57cec5SDimitry Andric      // do nothing.
9150b57cec5SDimitry Andric    } else if (__that.valueless_by_exception()) {
9160b57cec5SDimitry Andric      this->__destroy();
9170b57cec5SDimitry Andric    } else {
9180b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
9190b57cec5SDimitry Andric          __that.index(),
9200b57cec5SDimitry Andric          [this](auto& __this_alt, auto&& __that_alt) {
921*cb14a3feSDimitry Andric            this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value);
9220b57cec5SDimitry Andric          },
923*cb14a3feSDimitry Andric          *this,
924*cb14a3feSDimitry Andric          std::forward<_That>(__that));
9250b57cec5SDimitry Andric    }
9260b57cec5SDimitry Andric  }
9270b57cec5SDimitry Andric};
9280b57cec5SDimitry Andric
9290b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_assignable_trait>
9300b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_assignment;
9310b57cec5SDimitry Andric
932*cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment)                                      \
9330b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
934*cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, move_assignable_trait>                           \
9350b57cec5SDimitry Andric        : public __assignment<__traits<_Types...>> {                                                                   \
9360b57cec5SDimitry Andric      using __base_type = __assignment<__traits<_Types...>>;                                                           \
9370b57cec5SDimitry Andric                                                                                                                       \
9380b57cec5SDimitry Andric    public:                                                                                                            \
9390b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
9400b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
9410b57cec5SDimitry Andric                                                                                                                       \
9420b57cec5SDimitry Andric      __move_assignment(const __move_assignment&)            = default;                                                \
9430b57cec5SDimitry Andric      __move_assignment(__move_assignment&&)                 = default;                                                \
9440b57cec5SDimitry Andric      ~__move_assignment()                                   = default;                                                \
9450b57cec5SDimitry Andric      __move_assignment& operator=(const __move_assignment&) = default;                                                \
9460b57cec5SDimitry Andric      move_assignment                                                                                                  \
9470b57cec5SDimitry Andric    }
9480b57cec5SDimitry Andric
949*cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable,
9500b57cec5SDimitry Andric                                __move_assignment& operator=(__move_assignment&& __that) = default;);
9510b57cec5SDimitry Andric
9520b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
9530b57cec5SDimitry Andric    _Trait::_Available,
954*cb14a3feSDimitry Andric    __move_assignment&
955*cb14a3feSDimitry Andric    operator=(__move_assignment&& __that) noexcept(
956*cb14a3feSDimitry Andric        __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) {
9575f757f3fSDimitry Andric      this->__generic_assign(std::move(__that));
9580b57cec5SDimitry Andric      return *this;
9590b57cec5SDimitry Andric    });
9600b57cec5SDimitry Andric
961*cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_Unavailable, __move_assignment& operator=(__move_assignment&&) = delete;);
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
9640b57cec5SDimitry Andric
9650b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_assignable_trait>
9660b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_assignment;
9670b57cec5SDimitry Andric
968*cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment)                                      \
9690b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
970*cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, copy_assignable_trait>                           \
9710b57cec5SDimitry Andric        : public __move_assignment<__traits<_Types...>> {                                                              \
9720b57cec5SDimitry Andric      using __base_type = __move_assignment<__traits<_Types...>>;                                                      \
9730b57cec5SDimitry Andric                                                                                                                       \
9740b57cec5SDimitry Andric    public:                                                                                                            \
9750b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
9760b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
9770b57cec5SDimitry Andric                                                                                                                       \
9780b57cec5SDimitry Andric      __copy_assignment(const __copy_assignment&)                       = default;                                     \
9790b57cec5SDimitry Andric      __copy_assignment(__copy_assignment&&)                            = default;                                     \
9800b57cec5SDimitry Andric      ~__copy_assignment()                                              = default;                                     \
981*cb14a3feSDimitry Andric      copy_assignment __copy_assignment& operator=(__copy_assignment&&) = default;                                     \
9820b57cec5SDimitry Andric    }
9830b57cec5SDimitry Andric
984*cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable,
9850b57cec5SDimitry Andric                                __copy_assignment& operator=(const __copy_assignment& __that) = default;);
9860b57cec5SDimitry Andric
9870b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
988*cb14a3feSDimitry Andric    _Trait::_Available, __copy_assignment& operator=(const __copy_assignment& __that) {
9890b57cec5SDimitry Andric      this->__generic_assign(__that);
9900b57cec5SDimitry Andric      return *this;
9910b57cec5SDimitry Andric    });
9920b57cec5SDimitry Andric
993*cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable, __copy_assignment& operator=(const __copy_assignment&) = delete;);
9940b57cec5SDimitry Andric
9950b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
9960b57cec5SDimitry Andric
9970b57cec5SDimitry Andrictemplate <class... _Types>
998*cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __impl : public __copy_assignment<__traits<_Types...>> {
9990b57cec5SDimitry Andric  using __base_type = __copy_assignment<__traits<_Types...>>;
10000b57cec5SDimitry Andric
10010b57cec5SDimitry Andricpublic:
100281ad6265SDimitry Andric  using __base_type::__base_type; // get in_place_index_t constructor & friends
100306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl(__impl const&)            = default;
100406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl(__impl&&)                 = default;
100506c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
100606c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&)      = default;
10070b57cec5SDimitry Andric
10080b57cec5SDimitry Andric  template <size_t _Ip, class _Arg>
1009*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __assign(_Arg&& __arg) {
1010*cb14a3feSDimitry Andric    this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg));
10110b57cec5SDimitry Andric  }
10120b57cec5SDimitry Andric
1013*cb14a3feSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI void __swap(__impl& __that) {
10140b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
10150b57cec5SDimitry Andric      // do nothing.
10160b57cec5SDimitry Andric    } else if (this->index() == __that.index()) {
10170b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
10180b57cec5SDimitry Andric          this->index(),
10190b57cec5SDimitry Andric          [](auto& __this_alt, auto& __that_alt) {
10205f757f3fSDimitry Andric            using std::swap;
10210b57cec5SDimitry Andric            swap(__this_alt.__value, __that_alt.__value);
10220b57cec5SDimitry Andric          },
10230b57cec5SDimitry Andric          *this,
10240b57cec5SDimitry Andric          __that);
10250b57cec5SDimitry Andric    } else {
10260b57cec5SDimitry Andric      __impl* __lhs = this;
10275f757f3fSDimitry Andric      __impl* __rhs = std::addressof(__that);
10280b57cec5SDimitry Andric      if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
10295f757f3fSDimitry Andric        std::swap(__lhs, __rhs);
10300b57cec5SDimitry Andric      }
10315f757f3fSDimitry Andric      __impl __tmp(std::move(*__rhs));
103206c3fb27SDimitry Andric#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
10335ffd83dbSDimitry Andric      if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
10345f757f3fSDimitry Andric        this->__generic_construct(*__rhs, std::move(*__lhs));
10355ffd83dbSDimitry Andric      } else {
10360b57cec5SDimitry Andric        // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
10370b57cec5SDimitry Andric        // and `__tmp` is nothrow move constructible then we move `__tmp` back
10380b57cec5SDimitry Andric        // into `__rhs` and provide the strong exception safety guarantee.
10390b57cec5SDimitry Andric        try {
10405f757f3fSDimitry Andric          this->__generic_construct(*__rhs, std::move(*__lhs));
10410b57cec5SDimitry Andric        } catch (...) {
10420b57cec5SDimitry Andric          if (__tmp.__move_nothrow()) {
10435f757f3fSDimitry Andric            this->__generic_construct(*__rhs, std::move(__tmp));
10440b57cec5SDimitry Andric          }
10450b57cec5SDimitry Andric          throw;
10460b57cec5SDimitry Andric        }
10475ffd83dbSDimitry Andric      }
10480b57cec5SDimitry Andric#  else
10495ffd83dbSDimitry Andric      // this isn't consolidated with the `if constexpr` branch above due to
10505ffd83dbSDimitry Andric      // `throw` being ill-formed with exceptions disabled even when discarded.
10515f757f3fSDimitry Andric      this->__generic_construct(*__rhs, std::move(*__lhs));
10520b57cec5SDimitry Andric#  endif
10535f757f3fSDimitry Andric      this->__generic_construct(*__lhs, std::move(__tmp));
10540b57cec5SDimitry Andric    }
10550b57cec5SDimitry Andric  }
10560b57cec5SDimitry Andric
10570b57cec5SDimitry Andricprivate:
1058*cb14a3feSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const {
10590b57cec5SDimitry Andric    constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
10600b57cec5SDimitry Andric    return this->valueless_by_exception() || __results[this->index()];
10610b57cec5SDimitry Andric  }
10620b57cec5SDimitry Andric};
10630b57cec5SDimitry Andric
10640b57cec5SDimitry Andricstruct __no_narrowing_check {
10650b57cec5SDimitry Andric  template <class _Dest, class _Source>
106681ad6265SDimitry Andric  using _Apply = __type_identity<_Dest>;
10670b57cec5SDimitry Andric};
10680b57cec5SDimitry Andric
10690b57cec5SDimitry Andricstruct __narrowing_check {
10700b57cec5SDimitry Andric  template <class _Dest>
107181ad6265SDimitry Andric  static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
10720b57cec5SDimitry Andric  template <class _Dest, class _Source>
1073bdd1243dSDimitry Andric  using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
10740b57cec5SDimitry Andric};
10750b57cec5SDimitry Andric
10760b57cec5SDimitry Andrictemplate <class _Dest, class _Source>
1077*cb14a3feSDimitry Andricusing __check_for_narrowing _LIBCPP_NODEBUG = typename _If<
10780b57cec5SDimitry Andric#  ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
10790b57cec5SDimitry Andric    false &&
10800b57cec5SDimitry Andric#  endif
10810b57cec5SDimitry Andric        is_arithmetic<_Dest>::value,
10820b57cec5SDimitry Andric    __narrowing_check,
1083*cb14a3feSDimitry Andric    __no_narrowing_check >::template _Apply<_Dest, _Source>;
10840b57cec5SDimitry Andric
10850b57cec5SDimitry Andrictemplate <class _Tp, size_t _Idx>
10860b57cec5SDimitry Andricstruct __overload {
10870b57cec5SDimitry Andric  template <class _Up>
10880b57cec5SDimitry Andric  auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
10890b57cec5SDimitry Andric};
10900b57cec5SDimitry Andric
10915f757f3fSDimitry Andric// TODO(LLVM-19): Remove all occurrences of this macro.
10925f757f3fSDimitry Andric#  ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
10930b57cec5SDimitry Andrictemplate <class _Tp, size_t>
10940b57cec5SDimitry Andricstruct __overload_bool {
1095bdd1243dSDimitry Andric  template <class _Up, class _Ap = __remove_cvref_t<_Up>>
1096*cb14a3feSDimitry Andric  auto operator()(bool, _Up&&) const -> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
10970b57cec5SDimitry Andric};
10980b57cec5SDimitry Andric
10990b57cec5SDimitry Andrictemplate <size_t _Idx>
11000b57cec5SDimitry Andricstruct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
11010b57cec5SDimitry Andrictemplate <size_t _Idx>
11020b57cec5SDimitry Andricstruct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
11030b57cec5SDimitry Andrictemplate <size_t _Idx>
11040b57cec5SDimitry Andricstruct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
11050b57cec5SDimitry Andrictemplate <size_t _Idx>
11060b57cec5SDimitry Andricstruct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
11075f757f3fSDimitry Andric#  endif
11080b57cec5SDimitry Andric
11090b57cec5SDimitry Andrictemplate <class... _Bases>
11100b57cec5SDimitry Andricstruct __all_overloads : _Bases... {
11110b57cec5SDimitry Andric  void operator()() const;
11120b57cec5SDimitry Andric  using _Bases::operator()...;
11130b57cec5SDimitry Andric};
11140b57cec5SDimitry Andric
111506c3fb27SDimitry Andrictemplate <class _IdxSeq>
11160b57cec5SDimitry Andricstruct __make_overloads_imp;
11170b57cec5SDimitry Andric
11180b57cec5SDimitry Andrictemplate <size_t... _Idx>
11190b57cec5SDimitry Andricstruct __make_overloads_imp<__tuple_indices<_Idx...> > {
11200b57cec5SDimitry Andric  template <class... _Types>
1121349cc55cSDimitry Andric  using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
11220b57cec5SDimitry Andric};
11230b57cec5SDimitry Andric
11240b57cec5SDimitry Andrictemplate <class... _Types>
1125*cb14a3feSDimitry Andricusing _MakeOverloads _LIBCPP_NODEBUG =
1126*cb14a3feSDimitry Andric    typename __make_overloads_imp< __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
11270b57cec5SDimitry Andric
11280b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1129*cb14a3feSDimitry Andricusing __best_match_t = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
11300b57cec5SDimitry Andric
11311fd87a68SDimitry Andric} // namespace __variant_detail
11320b57cec5SDimitry Andric
11330b57cec5SDimitry Andrictemplate <class... _Types>
113406c3fb27SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
1135*cb14a3feSDimitry Andric    : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value,
11360b57cec5SDimitry Andric                                  __all<is_move_constructible_v<_Types>...>::value>,
11370b57cec5SDimitry Andric      private __sfinae_assign_base<
1138*cb14a3feSDimitry Andric          __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value,
1139*cb14a3feSDimitry Andric          __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> {
1140*cb14a3feSDimitry Andric  static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative.");
11410b57cec5SDimitry Andric
1142*cb14a3feSDimitry Andric  static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative.");
11430b57cec5SDimitry Andric
1144*cb14a3feSDimitry Andric  static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative.");
11450b57cec5SDimitry Andric
1146*cb14a3feSDimitry Andric  static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative.");
11470b57cec5SDimitry Andric
11480b57cec5SDimitry Andric  using __first_type = variant_alternative_t<0, variant>;
11490b57cec5SDimitry Andric
11500b57cec5SDimitry Andricpublic:
11510b57cec5SDimitry Andric  template <bool _Dummy                                                                               = true,
1152*cb14a3feSDimitry Andric            enable_if_t<__dependent_type<is_default_constructible<__first_type>, _Dummy>::value, int> = 0>
1153*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1154bdd1243dSDimitry Andric      : __impl_(in_place_index<0>) {}
11550b57cec5SDimitry Andric
115606c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
115706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&)      = default;
11580b57cec5SDimitry Andric
1159*cb14a3feSDimitry Andric  template < class _Arg,
1160bdd1243dSDimitry Andric             enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int>        = 0,
1161bdd1243dSDimitry Andric             enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int>  = 0,
1162bdd1243dSDimitry Andric             enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
11630b57cec5SDimitry Andric             class _Tp  = __variant_detail::__best_match_t<_Arg, _Types...>,
1164*cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
11650b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1166*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>)
11675f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
11680b57cec5SDimitry Andric
1169*cb14a3feSDimitry Andric  template <size_t _Ip,
1170*cb14a3feSDimitry Andric            class... _Args,
11710b57cec5SDimitry Andric            class                                               = enable_if_t<(_Ip < sizeof...(_Types)), int>,
11720b57cec5SDimitry Andric            class _Tp                                           = variant_alternative_t<_Ip, variant<_Types...>>,
11730b57cec5SDimitry Andric            enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1174*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept(
1175*cb14a3feSDimitry Andric      is_nothrow_constructible_v<_Tp, _Args...>)
11765f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
11770b57cec5SDimitry Andric
1178*cb14a3feSDimitry Andric  template < size_t _Ip,
11790b57cec5SDimitry Andric             class _Up,
11800b57cec5SDimitry Andric             class... _Args,
11810b57cec5SDimitry Andric             enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
11820b57cec5SDimitry Andric             class _Tp                                   = variant_alternative_t<_Ip, variant<_Types...>>,
1183*cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1184*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
11850b57cec5SDimitry Andric      in_place_index_t<_Ip>,
11860b57cec5SDimitry Andric      initializer_list<_Up> __il,
1187*cb14a3feSDimitry Andric      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
11885f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
11890b57cec5SDimitry Andric
1190*cb14a3feSDimitry Andric  template < class _Tp,
11910b57cec5SDimitry Andric             class... _Args,
1192*cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
11930b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1194*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
11950b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Args...>)
11965f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
11970b57cec5SDimitry Andric
1198*cb14a3feSDimitry Andric  template < class _Tp,
11990b57cec5SDimitry Andric             class _Up,
12000b57cec5SDimitry Andric             class... _Args,
1201*cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1202*cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1203*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
12040b57cec5SDimitry Andric      in_place_type_t<_Tp>,
12050b57cec5SDimitry Andric      initializer_list<_Up> __il,
1206*cb14a3feSDimitry Andric      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
12075f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
12080b57cec5SDimitry Andric
120906c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI ~variant() = default;
12100b57cec5SDimitry Andric
121106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
121206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&)      = default;
12130b57cec5SDimitry Andric
1214*cb14a3feSDimitry Andric  template < class _Arg,
1215bdd1243dSDimitry Andric             enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
12160b57cec5SDimitry Andric             class _Tp  = __variant_detail::__best_match_t<_Arg, _Types...>,
1217*cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1218*cb14a3feSDimitry Andric             enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0>
1219*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI variant&
1220*cb14a3feSDimitry Andric  operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) {
12215f757f3fSDimitry Andric    __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
12220b57cec5SDimitry Andric    return *this;
12230b57cec5SDimitry Andric  }
12240b57cec5SDimitry Andric
1225*cb14a3feSDimitry Andric  template < size_t _Ip,
12260b57cec5SDimitry Andric             class... _Args,
12270b57cec5SDimitry Andric             enable_if_t<(_Ip < sizeof...(_Types)), int>         = 0,
12280b57cec5SDimitry Andric             class _Tp                                           = variant_alternative_t<_Ip, variant<_Types...>>,
12290b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1230*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
12315f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
12320b57cec5SDimitry Andric  }
12330b57cec5SDimitry Andric
1234*cb14a3feSDimitry Andric  template < size_t _Ip,
12350b57cec5SDimitry Andric             class _Up,
12360b57cec5SDimitry Andric             class... _Args,
12370b57cec5SDimitry Andric             enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
12380b57cec5SDimitry Andric             class _Tp                                   = variant_alternative_t<_Ip, variant<_Types...>>,
1239*cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1240*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
12415f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
12420b57cec5SDimitry Andric  }
12430b57cec5SDimitry Andric
1244*cb14a3feSDimitry Andric  template < class _Tp,
12450b57cec5SDimitry Andric             class... _Args,
1246*cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
12470b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1248*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
12495f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
12500b57cec5SDimitry Andric  }
12510b57cec5SDimitry Andric
1252*cb14a3feSDimitry Andric  template < class _Tp,
12530b57cec5SDimitry Andric             class _Up,
12540b57cec5SDimitry Andric             class... _Args,
1255*cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1256*cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1257*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
12585f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
12590b57cec5SDimitry Andric  }
12600b57cec5SDimitry Andric
1261*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
1262bdd1243dSDimitry Andric    return __impl_.valueless_by_exception();
12630b57cec5SDimitry Andric  }
12640b57cec5SDimitry Andric
1265*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
12660b57cec5SDimitry Andric
1267*cb14a3feSDimitry Andric  template < bool _Dummy       = true,
1268*cb14a3feSDimitry Andric             enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
12690b57cec5SDimitry Andric                                 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
12700b57cec5SDimitry Andric                          int> = 0>
1271*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void swap(variant& __that) noexcept(
1272*cb14a3feSDimitry Andric      __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) {
1273bdd1243dSDimitry Andric    __impl_.__swap(__that.__impl_);
12740b57cec5SDimitry Andric  }
12750b57cec5SDimitry Andric
12760b57cec5SDimitry Andricprivate:
1277bdd1243dSDimitry Andric  __variant_detail::__impl<_Types...> __impl_;
12780b57cec5SDimitry Andric
12790b57cec5SDimitry Andric  friend struct __variant_detail::__access::__variant;
12800b57cec5SDimitry Andric  friend struct __variant_detail::__visitation::__variant;
12810b57cec5SDimitry Andric};
12820b57cec5SDimitry Andric
12830b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1284*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
12850b57cec5SDimitry Andric  return __v.index() == _Ip;
12860b57cec5SDimitry Andric}
12870b57cec5SDimitry Andric
12880b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1289*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1290bdd1243dSDimitry Andric  return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
12910b57cec5SDimitry Andric}
12920b57cec5SDimitry Andric
12930b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
1294*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr auto&& __generic_get(_Vp&& __v) {
12950b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
1296bdd1243dSDimitry Andric  if (!std::__holds_alternative<_Ip>(__v)) {
12970b57cec5SDimitry Andric    __throw_bad_variant_access();
12980b57cec5SDimitry Andric  }
12995f757f3fSDimitry Andric  return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
13000b57cec5SDimitry Andric}
13010b57cec5SDimitry Andric
13020b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1303bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1304*cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&
1305*cb14a3feSDimitry Andric    get(variant<_Types...>& __v) {
13060b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13070b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1308bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(__v);
13090b57cec5SDimitry Andric}
13100b57cec5SDimitry Andric
13110b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1312bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1313*cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
1314*cb14a3feSDimitry Andric    get(variant<_Types...>&& __v) {
13150b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13160b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13175f757f3fSDimitry Andric  return std::__generic_get<_Ip>(std::move(__v));
13180b57cec5SDimitry Andric}
13190b57cec5SDimitry Andric
13200b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1321bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1322*cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
1323*cb14a3feSDimitry Andric    get(const variant<_Types...>& __v) {
13240b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13250b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1326bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(__v);
13270b57cec5SDimitry Andric}
13280b57cec5SDimitry Andric
13290b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1330bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1331*cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
1332*cb14a3feSDimitry Andric    get(const variant<_Types...>&& __v) {
13330b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13340b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13355f757f3fSDimitry Andric  return std::__generic_get<_Ip>(std::move(__v));
13360b57cec5SDimitry Andric}
13370b57cec5SDimitry Andric
13380b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1339*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp& get(variant<_Types...>& __v) {
13400b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
13415f757f3fSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13420b57cec5SDimitry Andric}
13430b57cec5SDimitry Andric
13440b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1345*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp&& get(variant<_Types...>&& __v) {
13460b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
1347*cb14a3feSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
13480b57cec5SDimitry Andric}
13490b57cec5SDimitry Andric
13500b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1351*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&
1352*cb14a3feSDimitry Andricget(const variant<_Types...>& __v) {
13530b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
13545f757f3fSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13550b57cec5SDimitry Andric}
13560b57cec5SDimitry Andric
13570b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1358*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&&
1359*cb14a3feSDimitry Andricget(const variant<_Types...>&& __v) {
13600b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
1361*cb14a3feSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
13620b57cec5SDimitry Andric}
13630b57cec5SDimitry Andric
13640b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
1365*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
13660b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
1367*cb14a3feSDimitry Andric  return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr;
13680b57cec5SDimitry Andric}
13690b57cec5SDimitry Andric
13700b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1371*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
13720b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept {
13730b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13740b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1375bdd1243dSDimitry Andric  return std::__generic_get_if<_Ip>(__v);
13760b57cec5SDimitry Andric}
13770b57cec5SDimitry Andric
13780b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1379*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
13800b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept {
13810b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13820b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1383bdd1243dSDimitry Andric  return std::__generic_get_if<_Ip>(__v);
13840b57cec5SDimitry Andric}
13850b57cec5SDimitry Andric
13860b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1387*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
13880b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
13895f757f3fSDimitry Andric  return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13900b57cec5SDimitry Andric}
13910b57cec5SDimitry Andric
13920b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1393*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
13940b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
13955f757f3fSDimitry Andric  return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13960b57cec5SDimitry Andric}
13970b57cec5SDimitry Andric
13980b57cec5SDimitry Andrictemplate <class _Operator>
13990b57cec5SDimitry Andricstruct __convert_to_bool {
14000b57cec5SDimitry Andric  template <class _T1, class _T2>
1401*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const {
14025f757f3fSDimitry Andric    static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
14030b57cec5SDimitry Andric                  "the relational operator does not return a type which is implicitly convertible to bool");
14045f757f3fSDimitry Andric    return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
14050b57cec5SDimitry Andric  }
14060b57cec5SDimitry Andric};
14070b57cec5SDimitry Andric
14080b57cec5SDimitry Andrictemplate <class... _Types>
1409*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14100b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1411*cb14a3feSDimitry Andric  if (__lhs.index() != __rhs.index())
1412*cb14a3feSDimitry Andric    return false;
1413*cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1414*cb14a3feSDimitry Andric    return true;
14150b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
14160b57cec5SDimitry Andric}
14170b57cec5SDimitry Andric
141806c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
1419bdd1243dSDimitry Andric
1420*cb14a3feSDimitry Andrictemplate <class... _Types>
1421*cb14a3feSDimitry Andric  requires(three_way_comparable<_Types> && ...)
1422bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1423bdd1243dSDimitry Andricoperator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1424bdd1243dSDimitry Andric  using __variant_detail::__visitation::__variant;
1425bdd1243dSDimitry Andric  using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1426bdd1243dSDimitry Andric  if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1427bdd1243dSDimitry Andric    return strong_ordering::equal;
1428bdd1243dSDimitry Andric  if (__lhs.valueless_by_exception())
1429bdd1243dSDimitry Andric    return strong_ordering::less;
1430bdd1243dSDimitry Andric  if (__rhs.valueless_by_exception())
1431bdd1243dSDimitry Andric    return strong_ordering::greater;
1432bdd1243dSDimitry Andric  if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1433bdd1243dSDimitry Andric    return __c;
1434bdd1243dSDimitry Andric  auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1435bdd1243dSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1436bdd1243dSDimitry Andric}
1437bdd1243dSDimitry Andric
143806c3fb27SDimitry Andric#  endif // _LIBCPP_STD_VER >= 20
1439bdd1243dSDimitry Andric
14400b57cec5SDimitry Andrictemplate <class... _Types>
1441*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14420b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1443*cb14a3feSDimitry Andric  if (__lhs.index() != __rhs.index())
1444*cb14a3feSDimitry Andric    return true;
1445*cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1446*cb14a3feSDimitry Andric    return false;
1447*cb14a3feSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
14480b57cec5SDimitry Andric}
14490b57cec5SDimitry Andric
14500b57cec5SDimitry Andrictemplate <class... _Types>
1451*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14520b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1453*cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1454*cb14a3feSDimitry Andric    return false;
1455*cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1456*cb14a3feSDimitry Andric    return true;
1457*cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1458*cb14a3feSDimitry Andric    return true;
1459*cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1460*cb14a3feSDimitry Andric    return false;
14610b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
14620b57cec5SDimitry Andric}
14630b57cec5SDimitry Andric
14640b57cec5SDimitry Andrictemplate <class... _Types>
1465*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14660b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1467*cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1468*cb14a3feSDimitry Andric    return false;
1469*cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1470*cb14a3feSDimitry Andric    return true;
1471*cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1472*cb14a3feSDimitry Andric    return true;
1473*cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1474*cb14a3feSDimitry Andric    return false;
14750b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
14760b57cec5SDimitry Andric}
14770b57cec5SDimitry Andric
14780b57cec5SDimitry Andrictemplate <class... _Types>
1479*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14800b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1481*cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1482*cb14a3feSDimitry Andric    return true;
1483*cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1484*cb14a3feSDimitry Andric    return false;
1485*cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1486*cb14a3feSDimitry Andric    return true;
1487*cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1488*cb14a3feSDimitry Andric    return false;
1489*cb14a3feSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
14900b57cec5SDimitry Andric}
14910b57cec5SDimitry Andric
14920b57cec5SDimitry Andrictemplate <class... _Types>
1493*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14940b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1495*cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1496*cb14a3feSDimitry Andric    return true;
1497*cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1498*cb14a3feSDimitry Andric    return false;
1499*cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1500*cb14a3feSDimitry Andric    return true;
1501*cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1502*cb14a3feSDimitry Andric    return false;
1503*cb14a3feSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
15040b57cec5SDimitry Andric}
15050b57cec5SDimitry Andric
1506e8d8bef9SDimitry Andrictemplate <class... _Vs>
1507*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void __throw_if_valueless(_Vs&&... __vs) {
1508*cb14a3feSDimitry Andric  const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception());
1509e8d8bef9SDimitry Andric  if (__valueless) {
1510e8d8bef9SDimitry Andric    __throw_bad_variant_access();
1511e8d8bef9SDimitry Andric  }
1512e8d8bef9SDimitry Andric}
1513e8d8bef9SDimitry Andric
1514*cb14a3feSDimitry Andrictemplate < class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...> >
1515*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
1516*cb14a3feSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs) {
15170b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
15185f757f3fSDimitry Andric  std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1519*cb14a3feSDimitry Andric  return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
15200b57cec5SDimitry Andric}
15210b57cec5SDimitry Andric
152206c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
1523*cb14a3feSDimitry Andrictemplate < class _Rp,
1524*cb14a3feSDimitry Andric           class _Visitor,
1525*cb14a3feSDimitry Andric           class... _Vs,
15265f757f3fSDimitry Andric           typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...> >
1527*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
1528*cb14a3feSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs) {
1529e8d8bef9SDimitry Andric  using __variant_detail::__visitation::__variant;
15305f757f3fSDimitry Andric  std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1531*cb14a3feSDimitry Andric  return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1532e8d8bef9SDimitry Andric}
1533e8d8bef9SDimitry Andric#  endif
1534e8d8bef9SDimitry Andric
15350b57cec5SDimitry Andrictemplate <class... _Types>
1536*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI auto
1537*cb14a3feSDimitry Andricswap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1538*cb14a3feSDimitry Andric    -> decltype(__lhs.swap(__rhs)) {
1539*cb14a3feSDimitry Andric  return __lhs.swap(__rhs);
1540*cb14a3feSDimitry Andric}
15410b57cec5SDimitry Andric
15420b57cec5SDimitry Andrictemplate <class... _Types>
1543*cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
15440b57cec5SDimitry Andric  using argument_type = variant<_Types...>;
15450b57cec5SDimitry Andric  using result_type   = size_t;
15460b57cec5SDimitry Andric
1547*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const {
15480b57cec5SDimitry Andric    using __variant_detail::__visitation::__variant;
15490b57cec5SDimitry Andric    size_t __res =
15500b57cec5SDimitry Andric        __v.valueless_by_exception()
15510b57cec5SDimitry Andric            ? 299792458 // Random value chosen by the universe upon creation
15520b57cec5SDimitry Andric            : __variant::__visit_alt(
15530b57cec5SDimitry Andric                  [](const auto& __alt) {
1554bdd1243dSDimitry Andric                    using __alt_type   = __remove_cvref_t<decltype(__alt)>;
1555*cb14a3feSDimitry Andric                    using __value_type = remove_const_t< typename __alt_type::__value_type>;
15560b57cec5SDimitry Andric                    return hash<__value_type>{}(__alt.__value);
15570b57cec5SDimitry Andric                  },
15580b57cec5SDimitry Andric                  __v);
1559bdd1243dSDimitry Andric    return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
15600b57cec5SDimitry Andric  }
15610b57cec5SDimitry Andric};
15620b57cec5SDimitry Andric
1563fe6060f1SDimitry Andric// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1564fe6060f1SDimitry Andric// type whereas std::get will throw or returning nullptr. This makes it faster than
1565fe6060f1SDimitry Andric// std::get.
1566fe6060f1SDimitry Andrictemplate <size_t _Ip, class _Vp>
1567*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1568fe6060f1SDimitry Andric  using __variant_detail::__access::__variant;
15695f757f3fSDimitry Andric  return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
15700b57cec5SDimitry Andric}
1571fe6060f1SDimitry Andric
1572fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types>
1573*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1574bdd1243dSDimitry Andric  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1575fe6060f1SDimitry Andric}
1576fe6060f1SDimitry Andric
1577fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types>
1578*cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1579bdd1243dSDimitry Andric  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1580fe6060f1SDimitry Andric}
15810b57cec5SDimitry Andric
158206c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 17
15830b57cec5SDimitry Andric
15840b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
15850b57cec5SDimitry Andric
15860b57cec5SDimitry Andric_LIBCPP_POP_MACROS
15870b57cec5SDimitry Andric
1588bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
158906c3fb27SDimitry Andric#  include <exception>
1590bdd1243dSDimitry Andric#  include <type_traits>
1591bdd1243dSDimitry Andric#  include <typeinfo>
1592bdd1243dSDimitry Andric#  include <utility>
1593bdd1243dSDimitry Andric#endif
1594bdd1243dSDimitry Andric
15950b57cec5SDimitry Andric#endif // _LIBCPP_VARIANT
1596