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);
727a6dacacSDimitry Andric
737a6dacacSDimitry Andric    // [variant.visit], visitation
747a6dacacSDimitry Andric    template<class Self, class Visitor>
757a6dacacSDimitry Andric      constexpr decltype(auto) visit(this Self&&, Visitor&&); // Since C++26
767a6dacacSDimitry Andric    template<class R, class Self, class Visitor>
777a6dacacSDimitry Andric      constexpr R visit(this Self&&, Visitor&&);              // Since C++26
780b57cec5SDimitry Andric  };
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric  // 20.7.3, variant helper classes
810b57cec5SDimitry Andric  template <class T> struct variant_size; // undefined
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric  template <class T>
840b57cec5SDimitry Andric  inline constexpr size_t variant_size_v = variant_size<T>::value;
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric  template <class T> struct variant_size<const T>;
870b57cec5SDimitry Andric  template <class T> struct variant_size<volatile T>;
880b57cec5SDimitry Andric  template <class T> struct variant_size<const volatile T>;
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric  template <class... Types>
910b57cec5SDimitry Andric  struct variant_size<variant<Types...>>;
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative; // undefined
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric  template <size_t I, class T>
960b57cec5SDimitry Andric  using variant_alternative_t = typename variant_alternative<I, T>::type;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const T>;
990b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, volatile T>;
1000b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const volatile T>;
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric  template <size_t I, class... Types>
1030b57cec5SDimitry Andric  struct variant_alternative<I, variant<Types...>>;
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric  inline constexpr size_t variant_npos = -1;
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric  // 20.7.4, value access
1080b57cec5SDimitry Andric  template <class T, class... Types>
1090b57cec5SDimitry Andric  constexpr bool holds_alternative(const variant<Types...>&) noexcept;
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric  template <size_t I, class... Types>
1120b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&
1130b57cec5SDimitry Andric  get(variant<Types...>&);
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric  template <size_t I, class... Types>
1160b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&&
1170b57cec5SDimitry Andric  get(variant<Types...>&&);
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric  template <size_t I, class... Types>
1200b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&
1210b57cec5SDimitry Andric  get(const variant<Types...>&);
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric  template <size_t I, class... Types>
1240b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&&
1250b57cec5SDimitry Andric  get(const variant<Types...>&&);
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric  template <class T, class...  Types>
1280b57cec5SDimitry Andric  constexpr T& get(variant<Types...>&);
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric  template <class T, class... Types>
1310b57cec5SDimitry Andric  constexpr T&& get(variant<Types...>&&);
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric  template <class T, class... Types>
1340b57cec5SDimitry Andric  constexpr const T& get(const variant<Types...>&);
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric  template <class T, class... Types>
1370b57cec5SDimitry Andric  constexpr const T&& get(const variant<Types...>&&);
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric  template <size_t I, class... Types>
1400b57cec5SDimitry Andric  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
1410b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric  template <size_t I, class... Types>
1440b57cec5SDimitry Andric  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
1450b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric  template <class T, class... Types>
1480b57cec5SDimitry Andric  constexpr add_pointer_t<T>
1490b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric  template <class T, class... Types>
1520b57cec5SDimitry Andric  constexpr add_pointer_t<const T>
1530b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric  // 20.7.5, relational operators
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
1680b57cec5SDimitry Andric  template <class... Types>
1690b57cec5SDimitry Andric  constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric  template <class... Types>
1720b57cec5SDimitry Andric  constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
1730b57cec5SDimitry Andric
174bdd1243dSDimitry Andric  template <class... Types> requires (three_way_comparable<Types> && ...)
175bdd1243dSDimitry Andric  constexpr common_comparison_category_t<compare_three_way_result_t<Types>...>
176bdd1243dSDimitry Andric    operator<=>(const variant<Types...>&, const variant<Types...>&);           // since C++20
177bdd1243dSDimitry Andric
1780b57cec5SDimitry Andric  // 20.7.6, visitation
1790b57cec5SDimitry Andric  template <class Visitor, class... Variants>
1800b57cec5SDimitry Andric  constexpr see below visit(Visitor&&, Variants&&...);
1810b57cec5SDimitry Andric
182e8d8bef9SDimitry Andric  template <class R, class Visitor, class... Variants>
183e8d8bef9SDimitry Andric  constexpr R visit(Visitor&&, Variants&&...); // since C++20
184e8d8bef9SDimitry Andric
1850b57cec5SDimitry Andric  // 20.7.7, class monostate
1860b57cec5SDimitry Andric  struct monostate;
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric  // 20.7.8, monostate relational operators
1890b57cec5SDimitry Andric  constexpr bool operator==(monostate, monostate) noexcept;
190bdd1243dSDimitry Andric  constexpr bool operator!=(monostate, monostate) noexcept;             // until C++20
191bdd1243dSDimitry Andric  constexpr bool operator<(monostate, monostate) noexcept;              // until C++20
192bdd1243dSDimitry Andric  constexpr bool operator>(monostate, monostate) noexcept;              // until C++20
193bdd1243dSDimitry Andric  constexpr bool operator<=(monostate, monostate) noexcept;             // until C++20
194bdd1243dSDimitry Andric  constexpr bool operator>=(monostate, monostate) noexcept;             // until C++20
195bdd1243dSDimitry Andric  constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric  // 20.7.9, specialized algorithms
1980b57cec5SDimitry Andric  template <class... Types>
1990b57cec5SDimitry Andric  void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric  // 20.7.10, class bad_variant_access
2020b57cec5SDimitry Andric  class bad_variant_access;
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric  // 20.7.11, hash support
2050b57cec5SDimitry Andric  template <class T> struct hash;
2060b57cec5SDimitry Andric  template <class... Types> struct hash<variant<Types...>>;
2070b57cec5SDimitry Andric  template <> struct hash<monostate>;
2080b57cec5SDimitry Andric
2090b57cec5SDimitry Andric} // namespace std
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric*/
2120b57cec5SDimitry Andric
21381ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
214e8d8bef9SDimitry Andric#include <__availability>
215bdd1243dSDimitry Andric#include <__compare/common_comparison_category.h>
216bdd1243dSDimitry Andric#include <__compare/compare_three_way_result.h>
217bdd1243dSDimitry Andric#include <__compare/three_way_comparable.h>
218fe6060f1SDimitry Andric#include <__config>
21906c3fb27SDimitry Andric#include <__exception/exception.h>
220fe6060f1SDimitry Andric#include <__functional/hash.h>
221bdd1243dSDimitry Andric#include <__functional/invoke.h>
22281ad6265SDimitry Andric#include <__functional/operations.h>
22381ad6265SDimitry Andric#include <__functional/unary_function.h>
22406c3fb27SDimitry Andric#include <__memory/addressof.h>
225bdd1243dSDimitry Andric#include <__type_traits/add_const.h>
226bdd1243dSDimitry Andric#include <__type_traits/add_cv.h>
227bdd1243dSDimitry Andric#include <__type_traits/add_pointer.h>
228bdd1243dSDimitry Andric#include <__type_traits/add_volatile.h>
229bdd1243dSDimitry Andric#include <__type_traits/dependent_type.h>
230bdd1243dSDimitry Andric#include <__type_traits/is_array.h>
231bdd1243dSDimitry Andric#include <__type_traits/is_destructible.h>
232bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_move_constructible.h>
233bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_assignable.h>
234bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_constructible.h>
235bdd1243dSDimitry Andric#include <__type_traits/is_trivially_destructible.h>
236bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_assignable.h>
237bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_constructible.h>
238bdd1243dSDimitry Andric#include <__type_traits/is_void.h>
239bdd1243dSDimitry Andric#include <__type_traits/remove_const.h>
240bdd1243dSDimitry Andric#include <__type_traits/type_identity.h>
241bdd1243dSDimitry Andric#include <__type_traits/void_t.h>
24206c3fb27SDimitry Andric#include <__utility/declval.h>
243fe6060f1SDimitry Andric#include <__utility/forward.h>
2447a6dacacSDimitry Andric#include <__utility/forward_like.h>
24581ad6265SDimitry Andric#include <__utility/in_place.h>
24681ad6265SDimitry Andric#include <__utility/move.h>
24781ad6265SDimitry Andric#include <__utility/swap.h>
248fe6060f1SDimitry Andric#include <__variant/monostate.h>
24906c3fb27SDimitry Andric#include <__verbose_abort>
2500b57cec5SDimitry Andric#include <initializer_list>
251fe6060f1SDimitry Andric#include <limits>
2520b57cec5SDimitry Andric#include <new>
2530b57cec5SDimitry Andric#include <tuple>
2540b57cec5SDimitry Andric#include <version>
2550b57cec5SDimitry Andric
25681ad6265SDimitry Andric// standard-mandated includes
257bdd1243dSDimitry Andric
258bdd1243dSDimitry Andric// [variant.syn]
25981ad6265SDimitry Andric#include <compare>
26081ad6265SDimitry Andric
2610b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2620b57cec5SDimitry Andric#  pragma GCC system_header
2630b57cec5SDimitry Andric#endif
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
2660b57cec5SDimitry Andric#include <__undef_macros>
2670b57cec5SDimitry Andric
2680b57cec5SDimitry Andricnamespace std { // explicitly not using versioning namespace
2690b57cec5SDimitry Andric
27006c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
2710b57cec5SDimitry Andricpublic:
272bdd1243dSDimitry Andric  const char* what() const _NOEXCEPT override;
2730b57cec5SDimitry Andric};
2740b57cec5SDimitry Andric
2750b57cec5SDimitry Andric} // namespace std
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2780b57cec5SDimitry Andric
27906c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
2800b57cec5SDimitry Andric
281fe6060f1SDimitry Andric// Light N-dimensional array of function pointers. Used in place of std::array to avoid
282fe6060f1SDimitry Andric// adding a dependency.
283fe6060f1SDimitry Andrictemplate <class _Tp, size_t _Size>
284fe6060f1SDimitry Andricstruct __farray {
285fe6060f1SDimitry Andric  static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
286fe6060f1SDimitry Andric  _Tp __buf_[_Size] = {};
287fe6060f1SDimitry Andric
288cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __n) const noexcept { return __buf_[__n]; }
289fe6060f1SDimitry Andric};
290fe6060f1SDimitry Andric
291cb14a3feSDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS void
292cb14a3feSDimitry Andric__throw_bad_variant_access() {
29306c3fb27SDimitry Andric#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2940b57cec5SDimitry Andric  throw bad_variant_access();
2950b57cec5SDimitry Andric#  else
29606c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode");
2970b57cec5SDimitry Andric#  endif
2980b57cec5SDimitry Andric}
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andrictemplate <class... _Types>
3010b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant;
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andrictemplate <class _Tp>
3040b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size;
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andrictemplate <class _Tp>
307349cc55cSDimitry Andricinline constexpr size_t variant_size_v = variant_size<_Tp>::value;
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andrictemplate <class _Tp>
3100b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andrictemplate <class _Tp>
3130b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andrictemplate <class _Tp>
316cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> : variant_size<_Tp> {};
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andrictemplate <class... _Types>
319cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {};
3200b57cec5SDimitry Andric
3210b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3220b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative;
3230b57cec5SDimitry Andric
3240b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3250b57cec5SDimitry Andricusing variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
328cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {};
3290b57cec5SDimitry Andric
3300b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
331cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
334cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> : add_cv<variant_alternative_t<_Ip, _Tp>> {};
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
3370b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
3380b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
3390b57cec5SDimitry Andric  using type = __type_pack_element<_Ip, _Types...>;
3400b57cec5SDimitry Andric};
3410b57cec5SDimitry Andric
342349cc55cSDimitry Andricinline constexpr size_t variant_npos = static_cast<size_t>(-1);
3430b57cec5SDimitry Andric
344bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int __choose_index_type(unsigned int __num_elem) {
345e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned char>::max())
3460b57cec5SDimitry Andric    return 0;
347e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned short>::max())
3480b57cec5SDimitry Andric    return 1;
3490b57cec5SDimitry Andric  return 2;
3500b57cec5SDimitry Andric}
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andrictemplate <size_t _NumAlts>
3530b57cec5SDimitry Andricusing __variant_index_t =
3540b57cec5SDimitry Andric#  ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
3550b57cec5SDimitry Andric    unsigned int;
3560b57cec5SDimitry Andric#  else
357cb14a3feSDimitry Andric    std::tuple_element_t< __choose_index_type(_NumAlts), std::tuple<unsigned char, unsigned short, unsigned int> >;
3580b57cec5SDimitry Andric#  endif
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andrictemplate <class _IndexType>
3610b57cec5SDimitry Andricconstexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
3620b57cec5SDimitry Andric
363fe6060f1SDimitry Andrictemplate <class... _Types>
364fe6060f1SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant;
365fe6060f1SDimitry Andric
366fe6060f1SDimitry Andrictemplate <class... _Types>
367cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept {
368fe6060f1SDimitry Andric  return __vs;
369fe6060f1SDimitry Andric}
370fe6060f1SDimitry Andric
371fe6060f1SDimitry Andrictemplate <class... _Types>
372cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& __as_variant(const variant<_Types...>& __vs) noexcept {
373fe6060f1SDimitry Andric  return __vs;
374fe6060f1SDimitry Andric}
375fe6060f1SDimitry Andric
376fe6060f1SDimitry Andrictemplate <class... _Types>
377cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& __as_variant(variant<_Types...>&& __vs) noexcept {
3785f757f3fSDimitry Andric  return std::move(__vs);
379fe6060f1SDimitry Andric}
380fe6060f1SDimitry Andric
381fe6060f1SDimitry Andrictemplate <class... _Types>
382cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& __as_variant(const variant<_Types...>&& __vs) noexcept {
3835f757f3fSDimitry Andric  return std::move(__vs);
384fe6060f1SDimitry Andric}
385fe6060f1SDimitry Andric
3860b57cec5SDimitry Andricnamespace __find_detail {
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
389cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr size_t __find_index() {
3900b57cec5SDimitry Andric  constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
3910b57cec5SDimitry Andric  size_t __result            = __not_found;
3920b57cec5SDimitry Andric  for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
3930b57cec5SDimitry Andric    if (__matches[__i]) {
3940b57cec5SDimitry Andric      if (__result != __not_found) {
3950b57cec5SDimitry Andric        return __ambiguous;
3960b57cec5SDimitry Andric      }
3970b57cec5SDimitry Andric      __result = __i;
3980b57cec5SDimitry Andric    }
3990b57cec5SDimitry Andric  }
4000b57cec5SDimitry Andric  return __result;
4010b57cec5SDimitry Andric}
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andrictemplate <size_t _Index>
404cb14a3feSDimitry Andricstruct __find_unambiguous_index_sfinae_impl : integral_constant<size_t, _Index> {};
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andrictemplate <>
4070b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__not_found> {};
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andrictemplate <>
4100b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
413cb14a3feSDimitry Andricstruct __find_unambiguous_index_sfinae : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andric} // namespace __find_detail
4160b57cec5SDimitry Andric
4170b57cec5SDimitry Andricnamespace __variant_detail {
4180b57cec5SDimitry Andric
4190b57cec5SDimitry Andricstruct __valueless_t {};
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andricenum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
4220b57cec5SDimitry Andric
423cb14a3feSDimitry Andrictemplate <typename _Tp, template <typename> class _IsTriviallyAvailable, template <typename> class _IsAvailable>
4240b57cec5SDimitry Andricconstexpr _Trait __trait =
425cb14a3feSDimitry Andric    _IsTriviallyAvailable<_Tp>::value ? _Trait::_TriviallyAvailable
426cb14a3feSDimitry Andric    : _IsAvailable<_Tp>::value
427cb14a3feSDimitry Andric        ? _Trait::_Available
428cb14a3feSDimitry Andric        : _Trait::_Unavailable;
4290b57cec5SDimitry Andric
430cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
4310b57cec5SDimitry Andric  _Trait __result = _Trait::_TriviallyAvailable;
4320b57cec5SDimitry Andric  for (_Trait __t : __traits) {
4330b57cec5SDimitry Andric    if (static_cast<int>(__t) > static_cast<int>(__result)) {
4340b57cec5SDimitry Andric      __result = __t;
4350b57cec5SDimitry Andric    }
4360b57cec5SDimitry Andric  }
4370b57cec5SDimitry Andric  return __result;
4380b57cec5SDimitry Andric}
4390b57cec5SDimitry Andric
4400b57cec5SDimitry Andrictemplate <typename... _Types>
4410b57cec5SDimitry Andricstruct __traits {
4420b57cec5SDimitry Andric  static constexpr _Trait __copy_constructible_trait =
443cb14a3feSDimitry Andric      __variant_detail::__common_trait({__trait<_Types, is_trivially_copy_constructible, is_copy_constructible>...});
4440b57cec5SDimitry Andric
4450b57cec5SDimitry Andric  static constexpr _Trait __move_constructible_trait =
446cb14a3feSDimitry Andric      __variant_detail::__common_trait({__trait<_Types, is_trivially_move_constructible, is_move_constructible>...});
4470b57cec5SDimitry Andric
448bdd1243dSDimitry Andric  static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
449cb14a3feSDimitry Andric      {__copy_constructible_trait, __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
4500b57cec5SDimitry Andric
451bdd1243dSDimitry Andric  static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
452cb14a3feSDimitry Andric      {__move_constructible_trait, __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
4530b57cec5SDimitry Andric
454cb14a3feSDimitry Andric  static constexpr _Trait __destructible_trait =
455cb14a3feSDimitry Andric      __variant_detail::__common_trait({__trait<_Types, is_trivially_destructible, is_destructible>...});
4560b57cec5SDimitry Andric};
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andricnamespace __access {
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andricstruct __union {
4610b57cec5SDimitry Andric  template <class _Vp>
462cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
4635f757f3fSDimitry Andric    return std::forward<_Vp>(__v).__head;
4640b57cec5SDimitry Andric  }
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric  template <class _Vp, size_t _Ip>
467cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
4685f757f3fSDimitry Andric    return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
4690b57cec5SDimitry Andric  }
4700b57cec5SDimitry Andric};
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andricstruct __base {
4730b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
474cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
475cb14a3feSDimitry Andric    return __union::__get_alt(std::forward<_Vp>(__v).__data, in_place_index<_Ip>);
4760b57cec5SDimitry Andric  }
4770b57cec5SDimitry Andric};
4780b57cec5SDimitry Andric
4790b57cec5SDimitry Andricstruct __variant {
4800b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
481cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
4825f757f3fSDimitry Andric    return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_);
4830b57cec5SDimitry Andric  }
4840b57cec5SDimitry Andric};
4850b57cec5SDimitry Andric
4860b57cec5SDimitry Andric} // namespace __access
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andricnamespace __visitation {
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andricstruct __base {
4910b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
492cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
4930b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
494cb14a3feSDimitry Andric    constexpr auto __fdiagonal = __make_fdiagonal<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
495cb14a3feSDimitry Andric    return __fdiagonal[__index](std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
4960b57cec5SDimitry Andric  }
4970b57cec5SDimitry Andric
4980b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
499cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
500cb14a3feSDimitry Andric    constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
501cb14a3feSDimitry Andric    return __at(__fmatrix, __vs.index()...)(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
5020b57cec5SDimitry Andric  }
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andricprivate:
5050b57cec5SDimitry Andric  template <class _Tp>
506cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr const _Tp& __at(const _Tp& __elem) {
507cb14a3feSDimitry Andric    return __elem;
508cb14a3feSDimitry Andric  }
5090b57cec5SDimitry Andric
5100b57cec5SDimitry Andric  template <class _Tp, size_t _Np, typename... _Indices>
511cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto&&
512cb14a3feSDimitry Andric  __at(const __farray<_Tp, _Np>& __elems, size_t __index, _Indices... __indices) {
5130b57cec5SDimitry Andric    return __at(__elems[__index], __indices...);
5140b57cec5SDimitry Andric  }
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andric  template <class _Fp, class... _Fs>
51706c3fb27SDimitry Andric  static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() {
5180b57cec5SDimitry Andric    static_assert(
519cb14a3feSDimitry Andric        __all<is_same_v<_Fp, _Fs>...>::value, "`std::visit` requires the visitor to have a single return type.");
5200b57cec5SDimitry Andric  }
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric  template <class... _Fs>
523cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_farray(_Fs&&... __fs) {
524bdd1243dSDimitry Andric    __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
525bdd1243dSDimitry Andric    using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
5265f757f3fSDimitry Andric    return __result{{std::forward<_Fs>(__fs)...}};
5270b57cec5SDimitry Andric  }
5280b57cec5SDimitry Andric
529e8d8bef9SDimitry Andric  template <size_t... _Is>
5300b57cec5SDimitry Andric  struct __dispatcher {
5310b57cec5SDimitry Andric    template <class _Fp, class... _Vs>
532cb14a3feSDimitry Andric    _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
533cb14a3feSDimitry Andric      return std::__invoke(static_cast<_Fp>(__f), __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
5340b57cec5SDimitry Andric    }
5350b57cec5SDimitry Andric  };
5360b57cec5SDimitry Andric
5370b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
538cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_dispatch(index_sequence<_Is...>) {
5390b57cec5SDimitry Andric    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
5400b57cec5SDimitry Andric  }
5410b57cec5SDimitry Andric
5420b57cec5SDimitry Andric  template <size_t _Ip, class _Fp, class... _Vs>
543cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl() {
544cb14a3feSDimitry Andric    return __make_dispatch<_Fp, _Vs...>(index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
5450b57cec5SDimitry Andric  }
5460b57cec5SDimitry Andric
5470b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
548cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
5490b57cec5SDimitry Andric    return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
5500b57cec5SDimitry Andric  }
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric  template <class _Fp, class _Vp, class... _Vs>
553cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal() {
55406c3fb27SDimitry Andric    constexpr size_t __np = __remove_cvref_t<_Vp>::__size();
55506c3fb27SDimitry Andric    static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value);
55606c3fb27SDimitry Andric    return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{});
5570b57cec5SDimitry Andric  }
5580b57cec5SDimitry Andric
5590b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
560cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
5610b57cec5SDimitry Andric    return __make_dispatch<_Fp, _Vs...>(__is);
5620b57cec5SDimitry Andric  }
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
565cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto
566cb14a3feSDimitry Andric  __make_fmatrix_impl(index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) {
567cb14a3feSDimitry Andric    return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(index_sequence<_Is..., _Js>{}, __ls...)...);
5680b57cec5SDimitry Andric  }
5690b57cec5SDimitry Andric
5700b57cec5SDimitry Andric  template <class _Fp, class... _Vs>
571cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix() {
5720b57cec5SDimitry Andric    return __make_fmatrix_impl<_Fp, _Vs...>(
573bdd1243dSDimitry Andric        index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
5740b57cec5SDimitry Andric  }
5750b57cec5SDimitry Andric};
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andricstruct __variant {
5780b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
579cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
5800b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
581cb14a3feSDimitry Andric    return __base::__visit_alt_at(__index, std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__impl_...);
5820b57cec5SDimitry Andric  }
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
585cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
586fe6060f1SDimitry Andric    return __base::__visit_alt(
587cb14a3feSDimitry Andric        std::forward<_Visitor>(__visitor), std::__as_variant(std::forward<_Vs>(__vs)).__impl_...);
5880b57cec5SDimitry Andric  }
5890b57cec5SDimitry Andric
5900b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
591cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
5920b57cec5SDimitry Andric  __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
593cb14a3feSDimitry Andric    return __visit_alt_at(__index, __make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
5940b57cec5SDimitry Andric  }
5950b57cec5SDimitry Andric
5960b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
597cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
598cb14a3feSDimitry Andric    return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
5990b57cec5SDimitry Andric  }
600fe6060f1SDimitry Andric
60106c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
602e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor, class... _Vs>
603cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr _Rp __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
604cb14a3feSDimitry Andric    return __visit_alt(__make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
605e8d8bef9SDimitry Andric  }
606e8d8bef9SDimitry Andric#  endif
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andricprivate:
6090b57cec5SDimitry Andric  template <class _Visitor, class... _Values>
61006c3fb27SDimitry Andric  static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() {
611cb14a3feSDimitry Andric    static_assert(is_invocable_v<_Visitor, _Values...>, "`std::visit` requires the visitor to be exhaustive.");
6120b57cec5SDimitry Andric  }
6130b57cec5SDimitry Andric
6140b57cec5SDimitry Andric  template <class _Visitor>
6150b57cec5SDimitry Andric  struct __value_visitor {
6160b57cec5SDimitry Andric    template <class... _Alts>
617cb14a3feSDimitry Andric    _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Alts&&... __alts) const {
618cb14a3feSDimitry Andric      __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
619cb14a3feSDimitry Andric      return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
6200b57cec5SDimitry Andric    }
6210b57cec5SDimitry Andric    _Visitor&& __visitor;
6220b57cec5SDimitry Andric  };
6230b57cec5SDimitry Andric
62406c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
625e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
626e8d8bef9SDimitry Andric  struct __value_visitor_return_type {
627e8d8bef9SDimitry Andric    template <class... _Alts>
628cb14a3feSDimitry Andric    _LIBCPP_HIDE_FROM_ABI constexpr _Rp operator()(_Alts&&... __alts) const {
629cb14a3feSDimitry Andric      __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
630e8d8bef9SDimitry Andric      if constexpr (is_void_v<_Rp>) {
631cb14a3feSDimitry Andric        std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
632cb14a3feSDimitry Andric      } else {
633cb14a3feSDimitry Andric        return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
634e8d8bef9SDimitry Andric      }
635e8d8bef9SDimitry Andric    }
636e8d8bef9SDimitry Andric
637e8d8bef9SDimitry Andric    _Visitor&& __visitor;
638e8d8bef9SDimitry Andric  };
639e8d8bef9SDimitry Andric#  endif
640e8d8bef9SDimitry Andric
6410b57cec5SDimitry Andric  template <class _Visitor>
642cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
6435f757f3fSDimitry Andric    return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)};
6440b57cec5SDimitry Andric  }
645e8d8bef9SDimitry Andric
64606c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
647e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
648cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
6495f757f3fSDimitry Andric    return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)};
650e8d8bef9SDimitry Andric  }
651e8d8bef9SDimitry Andric#  endif
6520b57cec5SDimitry Andric};
6530b57cec5SDimitry Andric
6540b57cec5SDimitry Andric} // namespace __visitation
6550b57cec5SDimitry Andric
6560b57cec5SDimitry Andrictemplate <size_t _Index, class _Tp>
6570b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __alt {
6580b57cec5SDimitry Andric  using __value_type = _Tp;
6590b57cec5SDimitry Andric
6600b57cec5SDimitry Andric  template <class... _Args>
661cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args)
6625f757f3fSDimitry Andric      : __value(std::forward<_Args>(__args)...) {}
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric  __value_type __value;
6650b57cec5SDimitry Andric};
6660b57cec5SDimitry Andric
6670b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index, class... _Types>
6680b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union;
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index>
6710b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric#  define _LIBCPP_VARIANT_UNION(destructible_trait, destructor)                                                        \
6740b57cec5SDimitry Andric    template <size_t _Index, class _Tp, class... _Types>                                                               \
675cb14a3feSDimitry Andric    union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, _Index, _Tp, _Types...> {                                   \
6760b57cec5SDimitry Andric    public:                                                                                                            \
677cb14a3feSDimitry Andric      _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(__valueless_t) noexcept : __dummy{} {}                          \
6780b57cec5SDimitry Andric                                                                                                                       \
6790b57cec5SDimitry Andric      template <class... _Args>                                                                                        \
680cb14a3feSDimitry Andric      _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<0>, _Args&&... __args)                         \
6815f757f3fSDimitry Andric          : __head(in_place, std::forward<_Args>(__args)...) {}                                                        \
6820b57cec5SDimitry Andric                                                                                                                       \
6830b57cec5SDimitry Andric      template <size_t _Ip, class... _Args>                                                                            \
684cb14a3feSDimitry Andric      _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args)                       \
6855f757f3fSDimitry Andric          : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {}                                         \
6860b57cec5SDimitry Andric                                                                                                                       \
6870b57cec5SDimitry Andric      __union(const __union&) = default;                                                                               \
6880b57cec5SDimitry Andric      __union(__union&&)      = default;                                                                               \
6890b57cec5SDimitry Andric                                                                                                                       \
6900b57cec5SDimitry Andric      destructor                                                                                                       \
6910b57cec5SDimitry Andric                                                                                                                       \
692cb14a3feSDimitry Andric          __union&                                                                                                     \
693cb14a3feSDimitry Andric          operator=(const __union&) = default;                                                                         \
6940b57cec5SDimitry Andric      __union& operator=(__union&&) = default;                                                                         \
6950b57cec5SDimitry Andric                                                                                                                       \
6960b57cec5SDimitry Andric    private:                                                                                                           \
6970b57cec5SDimitry Andric      char __dummy;                                                                                                    \
6980b57cec5SDimitry Andric      __alt<_Index, _Tp> __head;                                                                                       \
6990b57cec5SDimitry Andric      __union<destructible_trait, _Index + 1, _Types...> __tail;                                                       \
7000b57cec5SDimitry Andric                                                                                                                       \
7010b57cec5SDimitry Andric      friend struct __access::__union;                                                                                 \
7020b57cec5SDimitry Andric    }
7030b57cec5SDimitry Andric
7040b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
7050b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union(){});
7060b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
7070b57cec5SDimitry Andric
7080b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_UNION
7090b57cec5SDimitry Andric
7100b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, class... _Types>
7110b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __base {
7120b57cec5SDimitry Andricpublic:
7130b57cec5SDimitry Andric  using __index_t = __variant_index_t<sizeof...(_Types)>;
7140b57cec5SDimitry Andric
715cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(__valueless_t __tag) noexcept
716753f127fSDimitry Andric      : __data(__tag), __index(__variant_npos<__index_t>) {}
7170b57cec5SDimitry Andric
7180b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
719cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
720cb14a3feSDimitry Andric      : __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), __index(_Ip) {}
7210b57cec5SDimitry Andric
722cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { return index() == variant_npos; }
7230b57cec5SDimitry Andric
724cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept {
7250b57cec5SDimitry Andric    return __index == __variant_npos<__index_t> ? variant_npos : __index;
7260b57cec5SDimitry Andric  }
7270b57cec5SDimitry Andric
7280b57cec5SDimitry Andricprotected:
729cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() & { return *this; }
7300b57cec5SDimitry Andric
731cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() && { return std::move(*this); }
7320b57cec5SDimitry Andric
733cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const& { return *this; }
7340b57cec5SDimitry Andric
735cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const&& { return std::move(*this); }
7360b57cec5SDimitry Andric
737cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Types); }
7380b57cec5SDimitry Andric
7390b57cec5SDimitry Andric  __union<_DestructibleTrait, 0, _Types...> __data;
7400b57cec5SDimitry Andric  __index_t __index;
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric  friend struct __access::__base;
7430b57cec5SDimitry Andric  friend struct __visitation::__base;
7440b57cec5SDimitry Andric};
7450b57cec5SDimitry Andric
7460b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__destructible_trait>
747e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __dtor;
7480b57cec5SDimitry Andric
7490b57cec5SDimitry Andric#  define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy)                                          \
7500b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
751cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, destructible_trait>                                         \
7520b57cec5SDimitry Andric        : public __base<destructible_trait, _Types...> {                                                               \
7530b57cec5SDimitry Andric      using __base_type = __base<destructible_trait, _Types...>;                                                       \
7540b57cec5SDimitry Andric      using __index_t   = typename __base_type::__index_t;                                                             \
7550b57cec5SDimitry Andric                                                                                                                       \
7560b57cec5SDimitry Andric    public:                                                                                                            \
7570b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
7580b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
7590b57cec5SDimitry Andric                                                                                                                       \
760e8d8bef9SDimitry Andric      __dtor(const __dtor&)                       = default;                                                           \
761e8d8bef9SDimitry Andric      __dtor(__dtor&&)                            = default;                                                           \
762cb14a3feSDimitry Andric      destructor __dtor& operator=(const __dtor&) = default;                                                           \
763e8d8bef9SDimitry Andric      __dtor& operator=(__dtor&&)                 = default;                                                           \
7640b57cec5SDimitry Andric                                                                                                                       \
7650b57cec5SDimitry Andric    protected:                                                                                                         \
766cb14a3feSDimitry Andric      inline _LIBCPP_HIDE_FROM_ABI destroy                                                                             \
7670b57cec5SDimitry Andric    }
7680b57cec5SDimitry Andric
7690b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
770cb14a3feSDimitry Andric    _Trait::_TriviallyAvailable, ~__dtor() = default;
771cb14a3feSDimitry Andric    , void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
7720b57cec5SDimitry Andric
7730b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
7740b57cec5SDimitry Andric    _Trait::_Available,
775e8d8bef9SDimitry Andric    ~__dtor() { __destroy(); },
7760b57cec5SDimitry Andric    void __destroy() noexcept {
7770b57cec5SDimitry Andric      if (!this->valueless_by_exception()) {
7780b57cec5SDimitry Andric        __visitation::__base::__visit_alt(
7790b57cec5SDimitry Andric            [](auto& __alt) noexcept {
780bdd1243dSDimitry Andric              using __alt_type = __remove_cvref_t<decltype(__alt)>;
7810b57cec5SDimitry Andric              __alt.~__alt_type();
7820b57cec5SDimitry Andric            },
7830b57cec5SDimitry Andric            *this);
7840b57cec5SDimitry Andric      }
7850b57cec5SDimitry Andric      this->__index = __variant_npos<__index_t>;
7860b57cec5SDimitry Andric    });
7870b57cec5SDimitry Andric
788cb14a3feSDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable, ~__dtor() = delete;, void __destroy() noexcept = delete;);
7890b57cec5SDimitry Andric
7900b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_DESTRUCTOR
7910b57cec5SDimitry Andric
7920b57cec5SDimitry Andrictemplate <class _Traits>
793e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
794e8d8bef9SDimitry Andric  using __base_type = __dtor<_Traits>;
7950b57cec5SDimitry Andric
7960b57cec5SDimitry Andricpublic:
7970b57cec5SDimitry Andric  using __base_type::__base_type;
7980b57cec5SDimitry Andric  using __base_type::operator=;
7990b57cec5SDimitry Andric
8000b57cec5SDimitry Andricprotected:
8010b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class... _Args>
802cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
803cb14a3feSDimitry Andric    ::new ((void*)std::addressof(__a)) __alt<_Ip, _Tp>(in_place, std::forward<_Args>(__args)...);
8040b57cec5SDimitry Andric    return __a.__value;
8050b57cec5SDimitry Andric  }
8060b57cec5SDimitry Andric
8070b57cec5SDimitry Andric  template <class _Rhs>
808cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
8090b57cec5SDimitry Andric    __lhs.__destroy();
8100b57cec5SDimitry Andric    if (!__rhs.valueless_by_exception()) {
8117a6dacacSDimitry Andric      auto __rhs_index = __rhs.index();
8120b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
8137a6dacacSDimitry Andric          __rhs_index,
8140b57cec5SDimitry Andric          [](auto& __lhs_alt, auto&& __rhs_alt) {
815cb14a3feSDimitry Andric            __construct_alt(__lhs_alt, std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
8160b57cec5SDimitry Andric          },
817cb14a3feSDimitry Andric          __lhs,
818cb14a3feSDimitry Andric          std::forward<_Rhs>(__rhs));
8197a6dacacSDimitry Andric      __lhs.__index = __rhs_index;
8200b57cec5SDimitry Andric    }
8210b57cec5SDimitry Andric  }
8220b57cec5SDimitry Andric};
8230b57cec5SDimitry Andric
8240b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_constructible_trait>
8250b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_constructor;
8260b57cec5SDimitry Andric
827cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, move_constructor)                                 \
8280b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
829cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, move_constructible_trait>                       \
830e8d8bef9SDimitry Andric        : public __ctor<__traits<_Types...>> {                                                                         \
831e8d8bef9SDimitry Andric      using __base_type = __ctor<__traits<_Types...>>;                                                                 \
8320b57cec5SDimitry Andric                                                                                                                       \
8330b57cec5SDimitry Andric    public:                                                                                                            \
8340b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
8350b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
8360b57cec5SDimitry Andric                                                                                                                       \
8370b57cec5SDimitry Andric      __move_constructor(const __move_constructor&)            = default;                                              \
838cb14a3feSDimitry Andric      move_constructor ~__move_constructor()                   = default;                                              \
8390b57cec5SDimitry Andric      __move_constructor& operator=(const __move_constructor&) = default;                                              \
8400b57cec5SDimitry Andric      __move_constructor& operator=(__move_constructor&&)      = default;                                              \
8410b57cec5SDimitry Andric    }
8420b57cec5SDimitry Andric
843cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_TriviallyAvailable,
8440b57cec5SDimitry Andric                                 __move_constructor(__move_constructor&& __that) = default;);
8450b57cec5SDimitry Andric
8460b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
8470b57cec5SDimitry Andric    _Trait::_Available,
848cb14a3feSDimitry Andric    __move_constructor(__move_constructor&& __that) noexcept(__all<is_nothrow_move_constructible_v<_Types>...>::value)
849cb14a3feSDimitry Andric    : __move_constructor(__valueless_t{}) { this->__generic_construct(*this, std::move(__that)); });
8500b57cec5SDimitry Andric
851cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(_Trait::_Unavailable, __move_constructor(__move_constructor&&) = delete;);
8520b57cec5SDimitry Andric
8530b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
8540b57cec5SDimitry Andric
8550b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_constructible_trait>
8560b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_constructor;
8570b57cec5SDimitry Andric
858cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, copy_constructor)                                 \
8590b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
860cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, copy_constructible_trait>                       \
8610b57cec5SDimitry Andric        : public __move_constructor<__traits<_Types...>> {                                                             \
8620b57cec5SDimitry Andric      using __base_type = __move_constructor<__traits<_Types...>>;                                                     \
8630b57cec5SDimitry Andric                                                                                                                       \
8640b57cec5SDimitry Andric    public:                                                                                                            \
8650b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
8660b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
8670b57cec5SDimitry Andric                                                                                                                       \
868cb14a3feSDimitry Andric      copy_constructor __copy_constructor(__copy_constructor&&) = default;                                             \
8690b57cec5SDimitry Andric      ~__copy_constructor()                                     = default;                                             \
8700b57cec5SDimitry Andric      __copy_constructor& operator=(const __copy_constructor&)  = default;                                             \
8710b57cec5SDimitry Andric      __copy_constructor& operator=(__copy_constructor&&)       = default;                                             \
8720b57cec5SDimitry Andric    }
8730b57cec5SDimitry Andric
874cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_TriviallyAvailable,
8750b57cec5SDimitry Andric                                 __copy_constructor(const __copy_constructor& __that) = default;);
8760b57cec5SDimitry Andric
8770b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
878cb14a3feSDimitry Andric    _Trait::_Available, __copy_constructor(const __copy_constructor& __that)
879cb14a3feSDimitry Andric    : __copy_constructor(__valueless_t{}) { this->__generic_construct(*this, __that); });
8800b57cec5SDimitry Andric
881cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(_Trait::_Unavailable, __copy_constructor(const __copy_constructor&) = delete;);
8820b57cec5SDimitry Andric
8830b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
8840b57cec5SDimitry Andric
8850b57cec5SDimitry Andrictemplate <class _Traits>
8860b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
8870b57cec5SDimitry Andric  using __base_type = __copy_constructor<_Traits>;
8880b57cec5SDimitry Andric
8890b57cec5SDimitry Andricpublic:
8900b57cec5SDimitry Andric  using __base_type::__base_type;
8910b57cec5SDimitry Andric  using __base_type::operator=;
8920b57cec5SDimitry Andric
8930b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
894cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI auto& __emplace(_Args&&... __args) {
8950b57cec5SDimitry Andric    this->__destroy();
896cb14a3feSDimitry Andric    auto& __res   = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Args>(__args)...);
8970b57cec5SDimitry Andric    this->__index = _Ip;
8980b57cec5SDimitry Andric    return __res;
8990b57cec5SDimitry Andric  }
9000b57cec5SDimitry Andric
9010b57cec5SDimitry Andricprotected:
9020b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class _Arg>
903cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
9040b57cec5SDimitry Andric    if (this->index() == _Ip) {
9055f757f3fSDimitry Andric      __a.__value = std::forward<_Arg>(__arg);
9060b57cec5SDimitry Andric    } else {
9070b57cec5SDimitry Andric      struct {
908cb14a3feSDimitry Andric        _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const { __this->__emplace<_Ip>(std::forward<_Arg>(__arg)); }
90906c3fb27SDimitry Andric        _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const {
9105f757f3fSDimitry Andric          __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
9110b57cec5SDimitry Andric        }
9120b57cec5SDimitry Andric        __assignment* __this;
9130b57cec5SDimitry Andric        _Arg&& __arg;
9145f757f3fSDimitry Andric      } __impl{this, std::forward<_Arg>(__arg)};
915cb14a3feSDimitry Andric      __impl(bool_constant < is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v < _Tp >> {});
9160b57cec5SDimitry Andric    }
9170b57cec5SDimitry Andric  }
9180b57cec5SDimitry Andric
9190b57cec5SDimitry Andric  template <class _That>
920cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __generic_assign(_That&& __that) {
9210b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
9220b57cec5SDimitry Andric      // do nothing.
9230b57cec5SDimitry Andric    } else if (__that.valueless_by_exception()) {
9240b57cec5SDimitry Andric      this->__destroy();
9250b57cec5SDimitry Andric    } else {
9260b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
9270b57cec5SDimitry Andric          __that.index(),
9280b57cec5SDimitry Andric          [this](auto& __this_alt, auto&& __that_alt) {
929cb14a3feSDimitry Andric            this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value);
9300b57cec5SDimitry Andric          },
931cb14a3feSDimitry Andric          *this,
932cb14a3feSDimitry Andric          std::forward<_That>(__that));
9330b57cec5SDimitry Andric    }
9340b57cec5SDimitry Andric  }
9350b57cec5SDimitry Andric};
9360b57cec5SDimitry Andric
9370b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_assignable_trait>
9380b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_assignment;
9390b57cec5SDimitry Andric
940cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment)                                      \
9410b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
942cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, move_assignable_trait>                           \
9430b57cec5SDimitry Andric        : public __assignment<__traits<_Types...>> {                                                                   \
9440b57cec5SDimitry Andric      using __base_type = __assignment<__traits<_Types...>>;                                                           \
9450b57cec5SDimitry Andric                                                                                                                       \
9460b57cec5SDimitry Andric    public:                                                                                                            \
9470b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
9480b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
9490b57cec5SDimitry Andric                                                                                                                       \
9500b57cec5SDimitry Andric      __move_assignment(const __move_assignment&)            = default;                                                \
9510b57cec5SDimitry Andric      __move_assignment(__move_assignment&&)                 = default;                                                \
9520b57cec5SDimitry Andric      ~__move_assignment()                                   = default;                                                \
9530b57cec5SDimitry Andric      __move_assignment& operator=(const __move_assignment&) = default;                                                \
9540b57cec5SDimitry Andric      move_assignment                                                                                                  \
9550b57cec5SDimitry Andric    }
9560b57cec5SDimitry Andric
957cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable,
9580b57cec5SDimitry Andric                                __move_assignment& operator=(__move_assignment&& __that) = default;);
9590b57cec5SDimitry Andric
9600b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
9610b57cec5SDimitry Andric    _Trait::_Available,
962cb14a3feSDimitry Andric    __move_assignment&
963cb14a3feSDimitry Andric    operator=(__move_assignment&& __that) noexcept(
964cb14a3feSDimitry Andric        __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) {
9655f757f3fSDimitry Andric      this->__generic_assign(std::move(__that));
9660b57cec5SDimitry Andric      return *this;
9670b57cec5SDimitry Andric    });
9680b57cec5SDimitry Andric
969cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_Unavailable, __move_assignment& operator=(__move_assignment&&) = delete;);
9700b57cec5SDimitry Andric
9710b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
9720b57cec5SDimitry Andric
9730b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_assignable_trait>
9740b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_assignment;
9750b57cec5SDimitry Andric
976cb14a3feSDimitry Andric#  define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment)                                      \
9770b57cec5SDimitry Andric    template <class... _Types>                                                                                         \
978cb14a3feSDimitry Andric    class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, copy_assignable_trait>                           \
9790b57cec5SDimitry Andric        : public __move_assignment<__traits<_Types...>> {                                                              \
9800b57cec5SDimitry Andric      using __base_type = __move_assignment<__traits<_Types...>>;                                                      \
9810b57cec5SDimitry Andric                                                                                                                       \
9820b57cec5SDimitry Andric    public:                                                                                                            \
9830b57cec5SDimitry Andric      using __base_type::__base_type;                                                                                  \
9840b57cec5SDimitry Andric      using __base_type::operator=;                                                                                    \
9850b57cec5SDimitry Andric                                                                                                                       \
9860b57cec5SDimitry Andric      __copy_assignment(const __copy_assignment&)                       = default;                                     \
9870b57cec5SDimitry Andric      __copy_assignment(__copy_assignment&&)                            = default;                                     \
9880b57cec5SDimitry Andric      ~__copy_assignment()                                              = default;                                     \
989cb14a3feSDimitry Andric      copy_assignment __copy_assignment& operator=(__copy_assignment&&) = default;                                     \
9900b57cec5SDimitry Andric    }
9910b57cec5SDimitry Andric
992cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable,
9930b57cec5SDimitry Andric                                __copy_assignment& operator=(const __copy_assignment& __that) = default;);
9940b57cec5SDimitry Andric
9950b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
996cb14a3feSDimitry Andric    _Trait::_Available, __copy_assignment& operator=(const __copy_assignment& __that) {
9970b57cec5SDimitry Andric      this->__generic_assign(__that);
9980b57cec5SDimitry Andric      return *this;
9990b57cec5SDimitry Andric    });
10000b57cec5SDimitry Andric
1001cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable, __copy_assignment& operator=(const __copy_assignment&) = delete;);
10020b57cec5SDimitry Andric
10030b57cec5SDimitry Andric#  undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
10040b57cec5SDimitry Andric
10050b57cec5SDimitry Andrictemplate <class... _Types>
1006cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __impl : public __copy_assignment<__traits<_Types...>> {
10070b57cec5SDimitry Andric  using __base_type = __copy_assignment<__traits<_Types...>>;
10080b57cec5SDimitry Andric
10090b57cec5SDimitry Andricpublic:
101081ad6265SDimitry Andric  using __base_type::__base_type; // get in_place_index_t constructor & friends
101106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl(__impl const&)            = default;
101206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl(__impl&&)                 = default;
101306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
101406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&)      = default;
10150b57cec5SDimitry Andric
10160b57cec5SDimitry Andric  template <size_t _Ip, class _Arg>
1017cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __assign(_Arg&& __arg) {
1018cb14a3feSDimitry Andric    this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg));
10190b57cec5SDimitry Andric  }
10200b57cec5SDimitry Andric
1021cb14a3feSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI void __swap(__impl& __that) {
10220b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
10230b57cec5SDimitry Andric      // do nothing.
10240b57cec5SDimitry Andric    } else if (this->index() == __that.index()) {
10250b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
10260b57cec5SDimitry Andric          this->index(),
10270b57cec5SDimitry Andric          [](auto& __this_alt, auto& __that_alt) {
10285f757f3fSDimitry Andric            using std::swap;
10290b57cec5SDimitry Andric            swap(__this_alt.__value, __that_alt.__value);
10300b57cec5SDimitry Andric          },
10310b57cec5SDimitry Andric          *this,
10320b57cec5SDimitry Andric          __that);
10330b57cec5SDimitry Andric    } else {
10340b57cec5SDimitry Andric      __impl* __lhs = this;
10355f757f3fSDimitry Andric      __impl* __rhs = std::addressof(__that);
10360b57cec5SDimitry Andric      if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
10375f757f3fSDimitry Andric        std::swap(__lhs, __rhs);
10380b57cec5SDimitry Andric      }
10395f757f3fSDimitry Andric      __impl __tmp(std::move(*__rhs));
104006c3fb27SDimitry Andric#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
10415ffd83dbSDimitry Andric      if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
10425f757f3fSDimitry Andric        this->__generic_construct(*__rhs, std::move(*__lhs));
10435ffd83dbSDimitry Andric      } else {
10440b57cec5SDimitry Andric        // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
10450b57cec5SDimitry Andric        // and `__tmp` is nothrow move constructible then we move `__tmp` back
10460b57cec5SDimitry Andric        // into `__rhs` and provide the strong exception safety guarantee.
10470b57cec5SDimitry Andric        try {
10485f757f3fSDimitry Andric          this->__generic_construct(*__rhs, std::move(*__lhs));
10490b57cec5SDimitry Andric        } catch (...) {
10500b57cec5SDimitry Andric          if (__tmp.__move_nothrow()) {
10515f757f3fSDimitry Andric            this->__generic_construct(*__rhs, std::move(__tmp));
10520b57cec5SDimitry Andric          }
10530b57cec5SDimitry Andric          throw;
10540b57cec5SDimitry Andric        }
10555ffd83dbSDimitry Andric      }
10560b57cec5SDimitry Andric#  else
10575ffd83dbSDimitry Andric      // this isn't consolidated with the `if constexpr` branch above due to
10585ffd83dbSDimitry Andric      // `throw` being ill-formed with exceptions disabled even when discarded.
10595f757f3fSDimitry Andric      this->__generic_construct(*__rhs, std::move(*__lhs));
10600b57cec5SDimitry Andric#  endif
10615f757f3fSDimitry Andric      this->__generic_construct(*__lhs, std::move(__tmp));
10620b57cec5SDimitry Andric    }
10630b57cec5SDimitry Andric  }
10640b57cec5SDimitry Andric
10650b57cec5SDimitry Andricprivate:
1066cb14a3feSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const {
10670b57cec5SDimitry Andric    constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
10680b57cec5SDimitry Andric    return this->valueless_by_exception() || __results[this->index()];
10690b57cec5SDimitry Andric  }
10700b57cec5SDimitry Andric};
10710b57cec5SDimitry Andric
10720b57cec5SDimitry Andricstruct __no_narrowing_check {
10730b57cec5SDimitry Andric  template <class _Dest, class _Source>
107481ad6265SDimitry Andric  using _Apply = __type_identity<_Dest>;
10750b57cec5SDimitry Andric};
10760b57cec5SDimitry Andric
10770b57cec5SDimitry Andricstruct __narrowing_check {
10780b57cec5SDimitry Andric  template <class _Dest>
107981ad6265SDimitry Andric  static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
10800b57cec5SDimitry Andric  template <class _Dest, class _Source>
1081bdd1243dSDimitry Andric  using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
10820b57cec5SDimitry Andric};
10830b57cec5SDimitry Andric
10840b57cec5SDimitry Andrictemplate <class _Dest, class _Source>
1085cb14a3feSDimitry Andricusing __check_for_narrowing _LIBCPP_NODEBUG = typename _If<
10860b57cec5SDimitry Andric#  ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
10870b57cec5SDimitry Andric    false &&
10880b57cec5SDimitry Andric#  endif
10890b57cec5SDimitry Andric        is_arithmetic<_Dest>::value,
10900b57cec5SDimitry Andric    __narrowing_check,
1091cb14a3feSDimitry Andric    __no_narrowing_check >::template _Apply<_Dest, _Source>;
10920b57cec5SDimitry Andric
10930b57cec5SDimitry Andrictemplate <class _Tp, size_t _Idx>
10940b57cec5SDimitry Andricstruct __overload {
10950b57cec5SDimitry Andric  template <class _Up>
10960b57cec5SDimitry Andric  auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
10970b57cec5SDimitry Andric};
10980b57cec5SDimitry Andric
10995f757f3fSDimitry Andric// TODO(LLVM-19): Remove all occurrences of this macro.
11005f757f3fSDimitry Andric#  ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
11010b57cec5SDimitry Andrictemplate <class _Tp, size_t>
11020b57cec5SDimitry Andricstruct __overload_bool {
1103bdd1243dSDimitry Andric  template <class _Up, class _Ap = __remove_cvref_t<_Up>>
1104cb14a3feSDimitry Andric  auto operator()(bool, _Up&&) const -> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
11050b57cec5SDimitry Andric};
11060b57cec5SDimitry Andric
11070b57cec5SDimitry Andrictemplate <size_t _Idx>
11080b57cec5SDimitry Andricstruct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
11090b57cec5SDimitry Andrictemplate <size_t _Idx>
11100b57cec5SDimitry Andricstruct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
11110b57cec5SDimitry Andrictemplate <size_t _Idx>
11120b57cec5SDimitry Andricstruct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
11130b57cec5SDimitry Andrictemplate <size_t _Idx>
11140b57cec5SDimitry Andricstruct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
11155f757f3fSDimitry Andric#  endif
11160b57cec5SDimitry Andric
11170b57cec5SDimitry Andrictemplate <class... _Bases>
11180b57cec5SDimitry Andricstruct __all_overloads : _Bases... {
11190b57cec5SDimitry Andric  void operator()() const;
11200b57cec5SDimitry Andric  using _Bases::operator()...;
11210b57cec5SDimitry Andric};
11220b57cec5SDimitry Andric
112306c3fb27SDimitry Andrictemplate <class _IdxSeq>
11240b57cec5SDimitry Andricstruct __make_overloads_imp;
11250b57cec5SDimitry Andric
11260b57cec5SDimitry Andrictemplate <size_t... _Idx>
11270b57cec5SDimitry Andricstruct __make_overloads_imp<__tuple_indices<_Idx...> > {
11280b57cec5SDimitry Andric  template <class... _Types>
1129349cc55cSDimitry Andric  using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
11300b57cec5SDimitry Andric};
11310b57cec5SDimitry Andric
11320b57cec5SDimitry Andrictemplate <class... _Types>
1133cb14a3feSDimitry Andricusing _MakeOverloads _LIBCPP_NODEBUG =
1134cb14a3feSDimitry Andric    typename __make_overloads_imp< __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
11350b57cec5SDimitry Andric
11360b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1137cb14a3feSDimitry Andricusing __best_match_t = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
11380b57cec5SDimitry Andric
11391fd87a68SDimitry Andric} // namespace __variant_detail
11400b57cec5SDimitry Andric
11417a6dacacSDimitry Andrictemplate <class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
11427a6dacacSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
11437a6dacacSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs);
11447a6dacacSDimitry Andric
11457a6dacacSDimitry Andric#  if _LIBCPP_STD_VER >= 20
11467a6dacacSDimitry Andrictemplate <class _Rp,
11477a6dacacSDimitry Andric          class _Visitor,
11487a6dacacSDimitry Andric          class... _Vs,
11497a6dacacSDimitry Andric          typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
11507a6dacacSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
11517a6dacacSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs);
11527a6dacacSDimitry Andric#  endif
11537a6dacacSDimitry Andric
11540b57cec5SDimitry Andrictemplate <class... _Types>
115506c3fb27SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
1156cb14a3feSDimitry Andric    : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value,
11570b57cec5SDimitry Andric                                  __all<is_move_constructible_v<_Types>...>::value>,
11580b57cec5SDimitry Andric      private __sfinae_assign_base<
1159cb14a3feSDimitry Andric          __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value,
1160cb14a3feSDimitry Andric          __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> {
1161cb14a3feSDimitry Andric  static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative.");
11620b57cec5SDimitry Andric
1163cb14a3feSDimitry Andric  static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative.");
11640b57cec5SDimitry Andric
1165cb14a3feSDimitry Andric  static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative.");
11660b57cec5SDimitry Andric
1167cb14a3feSDimitry Andric  static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative.");
11680b57cec5SDimitry Andric
11690b57cec5SDimitry Andric  using __first_type = variant_alternative_t<0, variant>;
11700b57cec5SDimitry Andric
11710b57cec5SDimitry Andricpublic:
11720b57cec5SDimitry Andric  template <bool _Dummy                                                                               = true,
1173cb14a3feSDimitry Andric            enable_if_t<__dependent_type<is_default_constructible<__first_type>, _Dummy>::value, int> = 0>
1174cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1175bdd1243dSDimitry Andric      : __impl_(in_place_index<0>) {}
11760b57cec5SDimitry Andric
117706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
117806c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&)      = default;
11790b57cec5SDimitry Andric
1180cb14a3feSDimitry Andric  template < class _Arg,
1181bdd1243dSDimitry Andric             enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int>        = 0,
1182bdd1243dSDimitry Andric             enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int>  = 0,
1183bdd1243dSDimitry Andric             enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
11840b57cec5SDimitry Andric             class _Tp  = __variant_detail::__best_match_t<_Arg, _Types...>,
1185cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
11860b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1187cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>)
11885f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
11890b57cec5SDimitry Andric
1190cb14a3feSDimitry Andric  template <size_t _Ip,
1191cb14a3feSDimitry Andric            class... _Args,
11920b57cec5SDimitry Andric            class                                               = enable_if_t<(_Ip < sizeof...(_Types)), int>,
11930b57cec5SDimitry Andric            class _Tp                                           = variant_alternative_t<_Ip, variant<_Types...>>,
11940b57cec5SDimitry Andric            enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1195cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept(
1196cb14a3feSDimitry Andric      is_nothrow_constructible_v<_Tp, _Args...>)
11975f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
11980b57cec5SDimitry Andric
1199cb14a3feSDimitry Andric  template < size_t _Ip,
12000b57cec5SDimitry Andric             class _Up,
12010b57cec5SDimitry Andric             class... _Args,
12020b57cec5SDimitry Andric             enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
12030b57cec5SDimitry Andric             class _Tp                                   = variant_alternative_t<_Ip, variant<_Types...>>,
1204cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1205cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
12060b57cec5SDimitry Andric      in_place_index_t<_Ip>,
12070b57cec5SDimitry Andric      initializer_list<_Up> __il,
1208cb14a3feSDimitry Andric      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
12095f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
12100b57cec5SDimitry Andric
1211cb14a3feSDimitry Andric  template < class _Tp,
12120b57cec5SDimitry Andric             class... _Args,
1213cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
12140b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1215cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
12160b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Args...>)
12175f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
12180b57cec5SDimitry Andric
1219cb14a3feSDimitry Andric  template < class _Tp,
12200b57cec5SDimitry Andric             class _Up,
12210b57cec5SDimitry Andric             class... _Args,
1222cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1223cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1224cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
12250b57cec5SDimitry Andric      in_place_type_t<_Tp>,
12260b57cec5SDimitry Andric      initializer_list<_Up> __il,
1227cb14a3feSDimitry Andric      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
12285f757f3fSDimitry Andric      : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
12290b57cec5SDimitry Andric
123006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI ~variant() = default;
12310b57cec5SDimitry Andric
123206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
123306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&)      = default;
12340b57cec5SDimitry Andric
1235cb14a3feSDimitry Andric  template < class _Arg,
1236bdd1243dSDimitry Andric             enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
12370b57cec5SDimitry Andric             class _Tp  = __variant_detail::__best_match_t<_Arg, _Types...>,
1238cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1239cb14a3feSDimitry Andric             enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0>
1240cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI variant&
1241cb14a3feSDimitry Andric  operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) {
12425f757f3fSDimitry Andric    __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
12430b57cec5SDimitry Andric    return *this;
12440b57cec5SDimitry Andric  }
12450b57cec5SDimitry Andric
1246cb14a3feSDimitry Andric  template < size_t _Ip,
12470b57cec5SDimitry Andric             class... _Args,
12480b57cec5SDimitry Andric             enable_if_t<(_Ip < sizeof...(_Types)), int>         = 0,
12490b57cec5SDimitry Andric             class _Tp                                           = variant_alternative_t<_Ip, variant<_Types...>>,
12500b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1251cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
12525f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
12530b57cec5SDimitry Andric  }
12540b57cec5SDimitry Andric
1255cb14a3feSDimitry Andric  template < size_t _Ip,
12560b57cec5SDimitry Andric             class _Up,
12570b57cec5SDimitry Andric             class... _Args,
12580b57cec5SDimitry Andric             enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
12590b57cec5SDimitry Andric             class _Tp                                   = variant_alternative_t<_Ip, variant<_Types...>>,
1260cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1261cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
12625f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
12630b57cec5SDimitry Andric  }
12640b57cec5SDimitry Andric
1265cb14a3feSDimitry Andric  template < class _Tp,
12660b57cec5SDimitry Andric             class... _Args,
1267cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
12680b57cec5SDimitry Andric             enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1269cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
12705f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
12710b57cec5SDimitry Andric  }
12720b57cec5SDimitry Andric
1273cb14a3feSDimitry Andric  template < class _Tp,
12740b57cec5SDimitry Andric             class _Up,
12750b57cec5SDimitry Andric             class... _Args,
1276cb14a3feSDimitry Andric             size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1277cb14a3feSDimitry Andric             enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1278cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
12795f757f3fSDimitry Andric    return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
12800b57cec5SDimitry Andric  }
12810b57cec5SDimitry Andric
1282cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
1283bdd1243dSDimitry Andric    return __impl_.valueless_by_exception();
12840b57cec5SDimitry Andric  }
12850b57cec5SDimitry Andric
1286cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
12870b57cec5SDimitry Andric
1288cb14a3feSDimitry Andric  template < bool _Dummy       = true,
1289cb14a3feSDimitry Andric             enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
12900b57cec5SDimitry Andric                                 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
12910b57cec5SDimitry Andric                          int> = 0>
1292cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void swap(variant& __that) noexcept(
1293cb14a3feSDimitry Andric      __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) {
1294bdd1243dSDimitry Andric    __impl_.__swap(__that.__impl_);
12950b57cec5SDimitry Andric  }
12960b57cec5SDimitry Andric
12977a6dacacSDimitry Andric#  if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
12987a6dacacSDimitry Andric  // Helper class to implement [variant.visit]/10
12997a6dacacSDimitry Andric  //   Constraints: The call to visit does not use an explicit template-argument-list
13007a6dacacSDimitry Andric  //   that begins with a type template-argument.
13017a6dacacSDimitry Andric  struct __variant_visit_barrier_tag {
13027a6dacacSDimitry Andric    _LIBCPP_HIDE_FROM_ABI explicit __variant_visit_barrier_tag() = default;
13037a6dacacSDimitry Andric  };
13047a6dacacSDimitry Andric
13057a6dacacSDimitry Andric  template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor>
13067a6dacacSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) {
13077a6dacacSDimitry Andric    using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
13087a6dacacSDimitry Andric    return std::visit(std::forward<_Visitor>(__visitor), (_VariantT)__self);
13097a6dacacSDimitry Andric  }
13107a6dacacSDimitry Andric
13117a6dacacSDimitry Andric  template <class _Rp, class _Self, class _Visitor>
13127a6dacacSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) {
13137a6dacacSDimitry Andric    using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
13147a6dacacSDimitry Andric    return std::visit<_Rp>(std::forward<_Visitor>(__visitor), (_VariantT)__self);
13157a6dacacSDimitry Andric  }
13167a6dacacSDimitry Andric#  endif
13177a6dacacSDimitry Andric
13180b57cec5SDimitry Andricprivate:
1319bdd1243dSDimitry Andric  __variant_detail::__impl<_Types...> __impl_;
13200b57cec5SDimitry Andric
13210b57cec5SDimitry Andric  friend struct __variant_detail::__access::__variant;
13220b57cec5SDimitry Andric  friend struct __variant_detail::__visitation::__variant;
13230b57cec5SDimitry Andric};
13240b57cec5SDimitry Andric
13250b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1326cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
13270b57cec5SDimitry Andric  return __v.index() == _Ip;
13280b57cec5SDimitry Andric}
13290b57cec5SDimitry Andric
13300b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1331cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1332bdd1243dSDimitry Andric  return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13330b57cec5SDimitry Andric}
13340b57cec5SDimitry Andric
13350b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
1336cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr auto&& __generic_get(_Vp&& __v) {
13370b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
1338bdd1243dSDimitry Andric  if (!std::__holds_alternative<_Ip>(__v)) {
13390b57cec5SDimitry Andric    __throw_bad_variant_access();
13400b57cec5SDimitry Andric  }
13415f757f3fSDimitry Andric  return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
13420b57cec5SDimitry Andric}
13430b57cec5SDimitry Andric
13440b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1345bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1346cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&
1347cb14a3feSDimitry Andric    get(variant<_Types...>& __v) {
13480b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13490b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1350bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(__v);
13510b57cec5SDimitry Andric}
13520b57cec5SDimitry Andric
13530b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1354bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1355cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
1356cb14a3feSDimitry Andric    get(variant<_Types...>&& __v) {
13570b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13580b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13595f757f3fSDimitry Andric  return std::__generic_get<_Ip>(std::move(__v));
13600b57cec5SDimitry Andric}
13610b57cec5SDimitry Andric
13620b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1363bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1364cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
1365cb14a3feSDimitry Andric    get(const variant<_Types...>& __v) {
13660b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13670b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1368bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(__v);
13690b57cec5SDimitry Andric}
13700b57cec5SDimitry Andric
13710b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1372bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1373cb14a3feSDimitry Andric    _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
1374cb14a3feSDimitry Andric    get(const variant<_Types...>&& __v) {
13750b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
13760b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13775f757f3fSDimitry Andric  return std::__generic_get<_Ip>(std::move(__v));
13780b57cec5SDimitry Andric}
13790b57cec5SDimitry Andric
13800b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1381cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp& get(variant<_Types...>& __v) {
13820b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
13835f757f3fSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13840b57cec5SDimitry Andric}
13850b57cec5SDimitry Andric
13860b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1387cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp&& get(variant<_Types...>&& __v) {
13880b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
1389cb14a3feSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
13900b57cec5SDimitry Andric}
13910b57cec5SDimitry Andric
13920b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1393cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&
1394cb14a3feSDimitry Andricget(const variant<_Types...>& __v) {
13950b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
13965f757f3fSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13970b57cec5SDimitry Andric}
13980b57cec5SDimitry Andric
13990b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1400cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&&
1401cb14a3feSDimitry Andricget(const variant<_Types...>&& __v) {
14020b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
1403cb14a3feSDimitry Andric  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
14040b57cec5SDimitry Andric}
14050b57cec5SDimitry Andric
14060b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
1407cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
14080b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
1409cb14a3feSDimitry Andric  return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr;
14100b57cec5SDimitry Andric}
14110b57cec5SDimitry Andric
14120b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1413cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
14140b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept {
14150b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
14160b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1417bdd1243dSDimitry Andric  return std::__generic_get_if<_Ip>(__v);
14180b57cec5SDimitry Andric}
14190b57cec5SDimitry Andric
14200b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1421cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
14220b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept {
14230b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
14240b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1425bdd1243dSDimitry Andric  return std::__generic_get_if<_Ip>(__v);
14260b57cec5SDimitry Andric}
14270b57cec5SDimitry Andric
14280b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1429cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
14300b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
14315f757f3fSDimitry Andric  return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
14320b57cec5SDimitry Andric}
14330b57cec5SDimitry Andric
14340b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1435cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
14360b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
14375f757f3fSDimitry Andric  return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
14380b57cec5SDimitry Andric}
14390b57cec5SDimitry Andric
14400b57cec5SDimitry Andrictemplate <class _Operator>
14410b57cec5SDimitry Andricstruct __convert_to_bool {
14420b57cec5SDimitry Andric  template <class _T1, class _T2>
1443cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const {
14445f757f3fSDimitry Andric    static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
14450b57cec5SDimitry Andric                  "the relational operator does not return a type which is implicitly convertible to bool");
14465f757f3fSDimitry Andric    return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
14470b57cec5SDimitry Andric  }
14480b57cec5SDimitry Andric};
14490b57cec5SDimitry Andric
14500b57cec5SDimitry Andrictemplate <class... _Types>
1451cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14520b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1453cb14a3feSDimitry Andric  if (__lhs.index() != __rhs.index())
1454cb14a3feSDimitry Andric    return false;
1455cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1456cb14a3feSDimitry Andric    return true;
14570b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
14580b57cec5SDimitry Andric}
14590b57cec5SDimitry Andric
146006c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
1461bdd1243dSDimitry Andric
1462cb14a3feSDimitry Andrictemplate <class... _Types>
1463cb14a3feSDimitry Andric  requires(three_way_comparable<_Types> && ...)
1464bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1465bdd1243dSDimitry Andricoperator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1466bdd1243dSDimitry Andric  using __variant_detail::__visitation::__variant;
1467bdd1243dSDimitry Andric  using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1468bdd1243dSDimitry Andric  if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1469bdd1243dSDimitry Andric    return strong_ordering::equal;
1470bdd1243dSDimitry Andric  if (__lhs.valueless_by_exception())
1471bdd1243dSDimitry Andric    return strong_ordering::less;
1472bdd1243dSDimitry Andric  if (__rhs.valueless_by_exception())
1473bdd1243dSDimitry Andric    return strong_ordering::greater;
1474bdd1243dSDimitry Andric  if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1475bdd1243dSDimitry Andric    return __c;
1476bdd1243dSDimitry Andric  auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1477bdd1243dSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1478bdd1243dSDimitry Andric}
1479bdd1243dSDimitry Andric
148006c3fb27SDimitry Andric#  endif // _LIBCPP_STD_VER >= 20
1481bdd1243dSDimitry Andric
14820b57cec5SDimitry Andrictemplate <class... _Types>
1483cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14840b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1485cb14a3feSDimitry Andric  if (__lhs.index() != __rhs.index())
1486cb14a3feSDimitry Andric    return true;
1487cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1488cb14a3feSDimitry Andric    return false;
1489cb14a3feSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
14900b57cec5SDimitry Andric}
14910b57cec5SDimitry Andric
14920b57cec5SDimitry Andrictemplate <class... _Types>
1493cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14940b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1495cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1496cb14a3feSDimitry Andric    return false;
1497cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1498cb14a3feSDimitry Andric    return true;
1499cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1500cb14a3feSDimitry Andric    return true;
1501cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1502cb14a3feSDimitry Andric    return false;
15030b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
15040b57cec5SDimitry Andric}
15050b57cec5SDimitry Andric
15060b57cec5SDimitry Andrictemplate <class... _Types>
1507cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
15080b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1509cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1510cb14a3feSDimitry Andric    return false;
1511cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1512cb14a3feSDimitry Andric    return true;
1513cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1514cb14a3feSDimitry Andric    return true;
1515cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1516cb14a3feSDimitry Andric    return false;
15170b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
15180b57cec5SDimitry Andric}
15190b57cec5SDimitry Andric
15200b57cec5SDimitry Andrictemplate <class... _Types>
1521cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
15220b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1523cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1524cb14a3feSDimitry Andric    return true;
1525cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1526cb14a3feSDimitry Andric    return false;
1527cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1528cb14a3feSDimitry Andric    return true;
1529cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1530cb14a3feSDimitry Andric    return false;
1531cb14a3feSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
15320b57cec5SDimitry Andric}
15330b57cec5SDimitry Andric
15340b57cec5SDimitry Andrictemplate <class... _Types>
1535cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
15360b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1537cb14a3feSDimitry Andric  if (__rhs.valueless_by_exception())
1538cb14a3feSDimitry Andric    return true;
1539cb14a3feSDimitry Andric  if (__lhs.valueless_by_exception())
1540cb14a3feSDimitry Andric    return false;
1541cb14a3feSDimitry Andric  if (__lhs.index() > __rhs.index())
1542cb14a3feSDimitry Andric    return true;
1543cb14a3feSDimitry Andric  if (__lhs.index() < __rhs.index())
1544cb14a3feSDimitry Andric    return false;
1545cb14a3feSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
15460b57cec5SDimitry Andric}
15470b57cec5SDimitry Andric
1548e8d8bef9SDimitry Andrictemplate <class... _Vs>
1549cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void __throw_if_valueless(_Vs&&... __vs) {
1550cb14a3feSDimitry Andric  const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception());
1551e8d8bef9SDimitry Andric  if (__valueless) {
1552e8d8bef9SDimitry Andric    __throw_bad_variant_access();
1553e8d8bef9SDimitry Andric  }
1554e8d8bef9SDimitry Andric}
1555e8d8bef9SDimitry Andric
15567a6dacacSDimitry Andrictemplate < class _Visitor, class... _Vs, typename>
1557cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
1558cb14a3feSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs) {
15590b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
15605f757f3fSDimitry Andric  std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1561cb14a3feSDimitry Andric  return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
15620b57cec5SDimitry Andric}
15630b57cec5SDimitry Andric
156406c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 20
15657a6dacacSDimitry Andrictemplate < class _Rp, class _Visitor, class... _Vs, typename>
1566cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
1567cb14a3feSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs) {
1568e8d8bef9SDimitry Andric  using __variant_detail::__visitation::__variant;
15695f757f3fSDimitry Andric  std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1570cb14a3feSDimitry Andric  return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1571e8d8bef9SDimitry Andric}
1572e8d8bef9SDimitry Andric#  endif
1573e8d8bef9SDimitry Andric
15740b57cec5SDimitry Andrictemplate <class... _Types>
1575cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI auto
1576cb14a3feSDimitry Andricswap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1577cb14a3feSDimitry Andric    -> decltype(__lhs.swap(__rhs)) {
1578cb14a3feSDimitry Andric  return __lhs.swap(__rhs);
1579cb14a3feSDimitry Andric}
15800b57cec5SDimitry Andric
15810b57cec5SDimitry Andrictemplate <class... _Types>
1582cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
15830b57cec5SDimitry Andric  using argument_type = variant<_Types...>;
15840b57cec5SDimitry Andric  using result_type   = size_t;
15850b57cec5SDimitry Andric
1586cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const {
15870b57cec5SDimitry Andric    using __variant_detail::__visitation::__variant;
15880b57cec5SDimitry Andric    size_t __res =
15890b57cec5SDimitry Andric        __v.valueless_by_exception()
15900b57cec5SDimitry Andric            ? 299792458 // Random value chosen by the universe upon creation
15910b57cec5SDimitry Andric            : __variant::__visit_alt(
15920b57cec5SDimitry Andric                  [](const auto& __alt) {
1593bdd1243dSDimitry Andric                    using __alt_type   = __remove_cvref_t<decltype(__alt)>;
1594cb14a3feSDimitry Andric                    using __value_type = remove_const_t< typename __alt_type::__value_type>;
15950b57cec5SDimitry Andric                    return hash<__value_type>{}(__alt.__value);
15960b57cec5SDimitry Andric                  },
15970b57cec5SDimitry Andric                  __v);
1598bdd1243dSDimitry Andric    return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
15990b57cec5SDimitry Andric  }
16000b57cec5SDimitry Andric};
16010b57cec5SDimitry Andric
1602fe6060f1SDimitry Andric// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1603fe6060f1SDimitry Andric// type whereas std::get will throw or returning nullptr. This makes it faster than
1604fe6060f1SDimitry Andric// std::get.
1605fe6060f1SDimitry Andrictemplate <size_t _Ip, class _Vp>
1606cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1607fe6060f1SDimitry Andric  using __variant_detail::__access::__variant;
16085f757f3fSDimitry Andric  return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
16090b57cec5SDimitry Andric}
1610fe6060f1SDimitry Andric
1611fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types>
1612cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1613bdd1243dSDimitry Andric  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1614fe6060f1SDimitry Andric}
1615fe6060f1SDimitry Andric
1616fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types>
1617cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1618bdd1243dSDimitry Andric  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1619fe6060f1SDimitry Andric}
16200b57cec5SDimitry Andric
162106c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 17
16220b57cec5SDimitry Andric
16230b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
16240b57cec5SDimitry Andric
16250b57cec5SDimitry Andric_LIBCPP_POP_MACROS
16260b57cec5SDimitry Andric
1627bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
162806c3fb27SDimitry Andric#  include <exception>
1629bdd1243dSDimitry Andric#  include <type_traits>
1630bdd1243dSDimitry Andric#  include <typeinfo>
1631bdd1243dSDimitry Andric#  include <utility>
1632bdd1243dSDimitry Andric#endif
1633bdd1243dSDimitry Andric
16340b57cec5SDimitry Andric#endif // _LIBCPP_VARIANT
1635