xref: /freebsd/contrib/llvm-project/libcxx/include/variant (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_VARIANT
110b57cec5SDimitry Andric#define _LIBCPP_VARIANT
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric   variant synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std {
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric  // 20.7.2, class template variant
190b57cec5SDimitry Andric  template <class... Types>
200b57cec5SDimitry Andric  class variant {
210b57cec5SDimitry Andric  public:
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric    // 20.7.2.1, constructors
240b57cec5SDimitry Andric    constexpr variant() noexcept(see below);
25*bdd1243dSDimitry Andric    constexpr variant(const variant&);
26*bdd1243dSDimitry Andric    constexpr variant(variant&&) noexcept(see below);
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric    template <class T> constexpr variant(T&&) noexcept(see below);
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric    template <class T, class... Args>
310b57cec5SDimitry Andric    constexpr explicit variant(in_place_type_t<T>, Args&&...);
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric    template <class T, class U, class... Args>
340b57cec5SDimitry Andric    constexpr explicit variant(
350b57cec5SDimitry Andric        in_place_type_t<T>, initializer_list<U>, Args&&...);
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric    template <size_t I, class... Args>
380b57cec5SDimitry Andric    constexpr explicit variant(in_place_index_t<I>, Args&&...);
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric    template <size_t I, class U, class... Args>
410b57cec5SDimitry Andric    constexpr explicit variant(
420b57cec5SDimitry Andric        in_place_index_t<I>, initializer_list<U>, Args&&...);
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric    // 20.7.2.2, destructor
450b57cec5SDimitry Andric    ~variant();
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric    // 20.7.2.3, assignment
48*bdd1243dSDimitry Andric    constexpr variant& operator=(const variant&);
49*bdd1243dSDimitry Andric    constexpr variant& operator=(variant&&) noexcept(see below);
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric    template <class T> variant& operator=(T&&) noexcept(see below);
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric    // 20.7.2.4, modifiers
540b57cec5SDimitry Andric    template <class T, class... Args>
550b57cec5SDimitry Andric    T& emplace(Args&&...);
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric    template <class T, class U, class... Args>
580b57cec5SDimitry Andric    T& emplace(initializer_list<U>, Args&&...);
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric    template <size_t I, class... Args>
610b57cec5SDimitry Andric    variant_alternative_t<I, variant>& emplace(Args&&...);
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric    template <size_t I, class U, class...  Args>
640b57cec5SDimitry Andric    variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric    // 20.7.2.5, value status
670b57cec5SDimitry Andric    constexpr bool valueless_by_exception() const noexcept;
680b57cec5SDimitry Andric    constexpr size_t index() const noexcept;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric    // 20.7.2.6, swap
710b57cec5SDimitry Andric    void swap(variant&) noexcept(see below);
720b57cec5SDimitry Andric  };
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric  // 20.7.3, variant helper classes
750b57cec5SDimitry Andric  template <class T> struct variant_size; // undefined
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric  template <class T>
780b57cec5SDimitry Andric  inline constexpr size_t variant_size_v = variant_size<T>::value;
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric  template <class T> struct variant_size<const T>;
810b57cec5SDimitry Andric  template <class T> struct variant_size<volatile T>;
820b57cec5SDimitry Andric  template <class T> struct variant_size<const volatile T>;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric  template <class... Types>
850b57cec5SDimitry Andric  struct variant_size<variant<Types...>>;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative; // undefined
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric  template <size_t I, class T>
900b57cec5SDimitry Andric  using variant_alternative_t = typename variant_alternative<I, T>::type;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const T>;
930b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, volatile T>;
940b57cec5SDimitry Andric  template <size_t I, class T> struct variant_alternative<I, const volatile T>;
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric  template <size_t I, class... Types>
970b57cec5SDimitry Andric  struct variant_alternative<I, variant<Types...>>;
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric  inline constexpr size_t variant_npos = -1;
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric  // 20.7.4, value access
1020b57cec5SDimitry Andric  template <class T, class... Types>
1030b57cec5SDimitry Andric  constexpr bool holds_alternative(const variant<Types...>&) noexcept;
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric  template <size_t I, class... Types>
1060b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&
1070b57cec5SDimitry Andric  get(variant<Types...>&);
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric  template <size_t I, class... Types>
1100b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>>&&
1110b57cec5SDimitry Andric  get(variant<Types...>&&);
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric  template <size_t I, class... Types>
1140b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&
1150b57cec5SDimitry Andric  get(const variant<Types...>&);
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric  template <size_t I, class... Types>
1180b57cec5SDimitry Andric  constexpr variant_alternative_t<I, variant<Types...>> const&&
1190b57cec5SDimitry Andric  get(const variant<Types...>&&);
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric  template <class T, class...  Types>
1220b57cec5SDimitry Andric  constexpr T& get(variant<Types...>&);
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric  template <class T, class... Types>
1250b57cec5SDimitry Andric  constexpr T&& get(variant<Types...>&&);
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric  template <class T, class... Types>
1280b57cec5SDimitry Andric  constexpr const T& get(const variant<Types...>&);
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric  template <class T, class... Types>
1310b57cec5SDimitry Andric  constexpr const T&& get(const variant<Types...>&&);
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric  template <size_t I, class... Types>
1340b57cec5SDimitry Andric  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
1350b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric  template <size_t I, class... Types>
1380b57cec5SDimitry Andric  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
1390b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric  template <class T, class... Types>
1420b57cec5SDimitry Andric  constexpr add_pointer_t<T>
1430b57cec5SDimitry Andric  get_if(variant<Types...>*) noexcept;
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric  template <class T, class... Types>
1460b57cec5SDimitry Andric  constexpr add_pointer_t<const T>
1470b57cec5SDimitry Andric  get_if(const variant<Types...>*) noexcept;
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric  // 20.7.5, relational operators
1500b57cec5SDimitry Andric  template <class... Types>
1510b57cec5SDimitry Andric  constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric  template <class... Types>
1540b57cec5SDimitry Andric  constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric  template <class... Types>
1570b57cec5SDimitry Andric  constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric  template <class... Types>
1600b57cec5SDimitry Andric  constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric  template <class... Types>
1630b57cec5SDimitry Andric  constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric  template <class... Types>
1660b57cec5SDimitry Andric  constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
1670b57cec5SDimitry Andric
168*bdd1243dSDimitry Andric  template <class... Types> requires (three_way_comparable<Types> && ...)
169*bdd1243dSDimitry Andric  constexpr common_comparison_category_t<compare_three_way_result_t<Types>...>
170*bdd1243dSDimitry Andric    operator<=>(const variant<Types...>&, const variant<Types...>&);           // since C++20
171*bdd1243dSDimitry Andric
1720b57cec5SDimitry Andric  // 20.7.6, visitation
1730b57cec5SDimitry Andric  template <class Visitor, class... Variants>
1740b57cec5SDimitry Andric  constexpr see below visit(Visitor&&, Variants&&...);
1750b57cec5SDimitry Andric
176e8d8bef9SDimitry Andric  template <class R, class Visitor, class... Variants>
177e8d8bef9SDimitry Andric  constexpr R visit(Visitor&&, Variants&&...); // since C++20
178e8d8bef9SDimitry Andric
1790b57cec5SDimitry Andric  // 20.7.7, class monostate
1800b57cec5SDimitry Andric  struct monostate;
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric  // 20.7.8, monostate relational operators
1830b57cec5SDimitry Andric  constexpr bool operator==(monostate, monostate) noexcept;
184*bdd1243dSDimitry Andric  constexpr bool operator!=(monostate, monostate) noexcept;             // until C++20
185*bdd1243dSDimitry Andric  constexpr bool operator<(monostate, monostate) noexcept;              // until C++20
186*bdd1243dSDimitry Andric  constexpr bool operator>(monostate, monostate) noexcept;              // until C++20
187*bdd1243dSDimitry Andric  constexpr bool operator<=(monostate, monostate) noexcept;             // until C++20
188*bdd1243dSDimitry Andric  constexpr bool operator>=(monostate, monostate) noexcept;             // until C++20
189*bdd1243dSDimitry Andric  constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric  // 20.7.9, specialized algorithms
1920b57cec5SDimitry Andric  template <class... Types>
1930b57cec5SDimitry Andric  void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric  // 20.7.10, class bad_variant_access
1960b57cec5SDimitry Andric  class bad_variant_access;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric  // 20.7.11, hash support
1990b57cec5SDimitry Andric  template <class T> struct hash;
2000b57cec5SDimitry Andric  template <class... Types> struct hash<variant<Types...>>;
2010b57cec5SDimitry Andric  template <> struct hash<monostate>;
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric} // namespace std
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric*/
2060b57cec5SDimitry Andric
20781ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
208e8d8bef9SDimitry Andric#include <__availability>
209*bdd1243dSDimitry Andric#include <__compare/common_comparison_category.h>
210*bdd1243dSDimitry Andric#include <__compare/compare_three_way_result.h>
211*bdd1243dSDimitry Andric#include <__compare/three_way_comparable.h>
212fe6060f1SDimitry Andric#include <__config>
213fe6060f1SDimitry Andric#include <__functional/hash.h>
214*bdd1243dSDimitry Andric#include <__functional/invoke.h>
21581ad6265SDimitry Andric#include <__functional/operations.h>
21681ad6265SDimitry Andric#include <__functional/unary_function.h>
217*bdd1243dSDimitry Andric#include <__type_traits/add_const.h>
218*bdd1243dSDimitry Andric#include <__type_traits/add_cv.h>
219*bdd1243dSDimitry Andric#include <__type_traits/add_pointer.h>
220*bdd1243dSDimitry Andric#include <__type_traits/add_volatile.h>
221*bdd1243dSDimitry Andric#include <__type_traits/dependent_type.h>
222*bdd1243dSDimitry Andric#include <__type_traits/is_array.h>
223*bdd1243dSDimitry Andric#include <__type_traits/is_destructible.h>
224*bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_move_constructible.h>
225*bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_assignable.h>
226*bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_constructible.h>
227*bdd1243dSDimitry Andric#include <__type_traits/is_trivially_destructible.h>
228*bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_assignable.h>
229*bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_constructible.h>
230*bdd1243dSDimitry Andric#include <__type_traits/is_void.h>
231*bdd1243dSDimitry Andric#include <__type_traits/remove_const.h>
232*bdd1243dSDimitry Andric#include <__type_traits/type_identity.h>
233*bdd1243dSDimitry Andric#include <__type_traits/void_t.h>
234fe6060f1SDimitry Andric#include <__utility/forward.h>
23581ad6265SDimitry Andric#include <__utility/in_place.h>
23681ad6265SDimitry Andric#include <__utility/move.h>
23781ad6265SDimitry Andric#include <__utility/swap.h>
238fe6060f1SDimitry Andric#include <__variant/monostate.h>
2390b57cec5SDimitry Andric#include <exception>
2400b57cec5SDimitry Andric#include <initializer_list>
241fe6060f1SDimitry Andric#include <limits>
2420b57cec5SDimitry Andric#include <new>
2430b57cec5SDimitry Andric#include <tuple>
2440b57cec5SDimitry Andric#include <version>
2450b57cec5SDimitry Andric
24681ad6265SDimitry Andric// standard-mandated includes
247*bdd1243dSDimitry Andric
248*bdd1243dSDimitry Andric// [variant.syn]
24981ad6265SDimitry Andric#include <compare>
25081ad6265SDimitry Andric
2510b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2520b57cec5SDimitry Andric#  pragma GCC system_header
2530b57cec5SDimitry Andric#endif
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
2560b57cec5SDimitry Andric#include <__undef_macros>
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andricnamespace std { // explicitly not using versioning namespace
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
2610b57cec5SDimitry Andricpublic:
262*bdd1243dSDimitry Andric   const char* what() const _NOEXCEPT override;
2630b57cec5SDimitry Andric};
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric} // namespace std
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2680b57cec5SDimitry Andric
269349cc55cSDimitry Andric#if _LIBCPP_STD_VER > 14
2700b57cec5SDimitry Andric
271fe6060f1SDimitry Andric// Light N-dimensional array of function pointers. Used in place of std::array to avoid
272fe6060f1SDimitry Andric// adding a dependency.
273fe6060f1SDimitry Andrictemplate<class _Tp, size_t _Size>
274fe6060f1SDimitry Andricstruct __farray {
275fe6060f1SDimitry Andric  static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
276fe6060f1SDimitry Andric  _Tp __buf_[_Size] = {};
277fe6060f1SDimitry Andric
278fe6060f1SDimitry Andric  _LIBCPP_INLINE_VISIBILITY constexpr
279fe6060f1SDimitry Andric  const _Tp &operator[](size_t __n) const noexcept {
280fe6060f1SDimitry Andric      return __buf_[__n];
281fe6060f1SDimitry Andric  }
282fe6060f1SDimitry Andric};
283fe6060f1SDimitry Andric
2840b57cec5SDimitry Andric_LIBCPP_NORETURN
285*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
2860b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
2870b57cec5SDimitry Andricvoid __throw_bad_variant_access() {
2880b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
2890b57cec5SDimitry Andric        throw bad_variant_access();
2900b57cec5SDimitry Andric#else
2910b57cec5SDimitry Andric        _VSTD::abort();
2920b57cec5SDimitry Andric#endif
2930b57cec5SDimitry Andric}
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andrictemplate <class... _Types>
2960b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant;
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andrictemplate <class _Tp>
2990b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size;
3000b57cec5SDimitry Andric
3010b57cec5SDimitry Andrictemplate <class _Tp>
302349cc55cSDimitry Andricinline constexpr size_t variant_size_v = variant_size<_Tp>::value;
3030b57cec5SDimitry Andric
3040b57cec5SDimitry Andrictemplate <class _Tp>
3050b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andrictemplate <class _Tp>
3080b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andrictemplate <class _Tp>
3110b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
3120b57cec5SDimitry Andric    : variant_size<_Tp> {};
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andrictemplate <class... _Types>
3150b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
3160b57cec5SDimitry Andric    : integral_constant<size_t, sizeof...(_Types)> {};
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3190b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative;
3200b57cec5SDimitry Andric
3210b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3220b57cec5SDimitry Andricusing variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
3230b57cec5SDimitry Andric
3240b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3250b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
3260b57cec5SDimitry Andric    : add_const<variant_alternative_t<_Ip, _Tp>> {};
3270b57cec5SDimitry Andric
3280b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3290b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
3300b57cec5SDimitry Andric    : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
3310b57cec5SDimitry Andric
3320b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp>
3330b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
3340b57cec5SDimitry Andric    : add_cv<variant_alternative_t<_Ip, _Tp>> {};
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
3370b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
3380b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
3390b57cec5SDimitry Andric  using type = __type_pack_element<_Ip, _Types...>;
3400b57cec5SDimitry Andric};
3410b57cec5SDimitry Andric
342349cc55cSDimitry Andricinline constexpr size_t variant_npos = static_cast<size_t>(-1);
3430b57cec5SDimitry Andric
344*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int __choose_index_type(unsigned int __num_elem) {
345e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned char>::max())
3460b57cec5SDimitry Andric    return 0;
347e8d8bef9SDimitry Andric  if (__num_elem < numeric_limits<unsigned short>::max())
3480b57cec5SDimitry Andric    return 1;
3490b57cec5SDimitry Andric  return 2;
3500b57cec5SDimitry Andric}
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andrictemplate <size_t _NumAlts>
3530b57cec5SDimitry Andricusing __variant_index_t =
3540b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
3550b57cec5SDimitry Andric  unsigned int;
3560b57cec5SDimitry Andric#else
3570b57cec5SDimitry Andric  std::tuple_element_t<
3580b57cec5SDimitry Andric      __choose_index_type(_NumAlts),
3590b57cec5SDimitry Andric      std::tuple<unsigned char, unsigned short, unsigned int>
3600b57cec5SDimitry Andric  >;
3610b57cec5SDimitry Andric#endif
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andrictemplate <class _IndexType>
3640b57cec5SDimitry Andricconstexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
3650b57cec5SDimitry Andric
366fe6060f1SDimitry Andrictemplate <class... _Types>
367fe6060f1SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant;
368fe6060f1SDimitry Andric
369fe6060f1SDimitry Andrictemplate <class... _Types>
370fe6060f1SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&
371fe6060f1SDimitry Andric__as_variant(variant<_Types...>& __vs) noexcept {
372fe6060f1SDimitry Andric  return __vs;
373fe6060f1SDimitry Andric}
374fe6060f1SDimitry Andric
375fe6060f1SDimitry Andrictemplate <class... _Types>
376fe6060f1SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&
377fe6060f1SDimitry Andric__as_variant(const variant<_Types...>& __vs) noexcept {
378fe6060f1SDimitry Andric  return __vs;
379fe6060f1SDimitry Andric}
380fe6060f1SDimitry Andric
381fe6060f1SDimitry Andrictemplate <class... _Types>
382fe6060f1SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&&
383fe6060f1SDimitry Andric__as_variant(variant<_Types...>&& __vs) noexcept {
384fe6060f1SDimitry Andric  return _VSTD::move(__vs);
385fe6060f1SDimitry Andric}
386fe6060f1SDimitry Andric
387fe6060f1SDimitry Andrictemplate <class... _Types>
388fe6060f1SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&&
389fe6060f1SDimitry Andric__as_variant(const variant<_Types...>&& __vs) noexcept {
390fe6060f1SDimitry Andric  return _VSTD::move(__vs);
391fe6060f1SDimitry Andric}
392fe6060f1SDimitry Andric
3930b57cec5SDimitry Andricnamespace __find_detail {
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
396*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
3970b57cec5SDimitry Andricconstexpr size_t __find_index() {
3980b57cec5SDimitry Andric  constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
3990b57cec5SDimitry Andric  size_t __result = __not_found;
4000b57cec5SDimitry Andric  for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
4010b57cec5SDimitry Andric    if (__matches[__i]) {
4020b57cec5SDimitry Andric      if (__result != __not_found) {
4030b57cec5SDimitry Andric        return __ambiguous;
4040b57cec5SDimitry Andric      }
4050b57cec5SDimitry Andric      __result = __i;
4060b57cec5SDimitry Andric    }
4070b57cec5SDimitry Andric  }
4080b57cec5SDimitry Andric  return __result;
4090b57cec5SDimitry Andric}
4100b57cec5SDimitry Andric
4110b57cec5SDimitry Andrictemplate <size_t _Index>
4120b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl
4130b57cec5SDimitry Andric    : integral_constant<size_t, _Index> {};
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andrictemplate <>
4160b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__not_found> {};
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andrictemplate <>
4190b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
4220b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae
4230b57cec5SDimitry Andric    : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andric} // namespace __find_detail
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andricnamespace __variant_detail {
4280b57cec5SDimitry Andric
4290b57cec5SDimitry Andricstruct __valueless_t {};
4300b57cec5SDimitry Andric
4310b57cec5SDimitry Andricenum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andrictemplate <typename _Tp,
4340b57cec5SDimitry Andric          template <typename> class _IsTriviallyAvailable,
4350b57cec5SDimitry Andric          template <typename> class _IsAvailable>
4360b57cec5SDimitry Andricconstexpr _Trait __trait =
4370b57cec5SDimitry Andric    _IsTriviallyAvailable<_Tp>::value
4380b57cec5SDimitry Andric        ? _Trait::_TriviallyAvailable
4390b57cec5SDimitry Andric        : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
4400b57cec5SDimitry Andric
441*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
4420b57cec5SDimitry Andricconstexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
4430b57cec5SDimitry Andric  _Trait __result = _Trait::_TriviallyAvailable;
4440b57cec5SDimitry Andric  for (_Trait __t : __traits) {
4450b57cec5SDimitry Andric    if (static_cast<int>(__t) > static_cast<int>(__result)) {
4460b57cec5SDimitry Andric      __result = __t;
4470b57cec5SDimitry Andric    }
4480b57cec5SDimitry Andric  }
4490b57cec5SDimitry Andric  return __result;
4500b57cec5SDimitry Andric}
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andrictemplate <typename... _Types>
4530b57cec5SDimitry Andricstruct __traits {
4540b57cec5SDimitry Andric  static constexpr _Trait __copy_constructible_trait =
455*bdd1243dSDimitry Andric      __variant_detail::__common_trait({__trait<_Types,
4560b57cec5SDimitry Andric                              is_trivially_copy_constructible,
4570b57cec5SDimitry Andric                              is_copy_constructible>...});
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andric  static constexpr _Trait __move_constructible_trait =
460*bdd1243dSDimitry Andric      __variant_detail::__common_trait({__trait<_Types,
4610b57cec5SDimitry Andric                              is_trivially_move_constructible,
4620b57cec5SDimitry Andric                              is_move_constructible>...});
4630b57cec5SDimitry Andric
464*bdd1243dSDimitry Andric  static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
4650b57cec5SDimitry Andric      {__copy_constructible_trait,
4660b57cec5SDimitry Andric       __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
4670b57cec5SDimitry Andric
468*bdd1243dSDimitry Andric  static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
4690b57cec5SDimitry Andric      {__move_constructible_trait,
4700b57cec5SDimitry Andric       __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
4710b57cec5SDimitry Andric
472*bdd1243dSDimitry Andric  static constexpr _Trait __destructible_trait = __variant_detail::__common_trait(
4730b57cec5SDimitry Andric      {__trait<_Types, is_trivially_destructible, is_destructible>...});
4740b57cec5SDimitry Andric};
4750b57cec5SDimitry Andric
4760b57cec5SDimitry Andricnamespace __access {
4770b57cec5SDimitry Andric
4780b57cec5SDimitry Andricstruct __union {
4790b57cec5SDimitry Andric  template <class _Vp>
480*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
4810b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
4820b57cec5SDimitry Andric    return _VSTD::forward<_Vp>(__v).__head;
4830b57cec5SDimitry Andric  }
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric  template <class _Vp, size_t _Ip>
486*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
4870b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
4880b57cec5SDimitry Andric    return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
4890b57cec5SDimitry Andric  }
4900b57cec5SDimitry Andric};
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andricstruct __base {
4930b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
494*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
4950b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v) {
4960b57cec5SDimitry Andric    return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
4970b57cec5SDimitry Andric                              in_place_index<_Ip>);
4980b57cec5SDimitry Andric  }
4990b57cec5SDimitry Andric};
5000b57cec5SDimitry Andric
5010b57cec5SDimitry Andricstruct __variant {
5020b57cec5SDimitry Andric  template <size_t _Ip, class _Vp>
503*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5040b57cec5SDimitry Andric  static constexpr auto&& __get_alt(_Vp&& __v) {
505*bdd1243dSDimitry Andric    return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl_);
5060b57cec5SDimitry Andric  }
5070b57cec5SDimitry Andric};
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric} // namespace __access
5100b57cec5SDimitry Andric
5110b57cec5SDimitry Andricnamespace __visitation {
5120b57cec5SDimitry Andric
5130b57cec5SDimitry Andricstruct __base {
5140b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
515*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5160b57cec5SDimitry Andric  static constexpr decltype(auto)
5170b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
5180b57cec5SDimitry Andric    constexpr auto __fdiagonal =
5190b57cec5SDimitry Andric        __make_fdiagonal<_Visitor&&,
5200b57cec5SDimitry Andric                         decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
5210b57cec5SDimitry Andric    return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
5220b57cec5SDimitry Andric                                _VSTD::forward<_Vs>(__vs).__as_base()...);
5230b57cec5SDimitry Andric  }
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
526*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5270b57cec5SDimitry Andric  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
5280b57cec5SDimitry Andric                                              _Vs&&... __vs) {
5290b57cec5SDimitry Andric    constexpr auto __fmatrix =
5300b57cec5SDimitry Andric        __make_fmatrix<_Visitor&&,
5310b57cec5SDimitry Andric                       decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
5320b57cec5SDimitry Andric    return __at(__fmatrix, __vs.index()...)(
5330b57cec5SDimitry Andric        _VSTD::forward<_Visitor>(__visitor),
5340b57cec5SDimitry Andric        _VSTD::forward<_Vs>(__vs).__as_base()...);
5350b57cec5SDimitry Andric  }
5360b57cec5SDimitry Andric
5370b57cec5SDimitry Andricprivate:
5380b57cec5SDimitry Andric  template <class _Tp>
539*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5400b57cec5SDimitry Andric  static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
5410b57cec5SDimitry Andric
5420b57cec5SDimitry Andric  template <class _Tp, size_t _Np, typename... _Indices>
543*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
544fe6060f1SDimitry Andric  static constexpr auto&& __at(const __farray<_Tp, _Np>& __elems,
5450b57cec5SDimitry Andric                               size_t __index, _Indices... __indices) {
5460b57cec5SDimitry Andric    return __at(__elems[__index], __indices...);
5470b57cec5SDimitry Andric  }
5480b57cec5SDimitry Andric
5490b57cec5SDimitry Andric  template <class _Fp, class... _Fs>
5500b57cec5SDimitry Andric  static constexpr void __std_visit_visitor_return_type_check() {
5510b57cec5SDimitry Andric    static_assert(
5520b57cec5SDimitry Andric        __all<is_same_v<_Fp, _Fs>...>::value,
5530b57cec5SDimitry Andric        "`std::visit` requires the visitor to have a single return type.");
5540b57cec5SDimitry Andric  }
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric  template <class... _Fs>
557*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5580b57cec5SDimitry Andric  static constexpr auto __make_farray(_Fs&&... __fs) {
559*bdd1243dSDimitry Andric    __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
560*bdd1243dSDimitry Andric    using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
5610b57cec5SDimitry Andric    return __result{{_VSTD::forward<_Fs>(__fs)...}};
5620b57cec5SDimitry Andric  }
5630b57cec5SDimitry Andric
564e8d8bef9SDimitry Andric  template <size_t... _Is>
5650b57cec5SDimitry Andric  struct __dispatcher {
5660b57cec5SDimitry Andric    template <class _Fp, class... _Vs>
567*bdd1243dSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
5680b57cec5SDimitry Andric    static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
56981ad6265SDimitry Andric        return _VSTD::__invoke(
5700b57cec5SDimitry Andric            static_cast<_Fp>(__f),
5710b57cec5SDimitry Andric            __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
5720b57cec5SDimitry Andric    }
5730b57cec5SDimitry Andric  };
5740b57cec5SDimitry Andric
5750b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
576*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5770b57cec5SDimitry Andric  static constexpr auto __make_dispatch(index_sequence<_Is...>) {
5780b57cec5SDimitry Andric    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
5790b57cec5SDimitry Andric  }
5800b57cec5SDimitry Andric
5810b57cec5SDimitry Andric  template <size_t _Ip, class _Fp, class... _Vs>
582*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5830b57cec5SDimitry Andric  static constexpr auto __make_fdiagonal_impl() {
5840b57cec5SDimitry Andric    return __make_dispatch<_Fp, _Vs...>(
58581ad6265SDimitry Andric        index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
5860b57cec5SDimitry Andric  }
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
589*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5900b57cec5SDimitry Andric  static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
5910b57cec5SDimitry Andric    return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
5920b57cec5SDimitry Andric  }
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric  template <class _Fp, class _Vp, class... _Vs>
595*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5960b57cec5SDimitry Andric  static constexpr auto __make_fdiagonal() {
597*bdd1243dSDimitry Andric    constexpr size_t _Np = __remove_cvref_t<_Vp>::__size();
598*bdd1243dSDimitry Andric    static_assert(__all<(_Np == __remove_cvref_t<_Vs>::__size())...>::value);
5990b57cec5SDimitry Andric    return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
6000b57cec5SDimitry Andric  }
6010b57cec5SDimitry Andric
6020b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is>
603*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6040b57cec5SDimitry Andric  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
6050b57cec5SDimitry Andric    return __make_dispatch<_Fp, _Vs...>(__is);
6060b57cec5SDimitry Andric  }
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andric  template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
609*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6100b57cec5SDimitry Andric  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
6110b57cec5SDimitry Andric                                            index_sequence<_Js...>,
6120b57cec5SDimitry Andric                                            _Ls... __ls) {
6130b57cec5SDimitry Andric    return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
6140b57cec5SDimitry Andric        index_sequence<_Is..., _Js>{}, __ls...)...);
6150b57cec5SDimitry Andric  }
6160b57cec5SDimitry Andric
6170b57cec5SDimitry Andric  template <class _Fp, class... _Vs>
618*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6190b57cec5SDimitry Andric  static constexpr auto __make_fmatrix() {
6200b57cec5SDimitry Andric    return __make_fmatrix_impl<_Fp, _Vs...>(
621*bdd1243dSDimitry Andric        index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
6220b57cec5SDimitry Andric  }
6230b57cec5SDimitry Andric};
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andricstruct __variant {
6260b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
627*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6280b57cec5SDimitry Andric  static constexpr decltype(auto)
6290b57cec5SDimitry Andric  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
6300b57cec5SDimitry Andric    return __base::__visit_alt_at(__index,
6310b57cec5SDimitry Andric                                  _VSTD::forward<_Visitor>(__visitor),
632*bdd1243dSDimitry Andric                                  _VSTD::forward<_Vs>(__vs).__impl_...);
6330b57cec5SDimitry Andric  }
6340b57cec5SDimitry Andric
6350b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
636*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6370b57cec5SDimitry Andric  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
6380b57cec5SDimitry Andric                                              _Vs&&... __vs) {
639fe6060f1SDimitry Andric    return __base::__visit_alt(
640fe6060f1SDimitry Andric        _VSTD::forward<_Visitor>(__visitor),
641*bdd1243dSDimitry Andric        _VSTD::__as_variant(_VSTD::forward<_Vs>(__vs)).__impl_...);
6420b57cec5SDimitry Andric  }
6430b57cec5SDimitry Andric
6440b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
645*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6460b57cec5SDimitry Andric  static constexpr decltype(auto)
6470b57cec5SDimitry Andric  __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
6480b57cec5SDimitry Andric    return __visit_alt_at(
6490b57cec5SDimitry Andric        __index,
6500b57cec5SDimitry Andric        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
6510b57cec5SDimitry Andric        _VSTD::forward<_Vs>(__vs)...);
6520b57cec5SDimitry Andric  }
6530b57cec5SDimitry Andric
6540b57cec5SDimitry Andric  template <class _Visitor, class... _Vs>
655*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6560b57cec5SDimitry Andric  static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
6570b57cec5SDimitry Andric                                                _Vs&&... __vs) {
6580b57cec5SDimitry Andric    return __visit_alt(
6590b57cec5SDimitry Andric        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
6600b57cec5SDimitry Andric        _VSTD::forward<_Vs>(__vs)...);
6610b57cec5SDimitry Andric  }
662fe6060f1SDimitry Andric
663e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
664e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor, class... _Vs>
665*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
666e8d8bef9SDimitry Andric  static constexpr _Rp __visit_value(_Visitor&& __visitor,
667e8d8bef9SDimitry Andric                                     _Vs&&... __vs) {
668e8d8bef9SDimitry Andric    return __visit_alt(
669e8d8bef9SDimitry Andric        __make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)),
670e8d8bef9SDimitry Andric        _VSTD::forward<_Vs>(__vs)...);
671e8d8bef9SDimitry Andric  }
672e8d8bef9SDimitry Andric#endif
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andricprivate:
6750b57cec5SDimitry Andric  template <class _Visitor, class... _Values>
6760b57cec5SDimitry Andric  static constexpr void __std_visit_exhaustive_visitor_check() {
6770b57cec5SDimitry Andric    static_assert(is_invocable_v<_Visitor, _Values...>,
6780b57cec5SDimitry Andric                  "`std::visit` requires the visitor to be exhaustive.");
6790b57cec5SDimitry Andric  }
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric  template <class _Visitor>
6820b57cec5SDimitry Andric  struct __value_visitor {
6830b57cec5SDimitry Andric    template <class... _Alts>
684*bdd1243dSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
6850b57cec5SDimitry Andric    constexpr decltype(auto) operator()(_Alts&&... __alts) const {
6860b57cec5SDimitry Andric      __std_visit_exhaustive_visitor_check<
6870b57cec5SDimitry Andric          _Visitor,
6880b57cec5SDimitry Andric          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
68981ad6265SDimitry Andric      return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
6900b57cec5SDimitry Andric                             _VSTD::forward<_Alts>(__alts).__value...);
6910b57cec5SDimitry Andric    }
6920b57cec5SDimitry Andric    _Visitor&& __visitor;
6930b57cec5SDimitry Andric  };
6940b57cec5SDimitry Andric
695e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
696e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
697e8d8bef9SDimitry Andric  struct __value_visitor_return_type {
698e8d8bef9SDimitry Andric    template <class... _Alts>
699*bdd1243dSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
700e8d8bef9SDimitry Andric    constexpr _Rp operator()(_Alts&&... __alts) const {
701e8d8bef9SDimitry Andric      __std_visit_exhaustive_visitor_check<
702e8d8bef9SDimitry Andric          _Visitor,
703e8d8bef9SDimitry Andric          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
704e8d8bef9SDimitry Andric      if constexpr (is_void_v<_Rp>) {
70581ad6265SDimitry Andric        _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
706e8d8bef9SDimitry Andric                        _VSTD::forward<_Alts>(__alts).__value...);
707e8d8bef9SDimitry Andric      }
708e8d8bef9SDimitry Andric      else {
70981ad6265SDimitry Andric        return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
710e8d8bef9SDimitry Andric                               _VSTD::forward<_Alts>(__alts).__value...);
711e8d8bef9SDimitry Andric      }
712e8d8bef9SDimitry Andric    }
713e8d8bef9SDimitry Andric
714e8d8bef9SDimitry Andric    _Visitor&& __visitor;
715e8d8bef9SDimitry Andric  };
716e8d8bef9SDimitry Andric#endif
717e8d8bef9SDimitry Andric
7180b57cec5SDimitry Andric  template <class _Visitor>
719*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
7200b57cec5SDimitry Andric  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
7210b57cec5SDimitry Andric    return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
7220b57cec5SDimitry Andric  }
723e8d8bef9SDimitry Andric
724e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
725e8d8bef9SDimitry Andric  template <class _Rp, class _Visitor>
726*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
727e8d8bef9SDimitry Andric  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
728e8d8bef9SDimitry Andric    return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)};
729e8d8bef9SDimitry Andric  }
730e8d8bef9SDimitry Andric#endif
7310b57cec5SDimitry Andric};
7320b57cec5SDimitry Andric
7330b57cec5SDimitry Andric} // namespace __visitation
7340b57cec5SDimitry Andric
7350b57cec5SDimitry Andrictemplate <size_t _Index, class _Tp>
7360b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __alt {
7370b57cec5SDimitry Andric  using __value_type = _Tp;
7380b57cec5SDimitry Andric
7390b57cec5SDimitry Andric  template <class... _Args>
740*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
7410b57cec5SDimitry Andric  explicit constexpr __alt(in_place_t, _Args&&... __args)
7420b57cec5SDimitry Andric      : __value(_VSTD::forward<_Args>(__args)...) {}
7430b57cec5SDimitry Andric
7440b57cec5SDimitry Andric  __value_type __value;
7450b57cec5SDimitry Andric};
7460b57cec5SDimitry Andric
7470b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index, class... _Types>
7480b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union;
7490b57cec5SDimitry Andric
7500b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index>
7510b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
7520b57cec5SDimitry Andric
7530b57cec5SDimitry Andric#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor)                  \
7540b57cec5SDimitry Andric  template <size_t _Index, class _Tp, class... _Types>                         \
7550b57cec5SDimitry Andric  union _LIBCPP_TEMPLATE_VIS __union<destructible_trait,                       \
7560b57cec5SDimitry Andric                                      _Index,                                  \
7570b57cec5SDimitry Andric                                      _Tp,                                     \
7580b57cec5SDimitry Andric                                      _Types...> {                             \
7590b57cec5SDimitry Andric  public:                                                                      \
760*bdd1243dSDimitry Andric    _LIBCPP_HIDE_FROM_ABI                                                      \
7610b57cec5SDimitry Andric    explicit constexpr __union(__valueless_t) noexcept : __dummy{} {}          \
7620b57cec5SDimitry Andric                                                                               \
7630b57cec5SDimitry Andric    template <class... _Args>                                                  \
764*bdd1243dSDimitry Andric    _LIBCPP_HIDE_FROM_ABI                                                      \
7650b57cec5SDimitry Andric    explicit constexpr __union(in_place_index_t<0>, _Args&&... __args)         \
7660b57cec5SDimitry Andric        : __head(in_place, _VSTD::forward<_Args>(__args)...) {}                \
7670b57cec5SDimitry Andric                                                                               \
7680b57cec5SDimitry Andric    template <size_t _Ip, class... _Args>                                      \
769*bdd1243dSDimitry Andric    _LIBCPP_HIDE_FROM_ABI                                                      \
7700b57cec5SDimitry Andric    explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args)       \
7710b57cec5SDimitry Andric        : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
7720b57cec5SDimitry Andric                                                                               \
7730b57cec5SDimitry Andric    __union(const __union&) = default;                                         \
7740b57cec5SDimitry Andric    __union(__union&&) = default;                                              \
7750b57cec5SDimitry Andric                                                                               \
7760b57cec5SDimitry Andric    destructor                                                                 \
7770b57cec5SDimitry Andric                                                                               \
7780b57cec5SDimitry Andric    __union& operator=(const __union&) = default;                              \
7790b57cec5SDimitry Andric    __union& operator=(__union&&) = default;                                   \
7800b57cec5SDimitry Andric                                                                               \
7810b57cec5SDimitry Andric  private:                                                                     \
7820b57cec5SDimitry Andric    char __dummy;                                                              \
7830b57cec5SDimitry Andric    __alt<_Index, _Tp> __head;                                                 \
7840b57cec5SDimitry Andric    __union<destructible_trait, _Index + 1, _Types...> __tail;                 \
7850b57cec5SDimitry Andric                                                                               \
7860b57cec5SDimitry Andric    friend struct __access::__union;                                           \
7870b57cec5SDimitry Andric  }
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
7900b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
7910b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
7920b57cec5SDimitry Andric
7930b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_UNION
7940b57cec5SDimitry Andric
7950b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, class... _Types>
7960b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __base {
7970b57cec5SDimitry Andricpublic:
7980b57cec5SDimitry Andric  using __index_t = __variant_index_t<sizeof...(_Types)>;
7990b57cec5SDimitry Andric
800*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
801753f127fSDimitry Andric  explicit constexpr __base(__valueless_t __tag) noexcept
802753f127fSDimitry Andric      : __data(__tag), __index(__variant_npos<__index_t>) {}
8030b57cec5SDimitry Andric
8040b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
805*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8060b57cec5SDimitry Andric  explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
8070b57cec5SDimitry Andric      :
8080b57cec5SDimitry Andric        __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
8090b57cec5SDimitry Andric        __index(_Ip) {}
8100b57cec5SDimitry Andric
811*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8120b57cec5SDimitry Andric  constexpr bool valueless_by_exception() const noexcept {
8130b57cec5SDimitry Andric    return index() == variant_npos;
8140b57cec5SDimitry Andric  }
8150b57cec5SDimitry Andric
816*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8170b57cec5SDimitry Andric  constexpr size_t index() const noexcept {
8180b57cec5SDimitry Andric    return __index == __variant_npos<__index_t> ? variant_npos : __index;
8190b57cec5SDimitry Andric  }
8200b57cec5SDimitry Andric
8210b57cec5SDimitry Andricprotected:
822*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8230b57cec5SDimitry Andric  constexpr auto&& __as_base() & { return *this; }
8240b57cec5SDimitry Andric
825*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8260b57cec5SDimitry Andric  constexpr auto&& __as_base() && { return _VSTD::move(*this); }
8270b57cec5SDimitry Andric
828*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8290b57cec5SDimitry Andric  constexpr auto&& __as_base() const & { return *this; }
8300b57cec5SDimitry Andric
831*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8320b57cec5SDimitry Andric  constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
8330b57cec5SDimitry Andric
834*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
8350b57cec5SDimitry Andric  static constexpr size_t __size() { return sizeof...(_Types); }
8360b57cec5SDimitry Andric
8370b57cec5SDimitry Andric  __union<_DestructibleTrait, 0, _Types...> __data;
8380b57cec5SDimitry Andric  __index_t __index;
8390b57cec5SDimitry Andric
8400b57cec5SDimitry Andric  friend struct __access::__base;
8410b57cec5SDimitry Andric  friend struct __visitation::__base;
8420b57cec5SDimitry Andric};
8430b57cec5SDimitry Andric
8440b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__destructible_trait>
845e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __dtor;
8460b57cec5SDimitry Andric
8470b57cec5SDimitry Andric#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy)    \
8480b57cec5SDimitry Andric  template <class... _Types>                                                   \
849e8d8bef9SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>,                       \
8500b57cec5SDimitry Andric                                    destructible_trait>                        \
8510b57cec5SDimitry Andric      : public __base<destructible_trait, _Types...> {                         \
8520b57cec5SDimitry Andric    using __base_type = __base<destructible_trait, _Types...>;                 \
8530b57cec5SDimitry Andric    using __index_t = typename __base_type::__index_t;                         \
8540b57cec5SDimitry Andric                                                                               \
8550b57cec5SDimitry Andric  public:                                                                      \
8560b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
8570b57cec5SDimitry Andric    using __base_type::operator=;                                              \
8580b57cec5SDimitry Andric                                                                               \
859e8d8bef9SDimitry Andric    __dtor(const __dtor&) = default;                                           \
860e8d8bef9SDimitry Andric    __dtor(__dtor&&) = default;                                                \
8610b57cec5SDimitry Andric    destructor                                                                 \
862e8d8bef9SDimitry Andric    __dtor& operator=(const __dtor&) = default;                                \
863e8d8bef9SDimitry Andric    __dtor& operator=(__dtor&&) = default;                                     \
8640b57cec5SDimitry Andric                                                                               \
8650b57cec5SDimitry Andric  protected:                                                                   \
866*bdd1243dSDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI                                               \
8670b57cec5SDimitry Andric    destroy                                                                    \
8680b57cec5SDimitry Andric  }
8690b57cec5SDimitry Andric
8700b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
8710b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
872e8d8bef9SDimitry Andric    ~__dtor() = default;,
8730b57cec5SDimitry Andric    void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
8740b57cec5SDimitry Andric
8750b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
8760b57cec5SDimitry Andric    _Trait::_Available,
877e8d8bef9SDimitry Andric    ~__dtor() { __destroy(); },
8780b57cec5SDimitry Andric    void __destroy() noexcept {
8790b57cec5SDimitry Andric      if (!this->valueless_by_exception()) {
8800b57cec5SDimitry Andric        __visitation::__base::__visit_alt(
8810b57cec5SDimitry Andric            [](auto& __alt) noexcept {
882*bdd1243dSDimitry Andric              using __alt_type = __remove_cvref_t<decltype(__alt)>;
8830b57cec5SDimitry Andric              __alt.~__alt_type();
8840b57cec5SDimitry Andric            },
8850b57cec5SDimitry Andric            *this);
8860b57cec5SDimitry Andric      }
8870b57cec5SDimitry Andric      this->__index = __variant_npos<__index_t>;
8880b57cec5SDimitry Andric    });
8890b57cec5SDimitry Andric
8900b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(
8910b57cec5SDimitry Andric    _Trait::_Unavailable,
892e8d8bef9SDimitry Andric    ~__dtor() = delete;,
8930b57cec5SDimitry Andric    void __destroy() noexcept = delete;);
8940b57cec5SDimitry Andric
8950b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_DESTRUCTOR
8960b57cec5SDimitry Andric
8970b57cec5SDimitry Andrictemplate <class _Traits>
898e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
899e8d8bef9SDimitry Andric  using __base_type = __dtor<_Traits>;
9000b57cec5SDimitry Andric
9010b57cec5SDimitry Andricpublic:
9020b57cec5SDimitry Andric  using __base_type::__base_type;
9030b57cec5SDimitry Andric  using __base_type::operator=;
9040b57cec5SDimitry Andric
9050b57cec5SDimitry Andricprotected:
9060b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class... _Args>
907*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
9080b57cec5SDimitry Andric  static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
9090b57cec5SDimitry Andric    ::new ((void*)_VSTD::addressof(__a))
9100b57cec5SDimitry Andric        __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
9110b57cec5SDimitry Andric    return __a.__value;
9120b57cec5SDimitry Andric  }
9130b57cec5SDimitry Andric
9140b57cec5SDimitry Andric  template <class _Rhs>
915*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
916e8d8bef9SDimitry Andric  static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
9170b57cec5SDimitry Andric    __lhs.__destroy();
9180b57cec5SDimitry Andric    if (!__rhs.valueless_by_exception()) {
9190b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
9200b57cec5SDimitry Andric          __rhs.index(),
9210b57cec5SDimitry Andric          [](auto& __lhs_alt, auto&& __rhs_alt) {
9220b57cec5SDimitry Andric            __construct_alt(
9230b57cec5SDimitry Andric                __lhs_alt,
9240b57cec5SDimitry Andric                _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
9250b57cec5SDimitry Andric          },
9260b57cec5SDimitry Andric          __lhs, _VSTD::forward<_Rhs>(__rhs));
9270b57cec5SDimitry Andric      __lhs.__index = __rhs.index();
9280b57cec5SDimitry Andric    }
9290b57cec5SDimitry Andric  }
9300b57cec5SDimitry Andric};
9310b57cec5SDimitry Andric
9320b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_constructible_trait>
9330b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_constructor;
9340b57cec5SDimitry Andric
9350b57cec5SDimitry Andric#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait,             \
9360b57cec5SDimitry Andric                                         move_constructor)                     \
9370b57cec5SDimitry Andric  template <class... _Types>                                                   \
9380b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>,           \
9390b57cec5SDimitry Andric                                                move_constructible_trait>      \
940e8d8bef9SDimitry Andric      : public __ctor<__traits<_Types...>> {                                   \
941e8d8bef9SDimitry Andric    using __base_type = __ctor<__traits<_Types...>>;                           \
9420b57cec5SDimitry Andric                                                                               \
9430b57cec5SDimitry Andric  public:                                                                      \
9440b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
9450b57cec5SDimitry Andric    using __base_type::operator=;                                              \
9460b57cec5SDimitry Andric                                                                               \
9470b57cec5SDimitry Andric    __move_constructor(const __move_constructor&) = default;                   \
9480b57cec5SDimitry Andric    move_constructor                                                           \
9490b57cec5SDimitry Andric    ~__move_constructor() = default;                                           \
9500b57cec5SDimitry Andric    __move_constructor& operator=(const __move_constructor&) = default;        \
9510b57cec5SDimitry Andric    __move_constructor& operator=(__move_constructor&&) = default;             \
9520b57cec5SDimitry Andric  }
9530b57cec5SDimitry Andric
9540b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
9550b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
9560b57cec5SDimitry Andric    __move_constructor(__move_constructor&& __that) = default;);
9570b57cec5SDimitry Andric
9580b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
9590b57cec5SDimitry Andric    _Trait::_Available,
9600b57cec5SDimitry Andric    __move_constructor(__move_constructor&& __that) noexcept(
9610b57cec5SDimitry Andric        __all<is_nothrow_move_constructible_v<_Types>...>::value)
9620b57cec5SDimitry Andric        : __move_constructor(__valueless_t{}) {
9630b57cec5SDimitry Andric      this->__generic_construct(*this, _VSTD::move(__that));
9640b57cec5SDimitry Andric    });
9650b57cec5SDimitry Andric
9660b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
9670b57cec5SDimitry Andric    _Trait::_Unavailable,
9680b57cec5SDimitry Andric    __move_constructor(__move_constructor&&) = delete;);
9690b57cec5SDimitry Andric
9700b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
9710b57cec5SDimitry Andric
9720b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_constructible_trait>
9730b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_constructor;
9740b57cec5SDimitry Andric
9750b57cec5SDimitry Andric#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait,             \
9760b57cec5SDimitry Andric                                         copy_constructor)                     \
9770b57cec5SDimitry Andric  template <class... _Types>                                                   \
9780b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>,           \
9790b57cec5SDimitry Andric                                                 copy_constructible_trait>     \
9800b57cec5SDimitry Andric      : public __move_constructor<__traits<_Types...>> {                       \
9810b57cec5SDimitry Andric    using __base_type = __move_constructor<__traits<_Types...>>;               \
9820b57cec5SDimitry Andric                                                                               \
9830b57cec5SDimitry Andric  public:                                                                      \
9840b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
9850b57cec5SDimitry Andric    using __base_type::operator=;                                              \
9860b57cec5SDimitry Andric                                                                               \
9870b57cec5SDimitry Andric    copy_constructor                                                           \
9880b57cec5SDimitry Andric    __copy_constructor(__copy_constructor&&) = default;                        \
9890b57cec5SDimitry Andric    ~__copy_constructor() = default;                                           \
9900b57cec5SDimitry Andric    __copy_constructor& operator=(const __copy_constructor&) = default;        \
9910b57cec5SDimitry Andric    __copy_constructor& operator=(__copy_constructor&&) = default;             \
9920b57cec5SDimitry Andric  }
9930b57cec5SDimitry Andric
9940b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
9950b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
9960b57cec5SDimitry Andric    __copy_constructor(const __copy_constructor& __that) = default;);
9970b57cec5SDimitry Andric
9980b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
9990b57cec5SDimitry Andric    _Trait::_Available,
10000b57cec5SDimitry Andric    __copy_constructor(const __copy_constructor& __that)
10010b57cec5SDimitry Andric        : __copy_constructor(__valueless_t{}) {
10020b57cec5SDimitry Andric      this->__generic_construct(*this, __that);
10030b57cec5SDimitry Andric    });
10040b57cec5SDimitry Andric
10050b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
10060b57cec5SDimitry Andric    _Trait::_Unavailable,
10070b57cec5SDimitry Andric    __copy_constructor(const __copy_constructor&) = delete;);
10080b57cec5SDimitry Andric
10090b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
10100b57cec5SDimitry Andric
10110b57cec5SDimitry Andrictemplate <class _Traits>
10120b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
10130b57cec5SDimitry Andric  using __base_type = __copy_constructor<_Traits>;
10140b57cec5SDimitry Andric
10150b57cec5SDimitry Andricpublic:
10160b57cec5SDimitry Andric  using __base_type::__base_type;
10170b57cec5SDimitry Andric  using __base_type::operator=;
10180b57cec5SDimitry Andric
10190b57cec5SDimitry Andric  template <size_t _Ip, class... _Args>
1020*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
10210b57cec5SDimitry Andric  auto& __emplace(_Args&&... __args) {
10220b57cec5SDimitry Andric    this->__destroy();
10230b57cec5SDimitry Andric    auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
10240b57cec5SDimitry Andric                          _VSTD::forward<_Args>(__args)...);
10250b57cec5SDimitry Andric    this->__index = _Ip;
10260b57cec5SDimitry Andric    return __res;
10270b57cec5SDimitry Andric  }
10280b57cec5SDimitry Andric
10290b57cec5SDimitry Andricprotected:
10300b57cec5SDimitry Andric  template <size_t _Ip, class _Tp, class _Arg>
1031*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
10320b57cec5SDimitry Andric  void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
10330b57cec5SDimitry Andric    if (this->index() == _Ip) {
10340b57cec5SDimitry Andric      __a.__value = _VSTD::forward<_Arg>(__arg);
10350b57cec5SDimitry Andric    } else {
10360b57cec5SDimitry Andric      struct {
10370b57cec5SDimitry Andric        void operator()(true_type) const {
10380b57cec5SDimitry Andric          __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
10390b57cec5SDimitry Andric        }
10400b57cec5SDimitry Andric        void operator()(false_type) const {
10410b57cec5SDimitry Andric          __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
10420b57cec5SDimitry Andric        }
10430b57cec5SDimitry Andric        __assignment* __this;
10440b57cec5SDimitry Andric        _Arg&& __arg;
10450b57cec5SDimitry Andric      } __impl{this, _VSTD::forward<_Arg>(__arg)};
10460b57cec5SDimitry Andric      __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
10470b57cec5SDimitry Andric                           !is_nothrow_move_constructible_v<_Tp>>{});
10480b57cec5SDimitry Andric    }
10490b57cec5SDimitry Andric  }
10500b57cec5SDimitry Andric
10510b57cec5SDimitry Andric  template <class _That>
1052*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
10530b57cec5SDimitry Andric  void __generic_assign(_That&& __that) {
10540b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
10550b57cec5SDimitry Andric      // do nothing.
10560b57cec5SDimitry Andric    } else if (__that.valueless_by_exception()) {
10570b57cec5SDimitry Andric      this->__destroy();
10580b57cec5SDimitry Andric    } else {
10590b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
10600b57cec5SDimitry Andric          __that.index(),
10610b57cec5SDimitry Andric          [this](auto& __this_alt, auto&& __that_alt) {
10620b57cec5SDimitry Andric            this->__assign_alt(
10630b57cec5SDimitry Andric                __this_alt,
10640b57cec5SDimitry Andric                _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
10650b57cec5SDimitry Andric          },
10660b57cec5SDimitry Andric          *this, _VSTD::forward<_That>(__that));
10670b57cec5SDimitry Andric    }
10680b57cec5SDimitry Andric  }
10690b57cec5SDimitry Andric};
10700b57cec5SDimitry Andric
10710b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_assignable_trait>
10720b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_assignment;
10730b57cec5SDimitry Andric
10740b57cec5SDimitry Andric#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait,                 \
10750b57cec5SDimitry Andric                                        move_assignment)                       \
10760b57cec5SDimitry Andric  template <class... _Types>                                                   \
10770b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>,            \
10780b57cec5SDimitry Andric                                                move_assignable_trait>         \
10790b57cec5SDimitry Andric      : public __assignment<__traits<_Types...>> {                             \
10800b57cec5SDimitry Andric    using __base_type = __assignment<__traits<_Types...>>;                     \
10810b57cec5SDimitry Andric                                                                               \
10820b57cec5SDimitry Andric  public:                                                                      \
10830b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
10840b57cec5SDimitry Andric    using __base_type::operator=;                                              \
10850b57cec5SDimitry Andric                                                                               \
10860b57cec5SDimitry Andric    __move_assignment(const __move_assignment&) = default;                     \
10870b57cec5SDimitry Andric    __move_assignment(__move_assignment&&) = default;                          \
10880b57cec5SDimitry Andric    ~__move_assignment() = default;                                            \
10890b57cec5SDimitry Andric    __move_assignment& operator=(const __move_assignment&) = default;          \
10900b57cec5SDimitry Andric    move_assignment                                                            \
10910b57cec5SDimitry Andric  }
10920b57cec5SDimitry Andric
10930b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
10940b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
10950b57cec5SDimitry Andric    __move_assignment& operator=(__move_assignment&& __that) = default;);
10960b57cec5SDimitry Andric
10970b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
10980b57cec5SDimitry Andric    _Trait::_Available,
10990b57cec5SDimitry Andric    __move_assignment& operator=(__move_assignment&& __that) noexcept(
11000b57cec5SDimitry Andric        __all<(is_nothrow_move_constructible_v<_Types> &&
11010b57cec5SDimitry Andric               is_nothrow_move_assignable_v<_Types>)...>::value) {
11020b57cec5SDimitry Andric      this->__generic_assign(_VSTD::move(__that));
11030b57cec5SDimitry Andric      return *this;
11040b57cec5SDimitry Andric    });
11050b57cec5SDimitry Andric
11060b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
11070b57cec5SDimitry Andric    _Trait::_Unavailable,
11080b57cec5SDimitry Andric    __move_assignment& operator=(__move_assignment&&) = delete;);
11090b57cec5SDimitry Andric
11100b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
11110b57cec5SDimitry Andric
11120b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_assignable_trait>
11130b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_assignment;
11140b57cec5SDimitry Andric
11150b57cec5SDimitry Andric#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait,                 \
11160b57cec5SDimitry Andric                                        copy_assignment)                       \
11170b57cec5SDimitry Andric  template <class... _Types>                                                   \
11180b57cec5SDimitry Andric  class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>,            \
11190b57cec5SDimitry Andric                                                copy_assignable_trait>         \
11200b57cec5SDimitry Andric      : public __move_assignment<__traits<_Types...>> {                        \
11210b57cec5SDimitry Andric    using __base_type = __move_assignment<__traits<_Types...>>;                \
11220b57cec5SDimitry Andric                                                                               \
11230b57cec5SDimitry Andric  public:                                                                      \
11240b57cec5SDimitry Andric    using __base_type::__base_type;                                            \
11250b57cec5SDimitry Andric    using __base_type::operator=;                                              \
11260b57cec5SDimitry Andric                                                                               \
11270b57cec5SDimitry Andric    __copy_assignment(const __copy_assignment&) = default;                     \
11280b57cec5SDimitry Andric    __copy_assignment(__copy_assignment&&) = default;                          \
11290b57cec5SDimitry Andric    ~__copy_assignment() = default;                                            \
11300b57cec5SDimitry Andric    copy_assignment                                                            \
11310b57cec5SDimitry Andric    __copy_assignment& operator=(__copy_assignment&&) = default;               \
11320b57cec5SDimitry Andric  }
11330b57cec5SDimitry Andric
11340b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
11350b57cec5SDimitry Andric    _Trait::_TriviallyAvailable,
11360b57cec5SDimitry Andric    __copy_assignment& operator=(const __copy_assignment& __that) = default;);
11370b57cec5SDimitry Andric
11380b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
11390b57cec5SDimitry Andric    _Trait::_Available,
11400b57cec5SDimitry Andric    __copy_assignment& operator=(const __copy_assignment& __that) {
11410b57cec5SDimitry Andric      this->__generic_assign(__that);
11420b57cec5SDimitry Andric      return *this;
11430b57cec5SDimitry Andric    });
11440b57cec5SDimitry Andric
11450b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(
11460b57cec5SDimitry Andric    _Trait::_Unavailable,
11470b57cec5SDimitry Andric    __copy_assignment& operator=(const __copy_assignment&) = delete;);
11480b57cec5SDimitry Andric
11490b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
11500b57cec5SDimitry Andric
11510b57cec5SDimitry Andrictemplate <class... _Types>
11520b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __impl
11530b57cec5SDimitry Andric    : public __copy_assignment<__traits<_Types...>> {
11540b57cec5SDimitry Andric  using __base_type = __copy_assignment<__traits<_Types...>>;
11550b57cec5SDimitry Andric
11560b57cec5SDimitry Andricpublic:
115781ad6265SDimitry Andric  using __base_type::__base_type; // get in_place_index_t constructor & friends
115881ad6265SDimitry Andric  __impl(__impl const&) = default;
115981ad6265SDimitry Andric  __impl(__impl&&) = default;
116081ad6265SDimitry Andric  __impl& operator=(__impl const&) = default;
116181ad6265SDimitry Andric  __impl& operator=(__impl&&) = default;
11620b57cec5SDimitry Andric
11630b57cec5SDimitry Andric  template <size_t _Ip, class _Arg>
1164*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
11650b57cec5SDimitry Andric  void __assign(_Arg&& __arg) {
11660b57cec5SDimitry Andric    this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
11670b57cec5SDimitry Andric                       _VSTD::forward<_Arg>(__arg));
11680b57cec5SDimitry Andric  }
11690b57cec5SDimitry Andric
1170*bdd1243dSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI
11710b57cec5SDimitry Andric  void __swap(__impl& __that)  {
11720b57cec5SDimitry Andric    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
11730b57cec5SDimitry Andric      // do nothing.
11740b57cec5SDimitry Andric    } else if (this->index() == __that.index()) {
11750b57cec5SDimitry Andric      __visitation::__base::__visit_alt_at(
11760b57cec5SDimitry Andric          this->index(),
11770b57cec5SDimitry Andric          [](auto& __this_alt, auto& __that_alt) {
11780b57cec5SDimitry Andric            using _VSTD::swap;
11790b57cec5SDimitry Andric            swap(__this_alt.__value, __that_alt.__value);
11800b57cec5SDimitry Andric          },
11810b57cec5SDimitry Andric          *this,
11820b57cec5SDimitry Andric          __that);
11830b57cec5SDimitry Andric    } else {
11840b57cec5SDimitry Andric      __impl* __lhs = this;
11850b57cec5SDimitry Andric      __impl* __rhs = _VSTD::addressof(__that);
11860b57cec5SDimitry Andric      if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
11870b57cec5SDimitry Andric        _VSTD::swap(__lhs, __rhs);
11880b57cec5SDimitry Andric      }
11890b57cec5SDimitry Andric      __impl __tmp(_VSTD::move(*__rhs));
11900b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
11915ffd83dbSDimitry Andric      if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
11925ffd83dbSDimitry Andric        this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
11935ffd83dbSDimitry Andric      } else {
11940b57cec5SDimitry Andric        // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
11950b57cec5SDimitry Andric        // and `__tmp` is nothrow move constructible then we move `__tmp` back
11960b57cec5SDimitry Andric        // into `__rhs` and provide the strong exception safety guarantee.
11970b57cec5SDimitry Andric        try {
11980b57cec5SDimitry Andric          this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
11990b57cec5SDimitry Andric        } catch (...) {
12000b57cec5SDimitry Andric          if (__tmp.__move_nothrow()) {
12010b57cec5SDimitry Andric            this->__generic_construct(*__rhs, _VSTD::move(__tmp));
12020b57cec5SDimitry Andric          }
12030b57cec5SDimitry Andric          throw;
12040b57cec5SDimitry Andric        }
12055ffd83dbSDimitry Andric      }
12060b57cec5SDimitry Andric#else
12075ffd83dbSDimitry Andric      // this isn't consolidated with the `if constexpr` branch above due to
12085ffd83dbSDimitry Andric      // `throw` being ill-formed with exceptions disabled even when discarded.
12090b57cec5SDimitry Andric      this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
12100b57cec5SDimitry Andric#endif
12110b57cec5SDimitry Andric      this->__generic_construct(*__lhs, _VSTD::move(__tmp));
12120b57cec5SDimitry Andric    }
12130b57cec5SDimitry Andric  }
12140b57cec5SDimitry Andric
12150b57cec5SDimitry Andricprivate:
1216*bdd1243dSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI
12170b57cec5SDimitry Andric  bool __move_nothrow() const {
12180b57cec5SDimitry Andric    constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
12190b57cec5SDimitry Andric    return this->valueless_by_exception() || __results[this->index()];
12200b57cec5SDimitry Andric  }
12210b57cec5SDimitry Andric};
12220b57cec5SDimitry Andric
12230b57cec5SDimitry Andricstruct __no_narrowing_check {
12240b57cec5SDimitry Andric  template <class _Dest, class _Source>
122581ad6265SDimitry Andric  using _Apply = __type_identity<_Dest>;
12260b57cec5SDimitry Andric};
12270b57cec5SDimitry Andric
12280b57cec5SDimitry Andricstruct __narrowing_check {
12290b57cec5SDimitry Andric  template <class _Dest>
123081ad6265SDimitry Andric  static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
12310b57cec5SDimitry Andric  template <class _Dest, class _Source>
1232*bdd1243dSDimitry Andric  using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
12330b57cec5SDimitry Andric};
12340b57cec5SDimitry Andric
12350b57cec5SDimitry Andrictemplate <class _Dest, class _Source>
1236349cc55cSDimitry Andricusing __check_for_narrowing _LIBCPP_NODEBUG =
12370b57cec5SDimitry Andric  typename _If<
12380b57cec5SDimitry Andric#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
12390b57cec5SDimitry Andric    false &&
12400b57cec5SDimitry Andric#endif
12410b57cec5SDimitry Andric    is_arithmetic<_Dest>::value,
12420b57cec5SDimitry Andric    __narrowing_check,
12430b57cec5SDimitry Andric    __no_narrowing_check
12440b57cec5SDimitry Andric  >::template _Apply<_Dest, _Source>;
12450b57cec5SDimitry Andric
12460b57cec5SDimitry Andrictemplate <class _Tp, size_t _Idx>
12470b57cec5SDimitry Andricstruct __overload {
12480b57cec5SDimitry Andric  template <class _Up>
12490b57cec5SDimitry Andric  auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
12500b57cec5SDimitry Andric};
12510b57cec5SDimitry Andric
12520b57cec5SDimitry Andrictemplate <class _Tp, size_t>
12530b57cec5SDimitry Andricstruct __overload_bool  {
1254*bdd1243dSDimitry Andric  template <class _Up, class _Ap = __remove_cvref_t<_Up>>
12550b57cec5SDimitry Andric  auto operator()(bool, _Up&&) const
125681ad6265SDimitry Andric      -> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
12570b57cec5SDimitry Andric};
12580b57cec5SDimitry Andric
12590b57cec5SDimitry Andrictemplate <size_t _Idx>
12600b57cec5SDimitry Andricstruct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
12610b57cec5SDimitry Andrictemplate <size_t _Idx>
12620b57cec5SDimitry Andricstruct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
12630b57cec5SDimitry Andrictemplate <size_t _Idx>
12640b57cec5SDimitry Andricstruct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
12650b57cec5SDimitry Andrictemplate <size_t _Idx>
12660b57cec5SDimitry Andricstruct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
12670b57cec5SDimitry Andric
12680b57cec5SDimitry Andrictemplate <class ..._Bases>
12690b57cec5SDimitry Andricstruct __all_overloads : _Bases... {
12700b57cec5SDimitry Andric  void operator()() const;
12710b57cec5SDimitry Andric  using _Bases::operator()...;
12720b57cec5SDimitry Andric};
12730b57cec5SDimitry Andric
12740b57cec5SDimitry Andrictemplate <class IdxSeq>
12750b57cec5SDimitry Andricstruct __make_overloads_imp;
12760b57cec5SDimitry Andric
12770b57cec5SDimitry Andrictemplate <size_t ..._Idx>
12780b57cec5SDimitry Andricstruct __make_overloads_imp<__tuple_indices<_Idx...> > {
12790b57cec5SDimitry Andric  template <class ..._Types>
1280349cc55cSDimitry Andric  using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
12810b57cec5SDimitry Andric};
12820b57cec5SDimitry Andric
12830b57cec5SDimitry Andrictemplate <class ..._Types>
1284349cc55cSDimitry Andricusing _MakeOverloads _LIBCPP_NODEBUG = typename __make_overloads_imp<
12850b57cec5SDimitry Andric    __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
12860b57cec5SDimitry Andric
12870b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
12880b57cec5SDimitry Andricusing __best_match_t =
12890b57cec5SDimitry Andric    typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
12900b57cec5SDimitry Andric
12911fd87a68SDimitry Andric} // namespace __variant_detail
12920b57cec5SDimitry Andric
12930b57cec5SDimitry Andrictemplate <class... _Types>
12940b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant
12950b57cec5SDimitry Andric    : private __sfinae_ctor_base<
12960b57cec5SDimitry Andric          __all<is_copy_constructible_v<_Types>...>::value,
12970b57cec5SDimitry Andric          __all<is_move_constructible_v<_Types>...>::value>,
12980b57cec5SDimitry Andric      private __sfinae_assign_base<
12990b57cec5SDimitry Andric          __all<(is_copy_constructible_v<_Types> &&
13000b57cec5SDimitry Andric                 is_copy_assignable_v<_Types>)...>::value,
13010b57cec5SDimitry Andric          __all<(is_move_constructible_v<_Types> &&
13020b57cec5SDimitry Andric                 is_move_assignable_v<_Types>)...>::value> {
13030b57cec5SDimitry Andric  static_assert(0 < sizeof...(_Types),
13040b57cec5SDimitry Andric                "variant must consist of at least one alternative.");
13050b57cec5SDimitry Andric
13060b57cec5SDimitry Andric  static_assert(__all<!is_array_v<_Types>...>::value,
13070b57cec5SDimitry Andric                "variant can not have an array type as an alternative.");
13080b57cec5SDimitry Andric
13090b57cec5SDimitry Andric  static_assert(__all<!is_reference_v<_Types>...>::value,
13100b57cec5SDimitry Andric                "variant can not have a reference type as an alternative.");
13110b57cec5SDimitry Andric
13120b57cec5SDimitry Andric  static_assert(__all<!is_void_v<_Types>...>::value,
13130b57cec5SDimitry Andric                "variant can not have a void type as an alternative.");
13140b57cec5SDimitry Andric
13150b57cec5SDimitry Andric  using __first_type = variant_alternative_t<0, variant>;
13160b57cec5SDimitry Andric
13170b57cec5SDimitry Andricpublic:
13180b57cec5SDimitry Andric  template <bool _Dummy = true,
13190b57cec5SDimitry Andric            enable_if_t<__dependent_type<is_default_constructible<__first_type>,
13200b57cec5SDimitry Andric                                         _Dummy>::value,
13210b57cec5SDimitry Andric                        int> = 0>
1322*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
13230b57cec5SDimitry Andric  constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1324*bdd1243dSDimitry Andric      : __impl_(in_place_index<0>) {}
13250b57cec5SDimitry Andric
1326*bdd1243dSDimitry Andric  constexpr variant(const variant&) = default;
1327*bdd1243dSDimitry Andric  constexpr variant(variant&&) = default;
13280b57cec5SDimitry Andric
13290b57cec5SDimitry Andric  template <
13300b57cec5SDimitry Andric      class _Arg,
1331*bdd1243dSDimitry Andric      enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1332*bdd1243dSDimitry Andric      enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
1333*bdd1243dSDimitry Andric      enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
13340b57cec5SDimitry Andric      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
13350b57cec5SDimitry Andric      size_t _Ip =
13360b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
13370b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1338*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
13390b57cec5SDimitry Andric  constexpr variant(_Arg&& __arg) noexcept(
13400b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Arg>)
1341*bdd1243dSDimitry Andric      : __impl_(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
13420b57cec5SDimitry Andric
13430b57cec5SDimitry Andric  template <size_t _Ip, class... _Args,
13440b57cec5SDimitry Andric            class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
13450b57cec5SDimitry Andric            class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
13460b57cec5SDimitry Andric            enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1347*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
13480b57cec5SDimitry Andric  explicit constexpr variant(
13490b57cec5SDimitry Andric      in_place_index_t<_Ip>,
13500b57cec5SDimitry Andric      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1351*bdd1243dSDimitry Andric      : __impl_(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
13520b57cec5SDimitry Andric
13530b57cec5SDimitry Andric  template <
13540b57cec5SDimitry Andric      size_t _Ip,
13550b57cec5SDimitry Andric      class _Up,
13560b57cec5SDimitry Andric      class... _Args,
13570b57cec5SDimitry Andric      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
13580b57cec5SDimitry Andric      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
13590b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
13600b57cec5SDimitry Andric                  int> = 0>
1361*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
13620b57cec5SDimitry Andric  explicit constexpr variant(
13630b57cec5SDimitry Andric      in_place_index_t<_Ip>,
13640b57cec5SDimitry Andric      initializer_list<_Up> __il,
13650b57cec5SDimitry Andric      _Args&&... __args) noexcept(
13660b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1367*bdd1243dSDimitry Andric      : __impl_(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
13680b57cec5SDimitry Andric
13690b57cec5SDimitry Andric  template <
13700b57cec5SDimitry Andric      class _Tp,
13710b57cec5SDimitry Andric      class... _Args,
13720b57cec5SDimitry Andric      size_t _Ip =
13730b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
13740b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1375*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
13760b57cec5SDimitry Andric  explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
13770b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Args...>)
1378*bdd1243dSDimitry Andric      : __impl_(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
13790b57cec5SDimitry Andric
13800b57cec5SDimitry Andric  template <
13810b57cec5SDimitry Andric      class _Tp,
13820b57cec5SDimitry Andric      class _Up,
13830b57cec5SDimitry Andric      class... _Args,
13840b57cec5SDimitry Andric      size_t _Ip =
13850b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
13860b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
13870b57cec5SDimitry Andric                  int> = 0>
1388*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
13890b57cec5SDimitry Andric  explicit constexpr variant(
13900b57cec5SDimitry Andric      in_place_type_t<_Tp>,
13910b57cec5SDimitry Andric      initializer_list<_Up> __il,
13920b57cec5SDimitry Andric      _Args&&... __args) noexcept(
13930b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1394*bdd1243dSDimitry Andric      : __impl_(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
13950b57cec5SDimitry Andric
13960b57cec5SDimitry Andric  ~variant() = default;
13970b57cec5SDimitry Andric
1398*bdd1243dSDimitry Andric  constexpr variant& operator=(const variant&) = default;
1399*bdd1243dSDimitry Andric  constexpr variant& operator=(variant&&) = default;
14000b57cec5SDimitry Andric
14010b57cec5SDimitry Andric  template <
14020b57cec5SDimitry Andric      class _Arg,
1403*bdd1243dSDimitry Andric      enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
14040b57cec5SDimitry Andric      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
14050b57cec5SDimitry Andric      size_t _Ip =
14060b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
14070b57cec5SDimitry Andric      enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
14080b57cec5SDimitry Andric                  int> = 0>
1409*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
14100b57cec5SDimitry Andric  variant& operator=(_Arg&& __arg) noexcept(
14110b57cec5SDimitry Andric      is_nothrow_assignable_v<_Tp&, _Arg> &&
14120b57cec5SDimitry Andric      is_nothrow_constructible_v<_Tp, _Arg>) {
1413*bdd1243dSDimitry Andric    __impl_.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
14140b57cec5SDimitry Andric    return *this;
14150b57cec5SDimitry Andric  }
14160b57cec5SDimitry Andric
14170b57cec5SDimitry Andric  template <
14180b57cec5SDimitry Andric      size_t _Ip,
14190b57cec5SDimitry Andric      class... _Args,
14200b57cec5SDimitry Andric      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
14210b57cec5SDimitry Andric      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
14220b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1423*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
14240b57cec5SDimitry Andric  _Tp& emplace(_Args&&... __args) {
1425*bdd1243dSDimitry Andric    return __impl_.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
14260b57cec5SDimitry Andric  }
14270b57cec5SDimitry Andric
14280b57cec5SDimitry Andric  template <
14290b57cec5SDimitry Andric      size_t _Ip,
14300b57cec5SDimitry Andric      class _Up,
14310b57cec5SDimitry Andric      class... _Args,
14320b57cec5SDimitry Andric      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
14330b57cec5SDimitry Andric      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
14340b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
14350b57cec5SDimitry Andric                  int> = 0>
1436*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
14370b57cec5SDimitry Andric  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1438*bdd1243dSDimitry Andric    return __impl_.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
14390b57cec5SDimitry Andric  }
14400b57cec5SDimitry Andric
14410b57cec5SDimitry Andric  template <
14420b57cec5SDimitry Andric      class _Tp,
14430b57cec5SDimitry Andric      class... _Args,
14440b57cec5SDimitry Andric      size_t _Ip =
14450b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
14460b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1447*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
14480b57cec5SDimitry Andric  _Tp& emplace(_Args&&... __args) {
1449*bdd1243dSDimitry Andric    return __impl_.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
14500b57cec5SDimitry Andric  }
14510b57cec5SDimitry Andric
14520b57cec5SDimitry Andric  template <
14530b57cec5SDimitry Andric      class _Tp,
14540b57cec5SDimitry Andric      class _Up,
14550b57cec5SDimitry Andric      class... _Args,
14560b57cec5SDimitry Andric      size_t _Ip =
14570b57cec5SDimitry Andric          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
14580b57cec5SDimitry Andric      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
14590b57cec5SDimitry Andric                  int> = 0>
1460*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
14610b57cec5SDimitry Andric  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1462*bdd1243dSDimitry Andric    return __impl_.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
14630b57cec5SDimitry Andric  }
14640b57cec5SDimitry Andric
1465*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
14660b57cec5SDimitry Andric  constexpr bool valueless_by_exception() const noexcept {
1467*bdd1243dSDimitry Andric    return __impl_.valueless_by_exception();
14680b57cec5SDimitry Andric  }
14690b57cec5SDimitry Andric
1470*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
1471*bdd1243dSDimitry Andric  constexpr size_t index() const noexcept { return __impl_.index(); }
14720b57cec5SDimitry Andric
14730b57cec5SDimitry Andric  template <
14740b57cec5SDimitry Andric      bool _Dummy = true,
14750b57cec5SDimitry Andric      enable_if_t<
14760b57cec5SDimitry Andric          __all<(
14770b57cec5SDimitry Andric              __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
14780b57cec5SDimitry Andric              __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
14790b57cec5SDimitry Andric          int> = 0>
1480*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
14810b57cec5SDimitry Andric  void swap(variant& __that) noexcept(
14820b57cec5SDimitry Andric      __all<(is_nothrow_move_constructible_v<_Types> &&
14830b57cec5SDimitry Andric             is_nothrow_swappable_v<_Types>)...>::value) {
1484*bdd1243dSDimitry Andric    __impl_.__swap(__that.__impl_);
14850b57cec5SDimitry Andric  }
14860b57cec5SDimitry Andric
14870b57cec5SDimitry Andricprivate:
1488*bdd1243dSDimitry Andric  __variant_detail::__impl<_Types...> __impl_;
14890b57cec5SDimitry Andric
14900b57cec5SDimitry Andric  friend struct __variant_detail::__access::__variant;
14910b57cec5SDimitry Andric  friend struct __variant_detail::__visitation::__variant;
14920b57cec5SDimitry Andric};
14930b57cec5SDimitry Andric
14940b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1495*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
14960b57cec5SDimitry Andricconstexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
14970b57cec5SDimitry Andric  return __v.index() == _Ip;
14980b57cec5SDimitry Andric}
14990b57cec5SDimitry Andric
15000b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1501*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15020b57cec5SDimitry Andricconstexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1503*bdd1243dSDimitry Andric  return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
15040b57cec5SDimitry Andric}
15050b57cec5SDimitry Andric
15060b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
1507*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15080b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15090b57cec5SDimitry Andricconstexpr auto&& __generic_get(_Vp&& __v) {
15100b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
1511*bdd1243dSDimitry Andric  if (!std::__holds_alternative<_Ip>(__v)) {
15120b57cec5SDimitry Andric    __throw_bad_variant_access();
15130b57cec5SDimitry Andric  }
15140b57cec5SDimitry Andric  return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
15150b57cec5SDimitry Andric}
15160b57cec5SDimitry Andric
15170b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1518*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15190b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15200b57cec5SDimitry Andricconstexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
15210b57cec5SDimitry Andric    variant<_Types...>& __v) {
15220b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
15230b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1524*bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(__v);
15250b57cec5SDimitry Andric}
15260b57cec5SDimitry Andric
15270b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1528*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15290b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15300b57cec5SDimitry Andricconstexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
15310b57cec5SDimitry Andric    variant<_Types...>&& __v) {
15320b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
15330b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1534*bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(_VSTD::move(__v));
15350b57cec5SDimitry Andric}
15360b57cec5SDimitry Andric
15370b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1538*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15390b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15400b57cec5SDimitry Andricconstexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
15410b57cec5SDimitry Andric    const variant<_Types...>& __v) {
15420b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
15430b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1544*bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(__v);
15450b57cec5SDimitry Andric}
15460b57cec5SDimitry Andric
15470b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1548*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15490b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15500b57cec5SDimitry Andricconstexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
15510b57cec5SDimitry Andric    const variant<_Types...>&& __v) {
15520b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
15530b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1554*bdd1243dSDimitry Andric  return std::__generic_get<_Ip>(_VSTD::move(__v));
15550b57cec5SDimitry Andric}
15560b57cec5SDimitry Andric
15570b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1558*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15590b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15600b57cec5SDimitry Andricconstexpr _Tp& get(variant<_Types...>& __v) {
15610b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15620b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
15630b57cec5SDimitry Andric}
15640b57cec5SDimitry Andric
15650b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1566*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15670b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15680b57cec5SDimitry Andricconstexpr _Tp&& get(variant<_Types...>&& __v) {
15690b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15700b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
15710b57cec5SDimitry Andric      _VSTD::move(__v));
15720b57cec5SDimitry Andric}
15730b57cec5SDimitry Andric
15740b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1575*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15760b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15770b57cec5SDimitry Andricconstexpr const _Tp& get(const variant<_Types...>& __v) {
15780b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15790b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
15800b57cec5SDimitry Andric}
15810b57cec5SDimitry Andric
15820b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1583*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15840b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
15850b57cec5SDimitry Andricconstexpr const _Tp&& get(const variant<_Types...>&& __v) {
15860b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
15870b57cec5SDimitry Andric  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
15880b57cec5SDimitry Andric      _VSTD::move(__v));
15890b57cec5SDimitry Andric}
15900b57cec5SDimitry Andric
15910b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp>
1592*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
15930b57cec5SDimitry Andricconstexpr auto* __generic_get_if(_Vp* __v) noexcept {
15940b57cec5SDimitry Andric  using __variant_detail::__access::__variant;
1595*bdd1243dSDimitry Andric  return __v && std::__holds_alternative<_Ip>(*__v)
15960b57cec5SDimitry Andric             ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
15970b57cec5SDimitry Andric             : nullptr;
15980b57cec5SDimitry Andric}
15990b57cec5SDimitry Andric
16000b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1601*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
16020b57cec5SDimitry Andricconstexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
16030b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept {
16040b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
16050b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1606*bdd1243dSDimitry Andric  return std::__generic_get_if<_Ip>(__v);
16070b57cec5SDimitry Andric}
16080b57cec5SDimitry Andric
16090b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types>
1610*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
16110b57cec5SDimitry Andricconstexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
16120b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept {
16130b57cec5SDimitry Andric  static_assert(_Ip < sizeof...(_Types));
16140b57cec5SDimitry Andric  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1615*bdd1243dSDimitry Andric  return std::__generic_get_if<_Ip>(__v);
16160b57cec5SDimitry Andric}
16170b57cec5SDimitry Andric
16180b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1619*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
16200b57cec5SDimitry Andricconstexpr add_pointer_t<_Tp>
16210b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept {
16220b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
16230b57cec5SDimitry Andric  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
16240b57cec5SDimitry Andric}
16250b57cec5SDimitry Andric
16260b57cec5SDimitry Andrictemplate <class _Tp, class... _Types>
1627*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
16280b57cec5SDimitry Andricconstexpr add_pointer_t<const _Tp>
16290b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept {
16300b57cec5SDimitry Andric  static_assert(!is_void_v<_Tp>);
16310b57cec5SDimitry Andric  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
16320b57cec5SDimitry Andric}
16330b57cec5SDimitry Andric
16340b57cec5SDimitry Andrictemplate <class _Operator>
16350b57cec5SDimitry Andricstruct __convert_to_bool {
16360b57cec5SDimitry Andric  template <class _T1, class _T2>
1637*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
1638*bdd1243dSDimitry Andric  constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1639e8d8bef9SDimitry Andric    static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
16400b57cec5SDimitry Andric        "the relational operator does not return a type which is implicitly convertible to bool");
16410b57cec5SDimitry Andric    return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
16420b57cec5SDimitry Andric  }
16430b57cec5SDimitry Andric};
16440b57cec5SDimitry Andric
16450b57cec5SDimitry Andrictemplate <class... _Types>
1646*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
16470b57cec5SDimitry Andricconstexpr bool operator==(const variant<_Types...>& __lhs,
16480b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
16490b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
16500b57cec5SDimitry Andric  if (__lhs.index() != __rhs.index()) return false;
16510b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return true;
16520b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
16530b57cec5SDimitry Andric}
16540b57cec5SDimitry Andric
1655*bdd1243dSDimitry Andric#  if _LIBCPP_STD_VER > 17
1656*bdd1243dSDimitry Andric
1657*bdd1243dSDimitry Andrictemplate <class... _Types> requires (three_way_comparable<_Types> && ...)
1658*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1659*bdd1243dSDimitry Andricoperator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1660*bdd1243dSDimitry Andric  using __variant_detail::__visitation::__variant;
1661*bdd1243dSDimitry Andric  using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1662*bdd1243dSDimitry Andric  if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1663*bdd1243dSDimitry Andric    return strong_ordering::equal;
1664*bdd1243dSDimitry Andric  if (__lhs.valueless_by_exception())
1665*bdd1243dSDimitry Andric    return strong_ordering::less;
1666*bdd1243dSDimitry Andric  if (__rhs.valueless_by_exception())
1667*bdd1243dSDimitry Andric    return strong_ordering::greater;
1668*bdd1243dSDimitry Andric  if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1669*bdd1243dSDimitry Andric    return __c;
1670*bdd1243dSDimitry Andric  auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1671*bdd1243dSDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1672*bdd1243dSDimitry Andric}
1673*bdd1243dSDimitry Andric
1674*bdd1243dSDimitry Andric#  endif // _LIBCPP_STD_VER > 17
1675*bdd1243dSDimitry Andric
16760b57cec5SDimitry Andrictemplate <class... _Types>
1677*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
16780b57cec5SDimitry Andricconstexpr bool operator!=(const variant<_Types...>& __lhs,
16790b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
16800b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
16810b57cec5SDimitry Andric  if (__lhs.index() != __rhs.index()) return true;
16820b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return false;
16830b57cec5SDimitry Andric  return __variant::__visit_value_at(
16840b57cec5SDimitry Andric      __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
16850b57cec5SDimitry Andric}
16860b57cec5SDimitry Andric
16870b57cec5SDimitry Andrictemplate <class... _Types>
1688*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
16890b57cec5SDimitry Andricconstexpr bool operator<(const variant<_Types...>& __lhs,
16900b57cec5SDimitry Andric                         const variant<_Types...>& __rhs) {
16910b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
16920b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return false;
16930b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return true;
16940b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return true;
16950b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return false;
16960b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
16970b57cec5SDimitry Andric}
16980b57cec5SDimitry Andric
16990b57cec5SDimitry Andrictemplate <class... _Types>
1700*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
17010b57cec5SDimitry Andricconstexpr bool operator>(const variant<_Types...>& __lhs,
17020b57cec5SDimitry Andric                         const variant<_Types...>& __rhs) {
17030b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
17040b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return false;
17050b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return true;
17060b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return true;
17070b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return false;
17080b57cec5SDimitry Andric  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
17090b57cec5SDimitry Andric}
17100b57cec5SDimitry Andric
17110b57cec5SDimitry Andrictemplate <class... _Types>
1712*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
17130b57cec5SDimitry Andricconstexpr bool operator<=(const variant<_Types...>& __lhs,
17140b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
17150b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
17160b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return true;
17170b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return false;
17180b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return true;
17190b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return false;
17200b57cec5SDimitry Andric  return __variant::__visit_value_at(
17210b57cec5SDimitry Andric      __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
17220b57cec5SDimitry Andric}
17230b57cec5SDimitry Andric
17240b57cec5SDimitry Andrictemplate <class... _Types>
1725*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
17260b57cec5SDimitry Andricconstexpr bool operator>=(const variant<_Types...>& __lhs,
17270b57cec5SDimitry Andric                          const variant<_Types...>& __rhs) {
17280b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
17290b57cec5SDimitry Andric  if (__rhs.valueless_by_exception()) return true;
17300b57cec5SDimitry Andric  if (__lhs.valueless_by_exception()) return false;
17310b57cec5SDimitry Andric  if (__lhs.index() > __rhs.index()) return true;
17320b57cec5SDimitry Andric  if (__lhs.index() < __rhs.index()) return false;
17330b57cec5SDimitry Andric  return __variant::__visit_value_at(
17340b57cec5SDimitry Andric      __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
17350b57cec5SDimitry Andric}
17360b57cec5SDimitry Andric
1737e8d8bef9SDimitry Andrictemplate <class... _Vs>
1738*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1739*bdd1243dSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1740*bdd1243dSDimitry Andricconstexpr void __throw_if_valueless(_Vs&&... __vs) {
1741fe6060f1SDimitry Andric  const bool __valueless =
1742fe6060f1SDimitry Andric      (... || _VSTD::__as_variant(__vs).valueless_by_exception());
1743e8d8bef9SDimitry Andric  if (__valueless) {
1744e8d8bef9SDimitry Andric    __throw_bad_variant_access();
1745e8d8bef9SDimitry Andric  }
1746e8d8bef9SDimitry Andric}
1747e8d8bef9SDimitry Andric
1748fe6060f1SDimitry Andrictemplate <
1749fe6060f1SDimitry Andric    class _Visitor, class... _Vs,
1750*bdd1243dSDimitry Andric    typename = void_t<decltype(_VSTD::__as_variant(std::declval<_Vs>()))...> >
1751*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1752*bdd1243dSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1753*bdd1243dSDimitry Andricconstexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
17540b57cec5SDimitry Andric  using __variant_detail::__visitation::__variant;
1755e8d8bef9SDimitry Andric  _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
17560b57cec5SDimitry Andric  return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
17570b57cec5SDimitry Andric                                  _VSTD::forward<_Vs>(__vs)...);
17580b57cec5SDimitry Andric}
17590b57cec5SDimitry Andric
1760e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 17
1761fe6060f1SDimitry Andrictemplate <
1762fe6060f1SDimitry Andric    class _Rp, class _Visitor, class... _Vs,
1763*bdd1243dSDimitry Andric    typename = void_t<decltype(_VSTD::__as_variant(std::declval<_Vs>()))...> >
1764*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1765*bdd1243dSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1766*bdd1243dSDimitry Andricconstexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs) {
1767e8d8bef9SDimitry Andric  using __variant_detail::__visitation::__variant;
1768e8d8bef9SDimitry Andric  _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
1769e8d8bef9SDimitry Andric  return __variant::__visit_value<_Rp>(_VSTD::forward<_Visitor>(__visitor),
1770e8d8bef9SDimitry Andric                                       _VSTD::forward<_Vs>(__vs)...);
1771e8d8bef9SDimitry Andric}
1772e8d8bef9SDimitry Andric#endif
1773e8d8bef9SDimitry Andric
17740b57cec5SDimitry Andrictemplate <class... _Types>
1775*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1776349cc55cSDimitry Andricauto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1777349cc55cSDimitry Andric  noexcept(noexcept(__lhs.swap(__rhs)))
1778349cc55cSDimitry Andric  -> decltype(      __lhs.swap(__rhs))
1779349cc55cSDimitry Andric  { return          __lhs.swap(__rhs); }
17800b57cec5SDimitry Andric
17810b57cec5SDimitry Andrictemplate <class... _Types>
17820b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<
17830b57cec5SDimitry Andric    __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
17840b57cec5SDimitry Andric  using argument_type = variant<_Types...>;
17850b57cec5SDimitry Andric  using result_type = size_t;
17860b57cec5SDimitry Andric
1787*bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
17880b57cec5SDimitry Andric  result_type operator()(const argument_type& __v) const {
17890b57cec5SDimitry Andric    using __variant_detail::__visitation::__variant;
17900b57cec5SDimitry Andric    size_t __res =
17910b57cec5SDimitry Andric        __v.valueless_by_exception()
17920b57cec5SDimitry Andric               ? 299792458 // Random value chosen by the universe upon creation
17930b57cec5SDimitry Andric               : __variant::__visit_alt(
17940b57cec5SDimitry Andric                     [](const auto& __alt) {
1795*bdd1243dSDimitry Andric                       using __alt_type = __remove_cvref_t<decltype(__alt)>;
17960b57cec5SDimitry Andric                       using __value_type = remove_const_t<
17970b57cec5SDimitry Andric                         typename __alt_type::__value_type>;
17980b57cec5SDimitry Andric                       return hash<__value_type>{}(__alt.__value);
17990b57cec5SDimitry Andric                     },
18000b57cec5SDimitry Andric                     __v);
1801*bdd1243dSDimitry Andric    return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
18020b57cec5SDimitry Andric  }
18030b57cec5SDimitry Andric};
18040b57cec5SDimitry Andric
1805fe6060f1SDimitry Andric// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1806fe6060f1SDimitry Andric// type whereas std::get will throw or returning nullptr. This makes it faster than
1807fe6060f1SDimitry Andric// std::get.
1808fe6060f1SDimitry Andrictemplate <size_t _Ip, class _Vp>
1809*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1810fe6060f1SDimitry Andricconstexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1811fe6060f1SDimitry Andric  using __variant_detail::__access::__variant;
1812fe6060f1SDimitry Andric  return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
18130b57cec5SDimitry Andric}
1814fe6060f1SDimitry Andric
1815fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types>
1816*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1817fe6060f1SDimitry Andricconstexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1818*bdd1243dSDimitry Andric  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1819fe6060f1SDimitry Andric}
1820fe6060f1SDimitry Andric
1821fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types>
1822*bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI
1823fe6060f1SDimitry Andricconstexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1824*bdd1243dSDimitry Andric  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1825fe6060f1SDimitry Andric}
18260b57cec5SDimitry Andric
18270b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 14
18280b57cec5SDimitry Andric
18290b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
18300b57cec5SDimitry Andric
18310b57cec5SDimitry Andric_LIBCPP_POP_MACROS
18320b57cec5SDimitry Andric
1833*bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1834*bdd1243dSDimitry Andric#  include <type_traits>
1835*bdd1243dSDimitry Andric#  include <typeinfo>
1836*bdd1243dSDimitry Andric#  include <utility>
1837*bdd1243dSDimitry Andric#endif
1838*bdd1243dSDimitry Andric
18390b57cec5SDimitry Andric#endif // _LIBCPP_VARIANT
1840