xref: /freebsd/contrib/llvm-project/libcxx/include/variant (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===------------------------------ variant -------------------------------===//
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);
250b57cec5SDimitry Andric    variant(const variant&);                // constexpr in C++20
260b57cec5SDimitry Andric    variant(variant&&) noexcept(see below); // constexpr in C++20
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
480b57cec5SDimitry Andric    variant& operator=(const variant&);                // constexpr in C++20
490b57cec5SDimitry Andric    variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric    template <class T> variant& operator=(T&&) noexcept(see below);
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric    // 20.7.2.4, modifiers
540b57cec5SDimitry Andric    template <class T, class... Args>
550b57cec5SDimitry Andric    T& emplace(Args&&...);
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric    template <class T, class U, class... Args>
580b57cec5SDimitry Andric    T& emplace(initializer_list<U>, Args&&...);
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric    template <size_t I, class... Args>
610b57cec5SDimitry Andric    variant_alternative_t<I, variant>& emplace(Args&&...);
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric    template <size_t I, class U, class...  Args>
640b57cec5SDimitry Andric    variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric    // 20.7.2.5, value status
670b57cec5SDimitry Andric    constexpr bool valueless_by_exception() const noexcept;
680b57cec5SDimitry Andric    constexpr size_t index() const noexcept;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric    // 20.7.2.6, swap
710b57cec5SDimitry Andric    void swap(variant&) noexcept(see below);
720b57cec5SDimitry Andric  };
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric  // 20.7.3, variant helper classes
750b57cec5SDimitry Andric  template <class T> struct variant_size; // undefined
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric  template <class T>
780b57cec5SDimitry Andric  inline constexpr size_t variant_size_v = variant_size<T>::value;
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric  template <class T> struct variant_size<const T>;
810b57cec5SDimitry Andric  template <class T> struct variant_size<volatile T>;
820b57cec5SDimitry Andric  template <class T> struct variant_size<const volatile T>;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric  template <class... Types>
850b57cec5SDimitry Andric  struct variant_size<variant<Types...>>;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative; // undefined
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric  template <size_t I, class T>
900b57cec5SDimitry Andric  using variant_alternative_t = typename variant_alternative<I, T>::type;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const T>;
930b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, volatile T>;
940b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const volatile T>;
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric  template <size_t I, class... Types>
970b57cec5SDimitry Andric  struct variant_alternative<I, variant<Types...>>;
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric  inline constexpr size_t variant_npos = -1;
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric  // 20.7.4, value access
1020b57cec5SDimitry Andric  template <class T, class... Types>
1030b57cec5SDimitry Andric  constexpr bool holds_alternative(const variant<Types...>&) noexcept;
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric  template <size_t I, class... Types>
1060b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&
1070b57cec5SDimitry Andric  get(variant<Types...>&);
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric  template <size_t I, class... Types>
1100b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&&
1110b57cec5SDimitry Andric  get(variant<Types...>&&);
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric  template <size_t I, class... Types>
1140b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&
1150b57cec5SDimitry Andric  get(const variant<Types...>&);
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric  template <size_t I, class... Types>
1180b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&&
1190b57cec5SDimitry Andric  get(const variant<Types...>&&);
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric  template <class T, class...  Types>
1220b57cec5SDimitry Andric  constexpr T& get(variant<Types...>&);
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric  template <class T, class... Types>
1250b57cec5SDimitry Andric  constexpr T&& get(variant<Types...>&&);
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric  template <class T, class... Types>
1280b57cec5SDimitry Andric  constexpr const T& get(const variant<Types...>&);
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric  template <class T, class... Types>
1310b57cec5SDimitry Andric  constexpr const T&& get(const variant<Types...>&&);
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric  template <size_t I, class... Types>
1340b57cec5SDimitry Andric  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
1350b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric  template <size_t I, class... Types>
1380b57cec5SDimitry Andric  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
1390b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric  template <class T, class... Types>
1420b57cec5SDimitry Andric  constexpr add_pointer_t<T>
1430b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric  template <class T, class... Types>
1460b57cec5SDimitry Andric  constexpr add_pointer_t<const T>
1470b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric  // 20.7.5, relational operators
1500b57cec5SDimitry Andric  template <class... Types>
1510b57cec5SDimitry Andric  constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric  template <class... Types>
1540b57cec5SDimitry Andric  constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric  template <class... Types>
1570b57cec5SDimitry Andric  constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric  template <class... Types>
1600b57cec5SDimitry Andric  constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric  template <class... Types>
1630b57cec5SDimitry Andric  constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric  template <class... Types>
1660b57cec5SDimitry Andric  constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric  // 20.7.6, visitation
1690b57cec5SDimitry Andric  template <class Visitor, class... Variants>
1700b57cec5SDimitry Andric  constexpr see below visit(Visitor&&, Variants&&...);
1710b57cec5SDimitry Andric
172*e8d8bef9SDimitry Andric  template <class R, class Visitor, class... Variants>
173*e8d8bef9SDimitry Andric  constexpr R visit(Visitor&&, Variants&&...); // since C++20
174*e8d8bef9SDimitry Andric
1750b57cec5SDimitry Andric  // 20.7.7, class monostate
1760b57cec5SDimitry Andric  struct monostate;
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric  // 20.7.8, monostate relational operators
1790b57cec5SDimitry Andric  constexpr bool operator<(monostate, monostate) noexcept;
1800b57cec5SDimitry Andric  constexpr bool operator>(monostate, monostate) noexcept;
1810b57cec5SDimitry Andric  constexpr bool operator<=(monostate, monostate) noexcept;
1820b57cec5SDimitry Andric  constexpr bool operator>=(monostate, monostate) noexcept;
1830b57cec5SDimitry Andric  constexpr bool operator==(monostate, monostate) noexcept;
1840b57cec5SDimitry Andric  constexpr bool operator!=(monostate, monostate) noexcept;
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric  // 20.7.9, specialized algorithms
1870b57cec5SDimitry Andric  template <class... Types>
1880b57cec5SDimitry Andric  void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric  // 20.7.10, class bad_variant_access
1910b57cec5SDimitry Andric  class bad_variant_access;
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric  // 20.7.11, hash support
1940b57cec5SDimitry Andric  template <class T> struct hash;
1950b57cec5SDimitry Andric  template <class... Types> struct hash<variant<Types...>>;
1960b57cec5SDimitry Andric  template <> struct hash<monostate>;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric} // namespace std
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric*/
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric#include <__config>
203*e8d8bef9SDimitry Andric#include <__availability>
2040b57cec5SDimitry Andric#include <__tuple>
2050b57cec5SDimitry Andric#include <array>
2060b57cec5SDimitry Andric#include <exception>
2070b57cec5SDimitry Andric#include <functional>
2080b57cec5SDimitry Andric#include <initializer_list>
2090b57cec5SDimitry Andric#include <new>
2100b57cec5SDimitry Andric#include <tuple>
2110b57cec5SDimitry Andric#include <type_traits>
2120b57cec5SDimitry Andric#include <utility>
2130b57cec5SDimitry Andric#include <limits>
2140b57cec5SDimitry Andric#include <version>
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2170b57cec5SDimitry Andric#pragma GCC system_header
2180b57cec5SDimitry Andric#endif
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
2210b57cec5SDimitry Andric#include <__undef_macros>
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andricnamespace std { // explicitly not using versioning namespace
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
2260b57cec5SDimitry Andricpublic:
2270b57cec5SDimitry Andric  virtual const char* what() const _NOEXCEPT;
2280b57cec5SDimitry Andric};
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric} // namespace std
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2330b57cec5SDimitry Andric
234*e8d8bef9SDimitry Andric// TODO: GCC 5 lies about its support for C++17 (it says it supports it but it
235*e8d8bef9SDimitry Andric//       really doesn't). That breaks variant, which uses some C++17 features.
236*e8d8bef9SDimitry Andric//       Remove this once we drop support for GCC 5.
237*e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 14 && !(defined(_LIBCPP_COMPILER_GCC) && _GNUC_VER_NEW < 6000)
2380b57cec5SDimitry Andric
2390b57cec5SDimitry Andric_LIBCPP_NORETURN
2400b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2410b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
2420b57cec5SDimitry Andricvoid __throw_bad_variant_access() {
2430b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
2440b57cec5SDimitry Andric        throw bad_variant_access();
2450b57cec5SDimitry Andric#else
2460b57cec5SDimitry Andric        _VSTD::abort();
2470b57cec5SDimitry Andric#endif
2480b57cec5SDimitry Andric}
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andrictemplate <class... _Types>
2510b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant;
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andrictemplate <class _Tp>
2540b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size;
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andrictemplate <class _Tp>
2570b57cec5SDimitry Andric_LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andrictemplate <class _Tp>
2600b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
2610b57cec5SDimitry Andric
2620b57cec5SDimitry Andrictemplate <class _Tp>
2630b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andrictemplate <class _Tp>
2660b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
2670b57cec5SDimitry Andric    : variant_size<_Tp> {};
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andrictemplate <class... _Types>
2700b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
2710b57cec5SDimitry Andric    : integral_constant<size_t, sizeof...(_Types)> {};
2720b57cec5SDimitry Andric
2730b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
2740b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative;
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
2770b57cec5SDimitry Andricusing variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
2800b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
2810b57cec5SDimitry Andric    : add_const<variant_alternative_t<_Ip, _Tp>> {};
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
2840b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
2850b57cec5SDimitry Andric    : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
2860b57cec5SDimitry Andric
2870b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
2880b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
2890b57cec5SDimitry Andric    : add_cv<variant_alternative_t<_Ip, _Tp>> {};
2900b57cec5SDimitry Andric
2910b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
2920b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
2930b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
2940b57cec5SDimitry Andric  using type = __type_pack_element<_Ip, _Types...>;
2950b57cec5SDimitry Andric};
2960b57cec5SDimitry Andric
2970b57cec5SDimitry Andric_LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andricconstexpr int __choose_index_type(unsigned int __num_elem) {
300*e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned char>::max())
3010b57cec5SDimitry Andric    return 0;
302*e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned short>::max())
3030b57cec5SDimitry Andric    return 1;
3040b57cec5SDimitry Andric  return 2;
3050b57cec5SDimitry Andric}
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andrictemplate <size_t _NumAlts>
3080b57cec5SDimitry Andricusing __variant_index_t =
3090b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
3100b57cec5SDimitry Andric  unsigned int;
3110b57cec5SDimitry Andric#else
3120b57cec5SDimitry Andric  std::tuple_element_t<
3130b57cec5SDimitry Andric      __choose_index_type(_NumAlts),
3140b57cec5SDimitry Andric      std::tuple<unsigned char, unsigned short, unsigned int>
3150b57cec5SDimitry Andric  >;
3160b57cec5SDimitry Andric#endif
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andrictemplate <class _IndexType>
3190b57cec5SDimitry Andricconstexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
3200b57cec5SDimitry Andric
3210b57cec5SDimitry Andricnamespace __find_detail {
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
3240b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3250b57cec5SDimitry Andricconstexpr size_t __find_index() {
3260b57cec5SDimitry Andric  constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
3270b57cec5SDimitry Andric  size_t __result = __not_found;
3280b57cec5SDimitry Andric  for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
3290b57cec5SDimitry Andric    if (__matches[__i]) {
3300b57cec5SDimitry Andric      if (__result != __not_found) {
3310b57cec5SDimitry Andric        return __ambiguous;
3320b57cec5SDimitry Andric      }
3330b57cec5SDimitry Andric      __result = __i;
3340b57cec5SDimitry Andric    }
3350b57cec5SDimitry Andric  }
3360b57cec5SDimitry Andric  return __result;
3370b57cec5SDimitry Andric}
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andrictemplate <size_t _Index>
3400b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl
3410b57cec5SDimitry Andric    : integral_constant<size_t, _Index> {};
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andrictemplate <>
3440b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__not_found> {};
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andrictemplate <>
3470b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
3500b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae
3510b57cec5SDimitry Andric    : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andric} // namespace __find_detail
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andricnamespace __variant_detail {
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andricstruct __valueless_t {};
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andricenum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andrictemplate <typename _Tp,
3620b57cec5SDimitry Andric          template <typename> class _IsTriviallyAvailable,
3630b57cec5SDimitry Andric          template <typename> class _IsAvailable>
3640b57cec5SDimitry Andricconstexpr _Trait __trait =
3650b57cec5SDimitry Andric    _IsTriviallyAvailable<_Tp>::value
3660b57cec5SDimitry Andric        ? _Trait::_TriviallyAvailable
3670b57cec5SDimitry Andric        : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3700b57cec5SDimitry Andricconstexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
3710b57cec5SDimitry Andric  _Trait __result = _Trait::_TriviallyAvailable;
3720b57cec5SDimitry Andric  for (_Trait __t : __traits) {
3730b57cec5SDimitry Andric    if (static_cast<int>(__t) > static_cast<int>(__result)) {
3740b57cec5SDimitry Andric      __result = __t;
3750b57cec5SDimitry Andric    }
3760b57cec5SDimitry Andric  }
3770b57cec5SDimitry Andric  return __result;
3780b57cec5SDimitry Andric}
3790b57cec5SDimitry Andric
3800b57cec5SDimitry Andrictemplate <typename... _Types>
3810b57cec5SDimitry Andricstruct __traits {
3820b57cec5SDimitry Andric  static constexpr _Trait __copy_constructible_trait =
3830b57cec5SDimitry Andric      __common_trait({__trait<_Types,
3840b57cec5SDimitry Andric                              is_trivially_copy_constructible,
3850b57cec5SDimitry Andric                              is_copy_constructible>...});
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andric  static constexpr _Trait __move_constructible_trait =
3880b57cec5SDimitry Andric      __common_trait({__trait<_Types,
3890b57cec5SDimitry Andric                              is_trivially_move_constructible,
3900b57cec5SDimitry Andric                              is_move_constructible>...});
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric  static constexpr _Trait __copy_assignable_trait = __common_trait(
3930b57cec5SDimitry Andric      {__copy_constructible_trait,
3940b57cec5SDimitry Andric       __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric  static constexpr _Trait __move_assignable_trait = __common_trait(
3970b57cec5SDimitry Andric      {__move_constructible_trait,
3980b57cec5SDimitry Andric       __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
3990b57cec5SDimitry Andric
4000b57cec5SDimitry Andric  static constexpr _Trait __destructible_trait = __common_trait(
4010b57cec5SDimitry Andric      {__trait<_Types, is_trivially_destructible, is_destructible>...});
4020b57cec5SDimitry Andric};
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andricnamespace __access {
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andricstruct __union {
4070b57cec5SDimitry Andric  template <class _Vp>
4080b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4090b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
4100b57cec5SDimitry Andric    return _VSTD::forward<_Vp>(__v).__head;
4110b57cec5SDimitry Andric  }
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric  template <class _Vp, size_t _Ip>
4140b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4150b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
4160b57cec5SDimitry Andric    return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
4170b57cec5SDimitry Andric  }
4180b57cec5SDimitry Andric};
4190b57cec5SDimitry Andric
4200b57cec5SDimitry Andricstruct __base {
4210b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
4220b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4230b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v) {
4240b57cec5SDimitry Andric    return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
4250b57cec5SDimitry Andric                              in_place_index<_Ip>);
4260b57cec5SDimitry Andric  }
4270b57cec5SDimitry Andric};
4280b57cec5SDimitry Andric
4290b57cec5SDimitry Andricstruct __variant {
4300b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
4310b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4320b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v) {
4330b57cec5SDimitry Andric    return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
4340b57cec5SDimitry Andric  }
4350b57cec5SDimitry Andric};
4360b57cec5SDimitry Andric
4370b57cec5SDimitry Andric} // namespace __access
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andricnamespace __visitation {
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andricstruct __base {
4420b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
4430b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4440b57cec5SDimitry Andric  static constexpr decltype(auto)
4450b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
4460b57cec5SDimitry Andric    constexpr auto __fdiagonal =
4470b57cec5SDimitry Andric        __make_fdiagonal<_Visitor&&,
4480b57cec5SDimitry Andric                         decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
4490b57cec5SDimitry Andric    return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
4500b57cec5SDimitry Andric                                _VSTD::forward<_Vs>(__vs).__as_base()...);
4510b57cec5SDimitry Andric  }
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
4540b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4550b57cec5SDimitry Andric  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
4560b57cec5SDimitry Andric                                              _Vs&&... __vs) {
4570b57cec5SDimitry Andric    constexpr auto __fmatrix =
4580b57cec5SDimitry Andric        __make_fmatrix<_Visitor&&,
4590b57cec5SDimitry Andric                       decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
4600b57cec5SDimitry Andric    return __at(__fmatrix, __vs.index()...)(
4610b57cec5SDimitry Andric        _VSTD::forward<_Visitor>(__visitor),
4620b57cec5SDimitry Andric        _VSTD::forward<_Vs>(__vs).__as_base()...);
4630b57cec5SDimitry Andric  }
4640b57cec5SDimitry Andric
4650b57cec5SDimitry Andricprivate:
4660b57cec5SDimitry Andric  template <class _Tp>
4670b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4680b57cec5SDimitry Andric  static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
4690b57cec5SDimitry Andric
4700b57cec5SDimitry Andric  template <class _Tp, size_t _Np, typename... _Indices>
4710b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4720b57cec5SDimitry Andric  static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
4730b57cec5SDimitry Andric                               size_t __index, _Indices... __indices) {
4740b57cec5SDimitry Andric    return __at(__elems[__index], __indices...);
4750b57cec5SDimitry Andric  }
4760b57cec5SDimitry Andric
4770b57cec5SDimitry Andric  template <class _Fp, class... _Fs>
4780b57cec5SDimitry Andric  static constexpr void __std_visit_visitor_return_type_check() {
4790b57cec5SDimitry Andric    static_assert(
4800b57cec5SDimitry Andric        __all<is_same_v<_Fp, _Fs>...>::value,
4810b57cec5SDimitry Andric        "`std::visit` requires the visitor to have a single return type.");
4820b57cec5SDimitry Andric  }
4830b57cec5SDimitry Andric
4840b57cec5SDimitry Andric  template <class... _Fs>
4850b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
4860b57cec5SDimitry Andric  static constexpr auto __make_farray(_Fs&&... __fs) {
4870b57cec5SDimitry Andric    __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
4880b57cec5SDimitry Andric    using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
4890b57cec5SDimitry Andric    return __result{{_VSTD::forward<_Fs>(__fs)...}};
4900b57cec5SDimitry Andric  }
4910b57cec5SDimitry Andric
492*e8d8bef9SDimitry Andric  template <size_t... _Is>
4930b57cec5SDimitry Andric  struct __dispatcher {
4940b57cec5SDimitry Andric    template <class _Fp, class... _Vs>
4950b57cec5SDimitry Andric    inline _LIBCPP_INLINE_VISIBILITY
4960b57cec5SDimitry Andric    static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
497*e8d8bef9SDimitry Andric        return _VSTD::__invoke_constexpr(
4980b57cec5SDimitry Andric            static_cast<_Fp>(__f),
4990b57cec5SDimitry Andric            __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
5000b57cec5SDimitry Andric    }
5010b57cec5SDimitry Andric  };
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
5040b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5050b57cec5SDimitry Andric  static constexpr auto __make_dispatch(index_sequence<_Is...>) {
5060b57cec5SDimitry Andric    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
5070b57cec5SDimitry Andric  }
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric  template <size_t _Ip, class _Fp, class... _Vs>
5100b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5110b57cec5SDimitry Andric  static constexpr auto __make_fdiagonal_impl() {
5120b57cec5SDimitry Andric    return __make_dispatch<_Fp, _Vs...>(
5130b57cec5SDimitry Andric        index_sequence<(__identity<_Vs>{}, _Ip)...>{});
5140b57cec5SDimitry Andric  }
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
5170b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5180b57cec5SDimitry Andric  static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
5190b57cec5SDimitry Andric    return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
5200b57cec5SDimitry Andric  }
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric  template <class _Fp, class _Vp, class... _Vs>
5230b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5240b57cec5SDimitry Andric  static constexpr auto __make_fdiagonal() {
5250b57cec5SDimitry Andric    constexpr size_t _Np = __uncvref_t<_Vp>::__size();
5260b57cec5SDimitry Andric    static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
5270b57cec5SDimitry Andric    return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
5280b57cec5SDimitry Andric  }
5290b57cec5SDimitry Andric
5300b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
5310b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5320b57cec5SDimitry Andric  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
5330b57cec5SDimitry Andric    return __make_dispatch<_Fp, _Vs...>(__is);
5340b57cec5SDimitry Andric  }
5350b57cec5SDimitry Andric
5360b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
5370b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5380b57cec5SDimitry Andric  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
5390b57cec5SDimitry Andric                                            index_sequence<_Js...>,
5400b57cec5SDimitry Andric                                            _Ls... __ls) {
5410b57cec5SDimitry Andric    return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
5420b57cec5SDimitry Andric        index_sequence<_Is..., _Js>{}, __ls...)...);
5430b57cec5SDimitry Andric  }
5440b57cec5SDimitry Andric
5450b57cec5SDimitry Andric  template <class _Fp, class... _Vs>
5460b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5470b57cec5SDimitry Andric  static constexpr auto __make_fmatrix() {
5480b57cec5SDimitry Andric    return __make_fmatrix_impl<_Fp, _Vs...>(
5490b57cec5SDimitry Andric        index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
5500b57cec5SDimitry Andric  }
5510b57cec5SDimitry Andric};
5520b57cec5SDimitry Andric
5530b57cec5SDimitry Andricstruct __variant {
5540b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
5550b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5560b57cec5SDimitry Andric  static constexpr decltype(auto)
5570b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
5580b57cec5SDimitry Andric    return __base::__visit_alt_at(__index,
5590b57cec5SDimitry Andric                                  _VSTD::forward<_Visitor>(__visitor),
5600b57cec5SDimitry Andric                                  _VSTD::forward<_Vs>(__vs).__impl...);
5610b57cec5SDimitry Andric  }
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
5640b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5650b57cec5SDimitry Andric  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
5660b57cec5SDimitry Andric                                              _Vs&&... __vs) {
5670b57cec5SDimitry Andric    return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
5680b57cec5SDimitry Andric                               _VSTD::forward<_Vs>(__vs).__impl...);
5690b57cec5SDimitry Andric  }
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
5720b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5730b57cec5SDimitry Andric  static constexpr decltype(auto)
5740b57cec5SDimitry Andric  __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
5750b57cec5SDimitry Andric    return __visit_alt_at(
5760b57cec5SDimitry Andric        __index,
5770b57cec5SDimitry Andric        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
5780b57cec5SDimitry Andric        _VSTD::forward<_Vs>(__vs)...);
5790b57cec5SDimitry Andric  }
5800b57cec5SDimitry Andric
5810b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
5820b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
5830b57cec5SDimitry Andric  static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
5840b57cec5SDimitry Andric                                                _Vs&&... __vs) {
5850b57cec5SDimitry Andric    return __visit_alt(
5860b57cec5SDimitry Andric        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
5870b57cec5SDimitry Andric        _VSTD::forward<_Vs>(__vs)...);
5880b57cec5SDimitry Andric  }
589*e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
590*e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor, class... _Vs>
591*e8d8bef9SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
592*e8d8bef9SDimitry Andric  static constexpr _Rp __visit_value(_Visitor&& __visitor,
593*e8d8bef9SDimitry Andric                                     _Vs&&... __vs) {
594*e8d8bef9SDimitry Andric    return __visit_alt(
595*e8d8bef9SDimitry Andric        __make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)),
596*e8d8bef9SDimitry Andric        _VSTD::forward<_Vs>(__vs)...);
597*e8d8bef9SDimitry Andric  }
598*e8d8bef9SDimitry Andric#endif
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andricprivate:
6010b57cec5SDimitry Andric  template <class _Visitor, class... _Values>
6020b57cec5SDimitry Andric  static constexpr void __std_visit_exhaustive_visitor_check() {
6030b57cec5SDimitry Andric    static_assert(is_invocable_v<_Visitor, _Values...>,
6040b57cec5SDimitry Andric                  "`std::visit` requires the visitor to be exhaustive.");
6050b57cec5SDimitry Andric  }
6060b57cec5SDimitry Andric
6070b57cec5SDimitry Andric  template <class _Visitor>
6080b57cec5SDimitry Andric  struct __value_visitor {
6090b57cec5SDimitry Andric    template <class... _Alts>
6100b57cec5SDimitry Andric    inline _LIBCPP_INLINE_VISIBILITY
6110b57cec5SDimitry Andric    constexpr decltype(auto) operator()(_Alts&&... __alts) const {
6120b57cec5SDimitry Andric      __std_visit_exhaustive_visitor_check<
6130b57cec5SDimitry Andric          _Visitor,
6140b57cec5SDimitry Andric          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
615*e8d8bef9SDimitry Andric      return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
6160b57cec5SDimitry Andric                                _VSTD::forward<_Alts>(__alts).__value...);
6170b57cec5SDimitry Andric    }
6180b57cec5SDimitry Andric    _Visitor&& __visitor;
6190b57cec5SDimitry Andric  };
6200b57cec5SDimitry Andric
621*e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
622*e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
623*e8d8bef9SDimitry Andric  struct __value_visitor_return_type {
624*e8d8bef9SDimitry Andric    template <class... _Alts>
625*e8d8bef9SDimitry Andric    inline _LIBCPP_INLINE_VISIBILITY
626*e8d8bef9SDimitry Andric    constexpr _Rp operator()(_Alts&&... __alts) const {
627*e8d8bef9SDimitry Andric      __std_visit_exhaustive_visitor_check<
628*e8d8bef9SDimitry Andric          _Visitor,
629*e8d8bef9SDimitry Andric          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
630*e8d8bef9SDimitry Andric      if constexpr (is_void_v<_Rp>) {
631*e8d8bef9SDimitry Andric        _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
632*e8d8bef9SDimitry Andric                                  _VSTD::forward<_Alts>(__alts).__value...);
633*e8d8bef9SDimitry Andric      }
634*e8d8bef9SDimitry Andric      else {
635*e8d8bef9SDimitry Andric        return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
636*e8d8bef9SDimitry Andric                                         _VSTD::forward<_Alts>(__alts).__value...);
637*e8d8bef9SDimitry Andric      }
638*e8d8bef9SDimitry Andric    }
639*e8d8bef9SDimitry Andric
640*e8d8bef9SDimitry Andric    _Visitor&& __visitor;
641*e8d8bef9SDimitry Andric  };
642*e8d8bef9SDimitry Andric#endif
643*e8d8bef9SDimitry Andric
6440b57cec5SDimitry Andric  template <class _Visitor>
6450b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
6460b57cec5SDimitry Andric  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
6470b57cec5SDimitry Andric    return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
6480b57cec5SDimitry Andric  }
649*e8d8bef9SDimitry Andric
650*e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
651*e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
652*e8d8bef9SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
653*e8d8bef9SDimitry Andric  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
654*e8d8bef9SDimitry Andric    return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)};
655*e8d8bef9SDimitry Andric  }
656*e8d8bef9SDimitry Andric#endif
6570b57cec5SDimitry Andric};
6580b57cec5SDimitry Andric
6590b57cec5SDimitry Andric} // namespace __visitation
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andrictemplate <size_t _Index, class _Tp>
6620b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __alt {
6630b57cec5SDimitry Andric  using __value_type = _Tp;
6640b57cec5SDimitry Andric
6650b57cec5SDimitry Andric  template <class... _Args>
6660b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
6670b57cec5SDimitry Andric  explicit constexpr __alt(in_place_t, _Args&&... __args)
6680b57cec5SDimitry Andric      : __value(_VSTD::forward<_Args>(__args)...) {}
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andric  __value_type __value;
6710b57cec5SDimitry Andric};
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index, class... _Types>
6740b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union;
6750b57cec5SDimitry Andric
6760b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index>
6770b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
6780b57cec5SDimitry Andric
6790b57cec5SDimitry Andric#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor)                  \
6800b57cec5SDimitry Andric  template <size_t _Index, class _Tp, class... _Types>                         \
6810b57cec5SDimitry Andric  union _LIBCPP_TEMPLATE_VIS __union<destructible_trait,                      \
6820b57cec5SDimitry Andric                                      _Index,                                  \
6830b57cec5SDimitry Andric                                      _Tp,                                     \
6840b57cec5SDimitry Andric                                      _Types...> {                             \
6850b57cec5SDimitry Andric  public:                                                                      \
6860b57cec5SDimitry Andric    inline _LIBCPP_INLINE_VISIBILITY                                           \
6870b57cec5SDimitry Andric    explicit constexpr __union(__valueless_t) noexcept : __dummy{} {}          \
6880b57cec5SDimitry Andric                                                                               \
6890b57cec5SDimitry Andric    template <class... _Args>                                                  \
6900b57cec5SDimitry Andric    inline _LIBCPP_INLINE_VISIBILITY                                           \
6910b57cec5SDimitry Andric    explicit constexpr __union(in_place_index_t<0>, _Args&&... __args)         \
6920b57cec5SDimitry Andric        : __head(in_place, _VSTD::forward<_Args>(__args)...) {}                \
6930b57cec5SDimitry Andric                                                                               \
6940b57cec5SDimitry Andric    template <size_t _Ip, class... _Args>                                      \
6950b57cec5SDimitry Andric    inline _LIBCPP_INLINE_VISIBILITY                                           \
6960b57cec5SDimitry Andric    explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args)       \
6970b57cec5SDimitry Andric        : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
6980b57cec5SDimitry Andric                                                                               \
6990b57cec5SDimitry Andric    __union(const __union&) = default;                                         \
7000b57cec5SDimitry Andric    __union(__union&&) = default;                                              \
7010b57cec5SDimitry Andric                                                                               \
7020b57cec5SDimitry Andric    destructor                                                                 \
7030b57cec5SDimitry Andric                                                                               \
7040b57cec5SDimitry Andric    __union& operator=(const __union&) = default;                              \
7050b57cec5SDimitry Andric    __union& operator=(__union&&) = default;                                   \
7060b57cec5SDimitry Andric                                                                               \
7070b57cec5SDimitry Andric  private:                                                                     \
7080b57cec5SDimitry Andric    char __dummy;                                                              \
7090b57cec5SDimitry Andric    __alt<_Index, _Tp> __head;                                                 \
7100b57cec5SDimitry Andric    __union<destructible_trait, _Index + 1, _Types...> __tail;                 \
7110b57cec5SDimitry Andric                                                                               \
7120b57cec5SDimitry Andric    friend struct __access::__union;                                           \
7130b57cec5SDimitry Andric  }
7140b57cec5SDimitry Andric
7150b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
7160b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
7170b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
7180b57cec5SDimitry Andric
7190b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_UNION
7200b57cec5SDimitry Andric
7210b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, class... _Types>
7220b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __base {
7230b57cec5SDimitry Andricpublic:
7240b57cec5SDimitry Andric  using __index_t = __variant_index_t<sizeof...(_Types)>;
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7270b57cec5SDimitry Andric  explicit constexpr __base(__valueless_t tag) noexcept
7280b57cec5SDimitry Andric      : __data(tag), __index(__variant_npos<__index_t>) {}
7290b57cec5SDimitry Andric
7300b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
7310b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7320b57cec5SDimitry Andric  explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
7330b57cec5SDimitry Andric      :
7340b57cec5SDimitry Andric        __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
7350b57cec5SDimitry Andric        __index(_Ip) {}
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7380b57cec5SDimitry Andric  constexpr bool valueless_by_exception() const noexcept {
7390b57cec5SDimitry Andric    return index() == variant_npos;
7400b57cec5SDimitry Andric  }
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7430b57cec5SDimitry Andric  constexpr size_t index() const noexcept {
7440b57cec5SDimitry Andric    return __index == __variant_npos<__index_t> ? variant_npos : __index;
7450b57cec5SDimitry Andric  }
7460b57cec5SDimitry Andric
7470b57cec5SDimitry Andricprotected:
7480b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7490b57cec5SDimitry Andric  constexpr auto&& __as_base() & { return *this; }
7500b57cec5SDimitry Andric
7510b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7520b57cec5SDimitry Andric  constexpr auto&& __as_base() && { return _VSTD::move(*this); }
7530b57cec5SDimitry Andric
7540b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7550b57cec5SDimitry Andric  constexpr auto&& __as_base() const & { return *this; }
7560b57cec5SDimitry Andric
7570b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7580b57cec5SDimitry Andric  constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
7590b57cec5SDimitry Andric
7600b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
7610b57cec5SDimitry Andric  static constexpr size_t __size() { return sizeof...(_Types); }
7620b57cec5SDimitry Andric
7630b57cec5SDimitry Andric  __union<_DestructibleTrait, 0, _Types...> __data;
7640b57cec5SDimitry Andric  __index_t __index;
7650b57cec5SDimitry Andric
7660b57cec5SDimitry Andric  friend struct __access::__base;
7670b57cec5SDimitry Andric  friend struct __visitation::__base;
7680b57cec5SDimitry Andric};
7690b57cec5SDimitry Andric
7700b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__destructible_trait>
771*e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __dtor;
7720b57cec5SDimitry Andric
7730b57cec5SDimitry Andric#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy)    \
7740b57cec5SDimitry Andric  template <class... _Types>                                                   \
775*e8d8bef9SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>,                       \
7760b57cec5SDimitry Andric                                    destructible_trait>                        \
7770b57cec5SDimitry Andric      : public __base<destructible_trait, _Types...> {                         \
7780b57cec5SDimitry Andric    using __base_type = __base<destructible_trait, _Types...>;                 \
7790b57cec5SDimitry Andric    using __index_t = typename __base_type::__index_t;                         \
7800b57cec5SDimitry Andric                                                                               \
7810b57cec5SDimitry Andric  public:                                                                      \
7820b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
7830b57cec5SDimitry Andric    using __base_type::operator=;                                              \
7840b57cec5SDimitry Andric                                                                               \
785*e8d8bef9SDimitry Andric    __dtor(const __dtor&) = default;                                           \
786*e8d8bef9SDimitry Andric    __dtor(__dtor&&) = default;                                                \
7870b57cec5SDimitry Andric    destructor                                                                 \
788*e8d8bef9SDimitry Andric    __dtor& operator=(const __dtor&) = default;                                \
789*e8d8bef9SDimitry Andric    __dtor& operator=(__dtor&&) = default;                                     \
7900b57cec5SDimitry Andric                                                                               \
7910b57cec5SDimitry Andric  protected:                                                                   \
7920b57cec5SDimitry Andric    inline _LIBCPP_INLINE_VISIBILITY                                           \
7930b57cec5SDimitry Andric    destroy                                                                    \
7940b57cec5SDimitry Andric  }
7950b57cec5SDimitry Andric
7960b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
7970b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
798*e8d8bef9SDimitry Andric    ~__dtor() = default;,
7990b57cec5SDimitry Andric    void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
8000b57cec5SDimitry Andric
8010b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
8020b57cec5SDimitry Andric    _Trait::_Available,
803*e8d8bef9SDimitry Andric    ~__dtor() { __destroy(); },
8040b57cec5SDimitry Andric    void __destroy() noexcept {
8050b57cec5SDimitry Andric      if (!this->valueless_by_exception()) {
8060b57cec5SDimitry Andric        __visitation::__base::__visit_alt(
8070b57cec5SDimitry Andric            [](auto& __alt) noexcept {
8080b57cec5SDimitry Andric              using __alt_type = __uncvref_t<decltype(__alt)>;
8090b57cec5SDimitry Andric              __alt.~__alt_type();
8100b57cec5SDimitry Andric            },
8110b57cec5SDimitry Andric            *this);
8120b57cec5SDimitry Andric      }
8130b57cec5SDimitry Andric      this->__index = __variant_npos<__index_t>;
8140b57cec5SDimitry Andric    });
8150b57cec5SDimitry Andric
8160b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
8170b57cec5SDimitry Andric    _Trait::_Unavailable,
818*e8d8bef9SDimitry Andric    ~__dtor() = delete;,
8190b57cec5SDimitry Andric    void __destroy() noexcept = delete;);
8200b57cec5SDimitry Andric
8210b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_DESTRUCTOR
8220b57cec5SDimitry Andric
8230b57cec5SDimitry Andrictemplate <class _Traits>
824*e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
825*e8d8bef9SDimitry Andric  using __base_type = __dtor<_Traits>;
8260b57cec5SDimitry Andric
8270b57cec5SDimitry Andricpublic:
8280b57cec5SDimitry Andric  using __base_type::__base_type;
8290b57cec5SDimitry Andric  using __base_type::operator=;
8300b57cec5SDimitry Andric
8310b57cec5SDimitry Andricprotected:
8320b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class... _Args>
8330b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
8340b57cec5SDimitry Andric  static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
8350b57cec5SDimitry Andric    ::new ((void*)_VSTD::addressof(__a))
8360b57cec5SDimitry Andric        __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
8370b57cec5SDimitry Andric    return __a.__value;
8380b57cec5SDimitry Andric  }
8390b57cec5SDimitry Andric
8400b57cec5SDimitry Andric  template <class _Rhs>
8410b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
842*e8d8bef9SDimitry Andric  static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
8430b57cec5SDimitry Andric    __lhs.__destroy();
8440b57cec5SDimitry Andric    if (!__rhs.valueless_by_exception()) {
8450b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
8460b57cec5SDimitry Andric          __rhs.index(),
8470b57cec5SDimitry Andric          [](auto& __lhs_alt, auto&& __rhs_alt) {
8480b57cec5SDimitry Andric            __construct_alt(
8490b57cec5SDimitry Andric                __lhs_alt,
8500b57cec5SDimitry Andric                _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
8510b57cec5SDimitry Andric          },
8520b57cec5SDimitry Andric          __lhs, _VSTD::forward<_Rhs>(__rhs));
8530b57cec5SDimitry Andric      __lhs.__index = __rhs.index();
8540b57cec5SDimitry Andric    }
8550b57cec5SDimitry Andric  }
8560b57cec5SDimitry Andric};
8570b57cec5SDimitry Andric
8580b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_constructible_trait>
8590b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_constructor;
8600b57cec5SDimitry Andric
8610b57cec5SDimitry Andric#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait,             \
8620b57cec5SDimitry Andric                                         move_constructor)                     \
8630b57cec5SDimitry Andric  template <class... _Types>                                                   \
8640b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>,           \
8650b57cec5SDimitry Andric                                                move_constructible_trait>      \
866*e8d8bef9SDimitry Andric      : public __ctor<__traits<_Types...>> {                                   \
867*e8d8bef9SDimitry Andric    using __base_type = __ctor<__traits<_Types...>>;                           \
8680b57cec5SDimitry Andric                                                                               \
8690b57cec5SDimitry Andric  public:                                                                      \
8700b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
8710b57cec5SDimitry Andric    using __base_type::operator=;                                              \
8720b57cec5SDimitry Andric                                                                               \
8730b57cec5SDimitry Andric    __move_constructor(const __move_constructor&) = default;                   \
8740b57cec5SDimitry Andric    move_constructor                                                           \
8750b57cec5SDimitry Andric    ~__move_constructor() = default;                                           \
8760b57cec5SDimitry Andric    __move_constructor& operator=(const __move_constructor&) = default;        \
8770b57cec5SDimitry Andric    __move_constructor& operator=(__move_constructor&&) = default;             \
8780b57cec5SDimitry Andric  }
8790b57cec5SDimitry Andric
8800b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
8810b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
8820b57cec5SDimitry Andric    __move_constructor(__move_constructor&& __that) = default;);
8830b57cec5SDimitry Andric
8840b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
8850b57cec5SDimitry Andric    _Trait::_Available,
8860b57cec5SDimitry Andric    __move_constructor(__move_constructor&& __that) noexcept(
8870b57cec5SDimitry Andric        __all<is_nothrow_move_constructible_v<_Types>...>::value)
8880b57cec5SDimitry Andric        : __move_constructor(__valueless_t{}) {
8890b57cec5SDimitry Andric      this->__generic_construct(*this, _VSTD::move(__that));
8900b57cec5SDimitry Andric    });
8910b57cec5SDimitry Andric
8920b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
8930b57cec5SDimitry Andric    _Trait::_Unavailable,
8940b57cec5SDimitry Andric    __move_constructor(__move_constructor&&) = delete;);
8950b57cec5SDimitry Andric
8960b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
8970b57cec5SDimitry Andric
8980b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_constructible_trait>
8990b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_constructor;
9000b57cec5SDimitry Andric
9010b57cec5SDimitry Andric#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait,             \
9020b57cec5SDimitry Andric                                         copy_constructor)                     \
9030b57cec5SDimitry Andric  template <class... _Types>                                                   \
9040b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>,          \
9050b57cec5SDimitry Andric                                                 copy_constructible_trait>     \
9060b57cec5SDimitry Andric      : public __move_constructor<__traits<_Types...>> {                       \
9070b57cec5SDimitry Andric    using __base_type = __move_constructor<__traits<_Types...>>;               \
9080b57cec5SDimitry Andric                                                                               \
9090b57cec5SDimitry Andric  public:                                                                      \
9100b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
9110b57cec5SDimitry Andric    using __base_type::operator=;                                              \
9120b57cec5SDimitry Andric                                                                               \
9130b57cec5SDimitry Andric    copy_constructor                                                           \
9140b57cec5SDimitry Andric    __copy_constructor(__copy_constructor&&) = default;                        \
9150b57cec5SDimitry Andric    ~__copy_constructor() = default;                                           \
9160b57cec5SDimitry Andric    __copy_constructor& operator=(const __copy_constructor&) = default;        \
9170b57cec5SDimitry Andric    __copy_constructor& operator=(__copy_constructor&&) = default;             \
9180b57cec5SDimitry Andric  }
9190b57cec5SDimitry Andric
9200b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
9210b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
9220b57cec5SDimitry Andric    __copy_constructor(const __copy_constructor& __that) = default;);
9230b57cec5SDimitry Andric
9240b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
9250b57cec5SDimitry Andric    _Trait::_Available,
9260b57cec5SDimitry Andric    __copy_constructor(const __copy_constructor& __that)
9270b57cec5SDimitry Andric        : __copy_constructor(__valueless_t{}) {
9280b57cec5SDimitry Andric      this->__generic_construct(*this, __that);
9290b57cec5SDimitry Andric    });
9300b57cec5SDimitry Andric
9310b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
9320b57cec5SDimitry Andric    _Trait::_Unavailable,
9330b57cec5SDimitry Andric    __copy_constructor(const __copy_constructor&) = delete;);
9340b57cec5SDimitry Andric
9350b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
9360b57cec5SDimitry Andric
9370b57cec5SDimitry Andrictemplate <class _Traits>
9380b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
9390b57cec5SDimitry Andric  using __base_type = __copy_constructor<_Traits>;
9400b57cec5SDimitry Andric
9410b57cec5SDimitry Andricpublic:
9420b57cec5SDimitry Andric  using __base_type::__base_type;
9430b57cec5SDimitry Andric  using __base_type::operator=;
9440b57cec5SDimitry Andric
9450b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
9460b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
9470b57cec5SDimitry Andric  auto& __emplace(_Args&&... __args) {
9480b57cec5SDimitry Andric    this->__destroy();
9490b57cec5SDimitry Andric    auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
9500b57cec5SDimitry Andric                          _VSTD::forward<_Args>(__args)...);
9510b57cec5SDimitry Andric    this->__index = _Ip;
9520b57cec5SDimitry Andric    return __res;
9530b57cec5SDimitry Andric  }
9540b57cec5SDimitry Andric
9550b57cec5SDimitry Andricprotected:
9560b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class _Arg>
9570b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
9580b57cec5SDimitry Andric  void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
9590b57cec5SDimitry Andric    if (this->index() == _Ip) {
9600b57cec5SDimitry Andric      __a.__value = _VSTD::forward<_Arg>(__arg);
9610b57cec5SDimitry Andric    } else {
9620b57cec5SDimitry Andric      struct {
9630b57cec5SDimitry Andric        void operator()(true_type) const {
9640b57cec5SDimitry Andric          __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
9650b57cec5SDimitry Andric        }
9660b57cec5SDimitry Andric        void operator()(false_type) const {
9670b57cec5SDimitry Andric          __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
9680b57cec5SDimitry Andric        }
9690b57cec5SDimitry Andric        __assignment* __this;
9700b57cec5SDimitry Andric        _Arg&& __arg;
9710b57cec5SDimitry Andric      } __impl{this, _VSTD::forward<_Arg>(__arg)};
9720b57cec5SDimitry Andric      __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
9730b57cec5SDimitry Andric                           !is_nothrow_move_constructible_v<_Tp>>{});
9740b57cec5SDimitry Andric    }
9750b57cec5SDimitry Andric  }
9760b57cec5SDimitry Andric
9770b57cec5SDimitry Andric  template <class _That>
9780b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
9790b57cec5SDimitry Andric  void __generic_assign(_That&& __that) {
9800b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
9810b57cec5SDimitry Andric      // do nothing.
9820b57cec5SDimitry Andric    } else if (__that.valueless_by_exception()) {
9830b57cec5SDimitry Andric      this->__destroy();
9840b57cec5SDimitry Andric    } else {
9850b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
9860b57cec5SDimitry Andric          __that.index(),
9870b57cec5SDimitry Andric          [this](auto& __this_alt, auto&& __that_alt) {
9880b57cec5SDimitry Andric            this->__assign_alt(
9890b57cec5SDimitry Andric                __this_alt,
9900b57cec5SDimitry Andric                _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
9910b57cec5SDimitry Andric          },
9920b57cec5SDimitry Andric          *this, _VSTD::forward<_That>(__that));
9930b57cec5SDimitry Andric    }
9940b57cec5SDimitry Andric  }
9950b57cec5SDimitry Andric};
9960b57cec5SDimitry Andric
9970b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_assignable_trait>
9980b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_assignment;
9990b57cec5SDimitry Andric
10000b57cec5SDimitry Andric#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait,                 \
10010b57cec5SDimitry Andric                                        move_assignment)                       \
10020b57cec5SDimitry Andric  template <class... _Types>                                                   \
10030b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>,           \
10040b57cec5SDimitry Andric                                                move_assignable_trait>         \
10050b57cec5SDimitry Andric      : public __assignment<__traits<_Types...>> {                             \
10060b57cec5SDimitry Andric    using __base_type = __assignment<__traits<_Types...>>;                     \
10070b57cec5SDimitry Andric                                                                               \
10080b57cec5SDimitry Andric  public:                                                                      \
10090b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
10100b57cec5SDimitry Andric    using __base_type::operator=;                                              \
10110b57cec5SDimitry Andric                                                                               \
10120b57cec5SDimitry Andric    __move_assignment(const __move_assignment&) = default;                     \
10130b57cec5SDimitry Andric    __move_assignment(__move_assignment&&) = default;                          \
10140b57cec5SDimitry Andric    ~__move_assignment() = default;                                            \
10150b57cec5SDimitry Andric    __move_assignment& operator=(const __move_assignment&) = default;          \
10160b57cec5SDimitry Andric    move_assignment                                                            \
10170b57cec5SDimitry Andric  }
10180b57cec5SDimitry Andric
10190b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
10200b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
10210b57cec5SDimitry Andric    __move_assignment& operator=(__move_assignment&& __that) = default;);
10220b57cec5SDimitry Andric
10230b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
10240b57cec5SDimitry Andric    _Trait::_Available,
10250b57cec5SDimitry Andric    __move_assignment& operator=(__move_assignment&& __that) noexcept(
10260b57cec5SDimitry Andric        __all<(is_nothrow_move_constructible_v<_Types> &&
10270b57cec5SDimitry Andric               is_nothrow_move_assignable_v<_Types>)...>::value) {
10280b57cec5SDimitry Andric      this->__generic_assign(_VSTD::move(__that));
10290b57cec5SDimitry Andric      return *this;
10300b57cec5SDimitry Andric    });
10310b57cec5SDimitry Andric
10320b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
10330b57cec5SDimitry Andric    _Trait::_Unavailable,
10340b57cec5SDimitry Andric    __move_assignment& operator=(__move_assignment&&) = delete;);
10350b57cec5SDimitry Andric
10360b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
10370b57cec5SDimitry Andric
10380b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_assignable_trait>
10390b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_assignment;
10400b57cec5SDimitry Andric
10410b57cec5SDimitry Andric#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait,                 \
10420b57cec5SDimitry Andric                                        copy_assignment)                       \
10430b57cec5SDimitry Andric  template <class... _Types>                                                   \
10440b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>,           \
10450b57cec5SDimitry Andric                                                copy_assignable_trait>         \
10460b57cec5SDimitry Andric      : public __move_assignment<__traits<_Types...>> {                        \
10470b57cec5SDimitry Andric    using __base_type = __move_assignment<__traits<_Types...>>;                \
10480b57cec5SDimitry Andric                                                                               \
10490b57cec5SDimitry Andric  public:                                                                      \
10500b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
10510b57cec5SDimitry Andric    using __base_type::operator=;                                              \
10520b57cec5SDimitry Andric                                                                               \
10530b57cec5SDimitry Andric    __copy_assignment(const __copy_assignment&) = default;                     \
10540b57cec5SDimitry Andric    __copy_assignment(__copy_assignment&&) = default;                          \
10550b57cec5SDimitry Andric    ~__copy_assignment() = default;                                            \
10560b57cec5SDimitry Andric    copy_assignment                                                            \
10570b57cec5SDimitry Andric    __copy_assignment& operator=(__copy_assignment&&) = default;               \
10580b57cec5SDimitry Andric  }
10590b57cec5SDimitry Andric
10600b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
10610b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
10620b57cec5SDimitry Andric    __copy_assignment& operator=(const __copy_assignment& __that) = default;);
10630b57cec5SDimitry Andric
10640b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
10650b57cec5SDimitry Andric    _Trait::_Available,
10660b57cec5SDimitry Andric    __copy_assignment& operator=(const __copy_assignment& __that) {
10670b57cec5SDimitry Andric      this->__generic_assign(__that);
10680b57cec5SDimitry Andric      return *this;
10690b57cec5SDimitry Andric    });
10700b57cec5SDimitry Andric
10710b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
10720b57cec5SDimitry Andric    _Trait::_Unavailable,
10730b57cec5SDimitry Andric    __copy_assignment& operator=(const __copy_assignment&) = delete;);
10740b57cec5SDimitry Andric
10750b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
10760b57cec5SDimitry Andric
10770b57cec5SDimitry Andrictemplate <class... _Types>
10780b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __impl
10790b57cec5SDimitry Andric    : public __copy_assignment<__traits<_Types...>> {
10800b57cec5SDimitry Andric  using __base_type = __copy_assignment<__traits<_Types...>>;
10810b57cec5SDimitry Andric
10820b57cec5SDimitry Andricpublic:
10830b57cec5SDimitry Andric  using __base_type::__base_type;
10840b57cec5SDimitry Andric  using __base_type::operator=;
10850b57cec5SDimitry Andric
10860b57cec5SDimitry Andric  template <size_t _Ip, class _Arg>
10870b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
10880b57cec5SDimitry Andric  void __assign(_Arg&& __arg) {
10890b57cec5SDimitry Andric    this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
10900b57cec5SDimitry Andric                       _VSTD::forward<_Arg>(__arg));
10910b57cec5SDimitry Andric  }
10920b57cec5SDimitry Andric
10930b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
10940b57cec5SDimitry Andric  void __swap(__impl& __that)  {
10950b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
10960b57cec5SDimitry Andric      // do nothing.
10970b57cec5SDimitry Andric    } else if (this->index() == __that.index()) {
10980b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
10990b57cec5SDimitry Andric          this->index(),
11000b57cec5SDimitry Andric          [](auto& __this_alt, auto& __that_alt) {
11010b57cec5SDimitry Andric            using _VSTD::swap;
11020b57cec5SDimitry Andric            swap(__this_alt.__value, __that_alt.__value);
11030b57cec5SDimitry Andric          },
11040b57cec5SDimitry Andric          *this,
11050b57cec5SDimitry Andric          __that);
11060b57cec5SDimitry Andric    } else {
11070b57cec5SDimitry Andric      __impl* __lhs = this;
11080b57cec5SDimitry Andric      __impl* __rhs = _VSTD::addressof(__that);
11090b57cec5SDimitry Andric      if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
11100b57cec5SDimitry Andric        _VSTD::swap(__lhs, __rhs);
11110b57cec5SDimitry Andric      }
11120b57cec5SDimitry Andric      __impl __tmp(_VSTD::move(*__rhs));
11130b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
11145ffd83dbSDimitry Andric      if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
11155ffd83dbSDimitry Andric        this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
11165ffd83dbSDimitry Andric      } else {
11170b57cec5SDimitry Andric        // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
11180b57cec5SDimitry Andric        // and `__tmp` is nothrow move constructible then we move `__tmp` back
11190b57cec5SDimitry Andric        // into `__rhs` and provide the strong exception safety guarantee.
11200b57cec5SDimitry Andric        try {
11210b57cec5SDimitry Andric          this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
11220b57cec5SDimitry Andric        } catch (...) {
11230b57cec5SDimitry Andric          if (__tmp.__move_nothrow()) {
11240b57cec5SDimitry Andric            this->__generic_construct(*__rhs, _VSTD::move(__tmp));
11250b57cec5SDimitry Andric          }
11260b57cec5SDimitry Andric          throw;
11270b57cec5SDimitry Andric        }
11285ffd83dbSDimitry Andric      }
11290b57cec5SDimitry Andric#else
11305ffd83dbSDimitry Andric      // this isn't consolidated with the `if constexpr` branch above due to
11315ffd83dbSDimitry Andric      // `throw` being ill-formed with exceptions disabled even when discarded.
11320b57cec5SDimitry Andric      this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
11330b57cec5SDimitry Andric#endif
11340b57cec5SDimitry Andric      this->__generic_construct(*__lhs, _VSTD::move(__tmp));
11350b57cec5SDimitry Andric    }
11360b57cec5SDimitry Andric  }
11370b57cec5SDimitry Andric
11380b57cec5SDimitry Andricprivate:
11390b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
11400b57cec5SDimitry Andric  bool __move_nothrow() const {
11410b57cec5SDimitry Andric    constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
11420b57cec5SDimitry Andric    return this->valueless_by_exception() || __results[this->index()];
11430b57cec5SDimitry Andric  }
11440b57cec5SDimitry Andric};
11450b57cec5SDimitry Andric
11460b57cec5SDimitry Andricstruct __no_narrowing_check {
11470b57cec5SDimitry Andric  template <class _Dest, class _Source>
11480b57cec5SDimitry Andric  using _Apply = __identity<_Dest>;
11490b57cec5SDimitry Andric};
11500b57cec5SDimitry Andric
11510b57cec5SDimitry Andricstruct __narrowing_check {
11520b57cec5SDimitry Andric  template <class _Dest>
11530b57cec5SDimitry Andric  static auto __test_impl(_Dest (&&)[1]) -> __identity<_Dest>;
11540b57cec5SDimitry Andric  template <class _Dest, class _Source>
1155*e8d8bef9SDimitry Andric  using _Apply _LIBCPP_NODEBUG_TYPE = decltype(__test_impl<_Dest>({_VSTD::declval<_Source>()}));
11560b57cec5SDimitry Andric};
11570b57cec5SDimitry Andric
11580b57cec5SDimitry Andrictemplate <class _Dest, class _Source>
11590b57cec5SDimitry Andricusing __check_for_narrowing _LIBCPP_NODEBUG_TYPE =
11600b57cec5SDimitry Andric  typename _If<
11610b57cec5SDimitry Andric#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
11620b57cec5SDimitry Andric    false &&
11630b57cec5SDimitry Andric#endif
11640b57cec5SDimitry Andric    is_arithmetic<_Dest>::value,
11650b57cec5SDimitry Andric    __narrowing_check,
11660b57cec5SDimitry Andric    __no_narrowing_check
11670b57cec5SDimitry Andric  >::template _Apply<_Dest, _Source>;
11680b57cec5SDimitry Andric
11690b57cec5SDimitry Andrictemplate <class _Tp, size_t _Idx>
11700b57cec5SDimitry Andricstruct __overload {
11710b57cec5SDimitry Andric  template <class _Up>
11720b57cec5SDimitry Andric  auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
11730b57cec5SDimitry Andric};
11740b57cec5SDimitry Andric
11750b57cec5SDimitry Andrictemplate <class _Tp, size_t>
11760b57cec5SDimitry Andricstruct __overload_bool  {
11770b57cec5SDimitry Andric  template <class _Up, class _Ap = __uncvref_t<_Up>>
11780b57cec5SDimitry Andric  auto operator()(bool, _Up&&) const
11790b57cec5SDimitry Andric      -> enable_if_t<is_same_v<_Ap, bool>, __identity<_Tp>>;
11800b57cec5SDimitry Andric};
11810b57cec5SDimitry Andric
11820b57cec5SDimitry Andrictemplate <size_t _Idx>
11830b57cec5SDimitry Andricstruct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
11840b57cec5SDimitry Andrictemplate <size_t _Idx>
11850b57cec5SDimitry Andricstruct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
11860b57cec5SDimitry Andrictemplate <size_t _Idx>
11870b57cec5SDimitry Andricstruct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
11880b57cec5SDimitry Andrictemplate <size_t _Idx>
11890b57cec5SDimitry Andricstruct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
11900b57cec5SDimitry Andric
11910b57cec5SDimitry Andrictemplate <class ..._Bases>
11920b57cec5SDimitry Andricstruct __all_overloads : _Bases... {
11930b57cec5SDimitry Andric  void operator()() const;
11940b57cec5SDimitry Andric  using _Bases::operator()...;
11950b57cec5SDimitry Andric};
11960b57cec5SDimitry Andric
11970b57cec5SDimitry Andrictemplate <class IdxSeq>
11980b57cec5SDimitry Andricstruct __make_overloads_imp;
11990b57cec5SDimitry Andric
12000b57cec5SDimitry Andrictemplate <size_t ..._Idx>
12010b57cec5SDimitry Andricstruct __make_overloads_imp<__tuple_indices<_Idx...> > {
12020b57cec5SDimitry Andric  template <class ..._Types>
12030b57cec5SDimitry Andric  using _Apply _LIBCPP_NODEBUG_TYPE = __all_overloads<__overload<_Types, _Idx>...>;
12040b57cec5SDimitry Andric};
12050b57cec5SDimitry Andric
12060b57cec5SDimitry Andrictemplate <class ..._Types>
12070b57cec5SDimitry Andricusing _MakeOverloads _LIBCPP_NODEBUG_TYPE = typename __make_overloads_imp<
12080b57cec5SDimitry Andric    __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
12090b57cec5SDimitry Andric
12100b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
12110b57cec5SDimitry Andricusing __best_match_t =
12120b57cec5SDimitry Andric    typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
12130b57cec5SDimitry Andric
12140b57cec5SDimitry Andric} // __variant_detail
12150b57cec5SDimitry Andric
12160b57cec5SDimitry Andrictemplate <class... _Types>
12170b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant
12180b57cec5SDimitry Andric    : private __sfinae_ctor_base<
12190b57cec5SDimitry Andric          __all<is_copy_constructible_v<_Types>...>::value,
12200b57cec5SDimitry Andric          __all<is_move_constructible_v<_Types>...>::value>,
12210b57cec5SDimitry Andric      private __sfinae_assign_base<
12220b57cec5SDimitry Andric          __all<(is_copy_constructible_v<_Types> &&
12230b57cec5SDimitry Andric                 is_copy_assignable_v<_Types>)...>::value,
12240b57cec5SDimitry Andric          __all<(is_move_constructible_v<_Types> &&
12250b57cec5SDimitry Andric                 is_move_assignable_v<_Types>)...>::value> {
12260b57cec5SDimitry Andric  static_assert(0 < sizeof...(_Types),
12270b57cec5SDimitry Andric                "variant must consist of at least one alternative.");
12280b57cec5SDimitry Andric
12290b57cec5SDimitry Andric  static_assert(__all<!is_array_v<_Types>...>::value,
12300b57cec5SDimitry Andric                "variant can not have an array type as an alternative.");
12310b57cec5SDimitry Andric
12320b57cec5SDimitry Andric  static_assert(__all<!is_reference_v<_Types>...>::value,
12330b57cec5SDimitry Andric                "variant can not have a reference type as an alternative.");
12340b57cec5SDimitry Andric
12350b57cec5SDimitry Andric  static_assert(__all<!is_void_v<_Types>...>::value,
12360b57cec5SDimitry Andric                "variant can not have a void type as an alternative.");
12370b57cec5SDimitry Andric
12380b57cec5SDimitry Andric  using __first_type = variant_alternative_t<0, variant>;
12390b57cec5SDimitry Andric
12400b57cec5SDimitry Andricpublic:
12410b57cec5SDimitry Andric  template <bool _Dummy = true,
12420b57cec5SDimitry Andric            enable_if_t<__dependent_type<is_default_constructible<__first_type>,
12430b57cec5SDimitry Andric                                         _Dummy>::value,
12440b57cec5SDimitry Andric                        int> = 0>
12450b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
12460b57cec5SDimitry Andric  constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
12470b57cec5SDimitry Andric      : __impl(in_place_index<0>) {}
12480b57cec5SDimitry Andric
12490b57cec5SDimitry Andric  variant(const variant&) = default;
12500b57cec5SDimitry Andric  variant(variant&&) = default;
12510b57cec5SDimitry Andric
12520b57cec5SDimitry Andric  template <
12530b57cec5SDimitry Andric      class _Arg,
12540b57cec5SDimitry Andric      enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
12550b57cec5SDimitry Andric      enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
12560b57cec5SDimitry Andric      enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
12570b57cec5SDimitry Andric      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
12580b57cec5SDimitry Andric      size_t _Ip =
12590b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
12600b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
12610b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
12620b57cec5SDimitry Andric  constexpr variant(_Arg&& __arg) noexcept(
12630b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Arg>)
12640b57cec5SDimitry Andric      : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
12650b57cec5SDimitry Andric
12660b57cec5SDimitry Andric  template <size_t _Ip, class... _Args,
12670b57cec5SDimitry Andric            class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
12680b57cec5SDimitry Andric            class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
12690b57cec5SDimitry Andric            enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
12700b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
12710b57cec5SDimitry Andric  explicit constexpr variant(
12720b57cec5SDimitry Andric      in_place_index_t<_Ip>,
12730b57cec5SDimitry Andric      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
12740b57cec5SDimitry Andric      : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
12750b57cec5SDimitry Andric
12760b57cec5SDimitry Andric  template <
12770b57cec5SDimitry Andric      size_t _Ip,
12780b57cec5SDimitry Andric      class _Up,
12790b57cec5SDimitry Andric      class... _Args,
12800b57cec5SDimitry Andric      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
12810b57cec5SDimitry Andric      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
12820b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
12830b57cec5SDimitry Andric                  int> = 0>
12840b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
12850b57cec5SDimitry Andric  explicit constexpr variant(
12860b57cec5SDimitry Andric      in_place_index_t<_Ip>,
12870b57cec5SDimitry Andric      initializer_list<_Up> __il,
12880b57cec5SDimitry Andric      _Args&&... __args) noexcept(
12890b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
12900b57cec5SDimitry Andric      : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
12910b57cec5SDimitry Andric
12920b57cec5SDimitry Andric  template <
12930b57cec5SDimitry Andric      class _Tp,
12940b57cec5SDimitry Andric      class... _Args,
12950b57cec5SDimitry Andric      size_t _Ip =
12960b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
12970b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
12980b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
12990b57cec5SDimitry Andric  explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
13000b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Args...>)
13010b57cec5SDimitry Andric      : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
13020b57cec5SDimitry Andric
13030b57cec5SDimitry Andric  template <
13040b57cec5SDimitry Andric      class _Tp,
13050b57cec5SDimitry Andric      class _Up,
13060b57cec5SDimitry Andric      class... _Args,
13070b57cec5SDimitry Andric      size_t _Ip =
13080b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
13090b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
13100b57cec5SDimitry Andric                  int> = 0>
13110b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13120b57cec5SDimitry Andric  explicit constexpr variant(
13130b57cec5SDimitry Andric      in_place_type_t<_Tp>,
13140b57cec5SDimitry Andric      initializer_list<_Up> __il,
13150b57cec5SDimitry Andric      _Args&&... __args) noexcept(
13160b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
13170b57cec5SDimitry Andric      : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
13180b57cec5SDimitry Andric
13190b57cec5SDimitry Andric  ~variant() = default;
13200b57cec5SDimitry Andric
13210b57cec5SDimitry Andric  variant& operator=(const variant&) = default;
13220b57cec5SDimitry Andric  variant& operator=(variant&&) = default;
13230b57cec5SDimitry Andric
13240b57cec5SDimitry Andric  template <
13250b57cec5SDimitry Andric      class _Arg,
13260b57cec5SDimitry Andric      enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
13270b57cec5SDimitry Andric      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
13280b57cec5SDimitry Andric      size_t _Ip =
13290b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
13300b57cec5SDimitry Andric      enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
13310b57cec5SDimitry Andric                  int> = 0>
13320b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13330b57cec5SDimitry Andric  variant& operator=(_Arg&& __arg) noexcept(
13340b57cec5SDimitry Andric      is_nothrow_assignable_v<_Tp&, _Arg> &&
13350b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Arg>) {
13360b57cec5SDimitry Andric    __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
13370b57cec5SDimitry Andric    return *this;
13380b57cec5SDimitry Andric  }
13390b57cec5SDimitry Andric
13400b57cec5SDimitry Andric  template <
13410b57cec5SDimitry Andric      size_t _Ip,
13420b57cec5SDimitry Andric      class... _Args,
13430b57cec5SDimitry Andric      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
13440b57cec5SDimitry Andric      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
13450b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
13460b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13470b57cec5SDimitry Andric  _Tp& emplace(_Args&&... __args) {
13480b57cec5SDimitry Andric    return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
13490b57cec5SDimitry Andric  }
13500b57cec5SDimitry Andric
13510b57cec5SDimitry Andric  template <
13520b57cec5SDimitry Andric      size_t _Ip,
13530b57cec5SDimitry Andric      class _Up,
13540b57cec5SDimitry Andric      class... _Args,
13550b57cec5SDimitry Andric      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
13560b57cec5SDimitry Andric      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
13570b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
13580b57cec5SDimitry Andric                  int> = 0>
13590b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13600b57cec5SDimitry Andric  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
13610b57cec5SDimitry Andric    return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
13620b57cec5SDimitry Andric  }
13630b57cec5SDimitry Andric
13640b57cec5SDimitry Andric  template <
13650b57cec5SDimitry Andric      class _Tp,
13660b57cec5SDimitry Andric      class... _Args,
13670b57cec5SDimitry Andric      size_t _Ip =
13680b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
13690b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
13700b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13710b57cec5SDimitry Andric  _Tp& emplace(_Args&&... __args) {
13720b57cec5SDimitry Andric    return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
13730b57cec5SDimitry Andric  }
13740b57cec5SDimitry Andric
13750b57cec5SDimitry Andric  template <
13760b57cec5SDimitry Andric      class _Tp,
13770b57cec5SDimitry Andric      class _Up,
13780b57cec5SDimitry Andric      class... _Args,
13790b57cec5SDimitry Andric      size_t _Ip =
13800b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
13810b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
13820b57cec5SDimitry Andric                  int> = 0>
13830b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13840b57cec5SDimitry Andric  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
13850b57cec5SDimitry Andric    return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
13860b57cec5SDimitry Andric  }
13870b57cec5SDimitry Andric
13880b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13890b57cec5SDimitry Andric  constexpr bool valueless_by_exception() const noexcept {
13900b57cec5SDimitry Andric    return __impl.valueless_by_exception();
13910b57cec5SDimitry Andric  }
13920b57cec5SDimitry Andric
13930b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
13940b57cec5SDimitry Andric  constexpr size_t index() const noexcept { return __impl.index(); }
13950b57cec5SDimitry Andric
13960b57cec5SDimitry Andric  template <
13970b57cec5SDimitry Andric      bool _Dummy = true,
13980b57cec5SDimitry Andric      enable_if_t<
13990b57cec5SDimitry Andric          __all<(
14000b57cec5SDimitry Andric              __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
14010b57cec5SDimitry Andric              __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
14020b57cec5SDimitry Andric          int> = 0>
14030b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
14040b57cec5SDimitry Andric  void swap(variant& __that) noexcept(
14050b57cec5SDimitry Andric      __all<(is_nothrow_move_constructible_v<_Types> &&
14060b57cec5SDimitry Andric             is_nothrow_swappable_v<_Types>)...>::value) {
14070b57cec5SDimitry Andric    __impl.__swap(__that.__impl);
14080b57cec5SDimitry Andric  }
14090b57cec5SDimitry Andric
14100b57cec5SDimitry Andricprivate:
14110b57cec5SDimitry Andric  __variant_detail::__impl<_Types...> __impl;
14120b57cec5SDimitry Andric
14130b57cec5SDimitry Andric  friend struct __variant_detail::__access::__variant;
14140b57cec5SDimitry Andric  friend struct __variant_detail::__visitation::__variant;
14150b57cec5SDimitry Andric};
14160b57cec5SDimitry Andric
14170b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
14180b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14190b57cec5SDimitry Andricconstexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
14200b57cec5SDimitry Andric  return __v.index() == _Ip;
14210b57cec5SDimitry Andric}
14220b57cec5SDimitry Andric
14230b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
14240b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14250b57cec5SDimitry Andricconstexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
14260b57cec5SDimitry Andric  return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
14270b57cec5SDimitry Andric}
14280b57cec5SDimitry Andric
14290b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
14300b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14310b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
14320b57cec5SDimitry Andricconstexpr auto&& __generic_get(_Vp&& __v) {
14330b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
14340b57cec5SDimitry Andric  if (!__holds_alternative<_Ip>(__v)) {
14350b57cec5SDimitry Andric    __throw_bad_variant_access();
14360b57cec5SDimitry Andric  }
14370b57cec5SDimitry Andric  return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
14380b57cec5SDimitry Andric}
14390b57cec5SDimitry Andric
14400b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
14410b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14420b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
14430b57cec5SDimitry Andricconstexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
14440b57cec5SDimitry Andric    variant<_Types...>& __v) {
14450b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
14460b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
14470b57cec5SDimitry Andric  return __generic_get<_Ip>(__v);
14480b57cec5SDimitry Andric}
14490b57cec5SDimitry Andric
14500b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
14510b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14520b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
14530b57cec5SDimitry Andricconstexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
14540b57cec5SDimitry Andric    variant<_Types...>&& __v) {
14550b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
14560b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
14570b57cec5SDimitry Andric  return __generic_get<_Ip>(_VSTD::move(__v));
14580b57cec5SDimitry Andric}
14590b57cec5SDimitry Andric
14600b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
14610b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14620b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
14630b57cec5SDimitry Andricconstexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
14640b57cec5SDimitry Andric    const variant<_Types...>& __v) {
14650b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
14660b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
14670b57cec5SDimitry Andric  return __generic_get<_Ip>(__v);
14680b57cec5SDimitry Andric}
14690b57cec5SDimitry Andric
14700b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
14710b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14720b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
14730b57cec5SDimitry Andricconstexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
14740b57cec5SDimitry Andric    const variant<_Types...>&& __v) {
14750b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
14760b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
14770b57cec5SDimitry Andric  return __generic_get<_Ip>(_VSTD::move(__v));
14780b57cec5SDimitry Andric}
14790b57cec5SDimitry Andric
14800b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
14810b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14820b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
14830b57cec5SDimitry Andricconstexpr _Tp& get(variant<_Types...>& __v) {
14840b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
14850b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
14860b57cec5SDimitry Andric}
14870b57cec5SDimitry Andric
14880b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
14890b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14900b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
14910b57cec5SDimitry Andricconstexpr _Tp&& get(variant<_Types...>&& __v) {
14920b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
14930b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
14940b57cec5SDimitry Andric      _VSTD::move(__v));
14950b57cec5SDimitry Andric}
14960b57cec5SDimitry Andric
14970b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
14980b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14990b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15000b57cec5SDimitry Andricconstexpr const _Tp& get(const variant<_Types...>& __v) {
15010b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15020b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
15030b57cec5SDimitry Andric}
15040b57cec5SDimitry Andric
15050b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
15060b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15070b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15080b57cec5SDimitry Andricconstexpr const _Tp&& get(const variant<_Types...>&& __v) {
15090b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15100b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
15110b57cec5SDimitry Andric      _VSTD::move(__v));
15120b57cec5SDimitry Andric}
15130b57cec5SDimitry Andric
15140b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
15150b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15160b57cec5SDimitry Andricconstexpr auto* __generic_get_if(_Vp* __v) noexcept {
15170b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
15180b57cec5SDimitry Andric  return __v && __holds_alternative<_Ip>(*__v)
15190b57cec5SDimitry Andric             ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
15200b57cec5SDimitry Andric             : nullptr;
15210b57cec5SDimitry Andric}
15220b57cec5SDimitry Andric
15230b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
15240b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15250b57cec5SDimitry Andricconstexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
15260b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept {
15270b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
15280b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
15290b57cec5SDimitry Andric  return __generic_get_if<_Ip>(__v);
15300b57cec5SDimitry Andric}
15310b57cec5SDimitry Andric
15320b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
15330b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15340b57cec5SDimitry Andricconstexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
15350b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept {
15360b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
15370b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
15380b57cec5SDimitry Andric  return __generic_get_if<_Ip>(__v);
15390b57cec5SDimitry Andric}
15400b57cec5SDimitry Andric
15410b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
15420b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15430b57cec5SDimitry Andricconstexpr add_pointer_t<_Tp>
15440b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept {
15450b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15460b57cec5SDimitry Andric  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
15470b57cec5SDimitry Andric}
15480b57cec5SDimitry Andric
15490b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
15500b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15510b57cec5SDimitry Andricconstexpr add_pointer_t<const _Tp>
15520b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept {
15530b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15540b57cec5SDimitry Andric  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
15550b57cec5SDimitry Andric}
15560b57cec5SDimitry Andric
15570b57cec5SDimitry Andrictemplate <class _Operator>
15580b57cec5SDimitry Andricstruct __convert_to_bool {
15590b57cec5SDimitry Andric  template <class _T1, class _T2>
15600b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1561*e8d8bef9SDimitry Andric    static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
15620b57cec5SDimitry Andric        "the relational operator does not return a type which is implicitly convertible to bool");
15630b57cec5SDimitry Andric    return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
15640b57cec5SDimitry Andric  }
15650b57cec5SDimitry Andric};
15660b57cec5SDimitry Andric
15670b57cec5SDimitry Andrictemplate <class... _Types>
15680b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15690b57cec5SDimitry Andricconstexpr bool operator==(const variant<_Types...>& __lhs,
15700b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
15710b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
15720b57cec5SDimitry Andric  if (__lhs.index() != __rhs.index()) return false;
15730b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return true;
15740b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
15750b57cec5SDimitry Andric}
15760b57cec5SDimitry Andric
15770b57cec5SDimitry Andrictemplate <class... _Types>
15780b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15790b57cec5SDimitry Andricconstexpr bool operator!=(const variant<_Types...>& __lhs,
15800b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
15810b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
15820b57cec5SDimitry Andric  if (__lhs.index() != __rhs.index()) return true;
15830b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return false;
15840b57cec5SDimitry Andric  return __variant::__visit_value_at(
15850b57cec5SDimitry Andric      __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
15860b57cec5SDimitry Andric}
15870b57cec5SDimitry Andric
15880b57cec5SDimitry Andrictemplate <class... _Types>
15890b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
15900b57cec5SDimitry Andricconstexpr bool operator<(const variant<_Types...>& __lhs,
15910b57cec5SDimitry Andric                         const variant<_Types...>& __rhs) {
15920b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
15930b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return false;
15940b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return true;
15950b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return true;
15960b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return false;
15970b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
15980b57cec5SDimitry Andric}
15990b57cec5SDimitry Andric
16000b57cec5SDimitry Andrictemplate <class... _Types>
16010b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16020b57cec5SDimitry Andricconstexpr bool operator>(const variant<_Types...>& __lhs,
16030b57cec5SDimitry Andric                         const variant<_Types...>& __rhs) {
16040b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
16050b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return false;
16060b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return true;
16070b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return true;
16080b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return false;
16090b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
16100b57cec5SDimitry Andric}
16110b57cec5SDimitry Andric
16120b57cec5SDimitry Andrictemplate <class... _Types>
16130b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16140b57cec5SDimitry Andricconstexpr bool operator<=(const variant<_Types...>& __lhs,
16150b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
16160b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
16170b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return true;
16180b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return false;
16190b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return true;
16200b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return false;
16210b57cec5SDimitry Andric  return __variant::__visit_value_at(
16220b57cec5SDimitry Andric      __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
16230b57cec5SDimitry Andric}
16240b57cec5SDimitry Andric
16250b57cec5SDimitry Andrictemplate <class... _Types>
16260b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16270b57cec5SDimitry Andricconstexpr bool operator>=(const variant<_Types...>& __lhs,
16280b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
16290b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
16300b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return true;
16310b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return false;
16320b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return true;
16330b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return false;
16340b57cec5SDimitry Andric  return __variant::__visit_value_at(
16350b57cec5SDimitry Andric      __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
16360b57cec5SDimitry Andric}
16370b57cec5SDimitry Andric
1638*e8d8bef9SDimitry Andrictemplate <class... _Vs>
1639*e8d8bef9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1640*e8d8bef9SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1641*e8d8bef9SDimitry Andricconstexpr void __throw_if_valueless(_Vs&&... __vs) {
1642*e8d8bef9SDimitry Andric  const bool __valueless = (... || __vs.valueless_by_exception());
1643*e8d8bef9SDimitry Andric  if (__valueless) {
1644*e8d8bef9SDimitry Andric      __throw_bad_variant_access();
1645*e8d8bef9SDimitry Andric  }
1646*e8d8bef9SDimitry Andric}
1647*e8d8bef9SDimitry Andric
16480b57cec5SDimitry Andrictemplate <class _Visitor, class... _Vs>
16490b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16500b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
16510b57cec5SDimitry Andricconstexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
16520b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1653*e8d8bef9SDimitry Andric  _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
16540b57cec5SDimitry Andric  return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
16550b57cec5SDimitry Andric                                  _VSTD::forward<_Vs>(__vs)...);
16560b57cec5SDimitry Andric}
16570b57cec5SDimitry Andric
1658*e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
1659*e8d8bef9SDimitry Andrictemplate <class _Rp, class _Visitor, class... _Vs>
1660*e8d8bef9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1661*e8d8bef9SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1662*e8d8bef9SDimitry Andricconstexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs) {
1663*e8d8bef9SDimitry Andric  using __variant_detail::__visitation::__variant;
1664*e8d8bef9SDimitry Andric  _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
1665*e8d8bef9SDimitry Andric  return __variant::__visit_value<_Rp>(_VSTD::forward<_Visitor>(__visitor),
1666*e8d8bef9SDimitry Andric                                       _VSTD::forward<_Vs>(__vs)...);
1667*e8d8bef9SDimitry Andric}
1668*e8d8bef9SDimitry Andric#endif
1669*e8d8bef9SDimitry Andric
16700b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS monostate {};
16710b57cec5SDimitry Andric
16720b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16730b57cec5SDimitry Andricconstexpr bool operator<(monostate, monostate) noexcept { return false; }
16740b57cec5SDimitry Andric
16750b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16760b57cec5SDimitry Andricconstexpr bool operator>(monostate, monostate) noexcept { return false; }
16770b57cec5SDimitry Andric
16780b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16790b57cec5SDimitry Andricconstexpr bool operator<=(monostate, monostate) noexcept { return true; }
16800b57cec5SDimitry Andric
16810b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16820b57cec5SDimitry Andricconstexpr bool operator>=(monostate, monostate) noexcept { return true; }
16830b57cec5SDimitry Andric
16840b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16850b57cec5SDimitry Andricconstexpr bool operator==(monostate, monostate) noexcept { return true; }
16860b57cec5SDimitry Andric
16870b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16880b57cec5SDimitry Andricconstexpr bool operator!=(monostate, monostate) noexcept { return false; }
16890b57cec5SDimitry Andric
16900b57cec5SDimitry Andrictemplate <class... _Types>
16910b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
16920b57cec5SDimitry Andricauto swap(variant<_Types...>& __lhs,
16930b57cec5SDimitry Andric          variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
16940b57cec5SDimitry Andric    -> decltype(__lhs.swap(__rhs)) {
16950b57cec5SDimitry Andric  __lhs.swap(__rhs);
16960b57cec5SDimitry Andric}
16970b57cec5SDimitry Andric
16980b57cec5SDimitry Andrictemplate <class... _Types>
16990b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<
17000b57cec5SDimitry Andric    __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
17010b57cec5SDimitry Andric  using argument_type = variant<_Types...>;
17020b57cec5SDimitry Andric  using result_type = size_t;
17030b57cec5SDimitry Andric
17040b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
17050b57cec5SDimitry Andric  result_type operator()(const argument_type& __v) const {
17060b57cec5SDimitry Andric    using __variant_detail::__visitation::__variant;
17070b57cec5SDimitry Andric    size_t __res =
17080b57cec5SDimitry Andric        __v.valueless_by_exception()
17090b57cec5SDimitry Andric               ? 299792458 // Random value chosen by the universe upon creation
17100b57cec5SDimitry Andric               : __variant::__visit_alt(
17110b57cec5SDimitry Andric                     [](const auto& __alt) {
17120b57cec5SDimitry Andric                       using __alt_type = __uncvref_t<decltype(__alt)>;
17130b57cec5SDimitry Andric                       using __value_type = remove_const_t<
17140b57cec5SDimitry Andric                         typename __alt_type::__value_type>;
17150b57cec5SDimitry Andric                       return hash<__value_type>{}(__alt.__value);
17160b57cec5SDimitry Andric                     },
17170b57cec5SDimitry Andric                     __v);
17180b57cec5SDimitry Andric    return __hash_combine(__res, hash<size_t>{}(__v.index()));
17190b57cec5SDimitry Andric  }
17200b57cec5SDimitry Andric};
17210b57cec5SDimitry Andric
17220b57cec5SDimitry Andrictemplate <>
17230b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<monostate> {
17240b57cec5SDimitry Andric  using argument_type = monostate;
17250b57cec5SDimitry Andric  using result_type = size_t;
17260b57cec5SDimitry Andric
17270b57cec5SDimitry Andric  inline _LIBCPP_INLINE_VISIBILITY
17280b57cec5SDimitry Andric  result_type operator()(const argument_type&) const _NOEXCEPT {
17290b57cec5SDimitry Andric    return 66740831; // return a fundamentally attractive random value.
17300b57cec5SDimitry Andric  }
17310b57cec5SDimitry Andric};
17320b57cec5SDimitry Andric
17330b57cec5SDimitry Andric#endif  // _LIBCPP_STD_VER > 14
17340b57cec5SDimitry Andric
17350b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
17360b57cec5SDimitry Andric
17370b57cec5SDimitry Andric_LIBCPP_POP_MACROS
17380b57cec5SDimitry Andric
17390b57cec5SDimitry Andric#endif  // _LIBCPP_VARIANT
1740