10b57cec5SDimitry Andric// -*- C++ -*- 2349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 30b57cec5SDimitry Andric// 40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70b57cec5SDimitry Andric// 80b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric#ifndef _LIBCPP_VARIANT 110b57cec5SDimitry Andric#define _LIBCPP_VARIANT 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric/* 140b57cec5SDimitry Andric variant synopsis 150b57cec5SDimitry Andric 160b57cec5SDimitry Andricnamespace std { 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric // 20.7.2, class template variant 190b57cec5SDimitry Andric template <class... Types> 200b57cec5SDimitry Andric class variant { 210b57cec5SDimitry Andric public: 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric // 20.7.2.1, constructors 240b57cec5SDimitry Andric constexpr variant() noexcept(see below); 25bdd1243dSDimitry Andric constexpr variant(const variant&); 26bdd1243dSDimitry Andric constexpr variant(variant&&) noexcept(see below); 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric template <class T> constexpr variant(T&&) noexcept(see below); 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric template <class T, class... Args> 310b57cec5SDimitry Andric constexpr explicit variant(in_place_type_t<T>, Args&&...); 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric template <class T, class U, class... Args> 340b57cec5SDimitry Andric constexpr explicit variant( 350b57cec5SDimitry Andric in_place_type_t<T>, initializer_list<U>, Args&&...); 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric template <size_t I, class... Args> 380b57cec5SDimitry Andric constexpr explicit variant(in_place_index_t<I>, Args&&...); 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric template <size_t I, class U, class... Args> 410b57cec5SDimitry Andric constexpr explicit variant( 420b57cec5SDimitry Andric in_place_index_t<I>, initializer_list<U>, Args&&...); 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric // 20.7.2.2, destructor 450b57cec5SDimitry Andric ~variant(); 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric // 20.7.2.3, assignment 48bdd1243dSDimitry Andric constexpr variant& operator=(const variant&); 49bdd1243dSDimitry Andric constexpr variant& operator=(variant&&) noexcept(see below); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric template <class T> variant& operator=(T&&) noexcept(see below); 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric // 20.7.2.4, modifiers 540b57cec5SDimitry Andric template <class T, class... Args> 550b57cec5SDimitry Andric T& emplace(Args&&...); 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric template <class T, class U, class... Args> 580b57cec5SDimitry Andric T& emplace(initializer_list<U>, Args&&...); 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric template <size_t I, class... Args> 610b57cec5SDimitry Andric variant_alternative_t<I, variant>& emplace(Args&&...); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric template <size_t I, class U, class... Args> 640b57cec5SDimitry Andric variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // 20.7.2.5, value status 670b57cec5SDimitry Andric constexpr bool valueless_by_exception() const noexcept; 680b57cec5SDimitry Andric constexpr size_t index() const noexcept; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // 20.7.2.6, swap 710b57cec5SDimitry Andric void swap(variant&) noexcept(see below); 720b57cec5SDimitry Andric }; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // 20.7.3, variant helper classes 750b57cec5SDimitry Andric template <class T> struct variant_size; // undefined 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric template <class T> 780b57cec5SDimitry Andric inline constexpr size_t variant_size_v = variant_size<T>::value; 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric template <class T> struct variant_size<const T>; 810b57cec5SDimitry Andric template <class T> struct variant_size<volatile T>; 820b57cec5SDimitry Andric template <class T> struct variant_size<const volatile T>; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric template <class... Types> 850b57cec5SDimitry Andric struct variant_size<variant<Types...>>; 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative; // undefined 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric template <size_t I, class T> 900b57cec5SDimitry Andric using variant_alternative_t = typename variant_alternative<I, T>::type; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative<I, const T>; 930b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative<I, volatile T>; 940b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative<I, const volatile T>; 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric template <size_t I, class... Types> 970b57cec5SDimitry Andric struct variant_alternative<I, variant<Types...>>; 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric inline constexpr size_t variant_npos = -1; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric // 20.7.4, value access 1020b57cec5SDimitry Andric template <class T, class... Types> 1030b57cec5SDimitry Andric constexpr bool holds_alternative(const variant<Types...>&) noexcept; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric template <size_t I, class... Types> 1060b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>>& 1070b57cec5SDimitry Andric get(variant<Types...>&); 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric template <size_t I, class... Types> 1100b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>>&& 1110b57cec5SDimitry Andric get(variant<Types...>&&); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric template <size_t I, class... Types> 1140b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>> const& 1150b57cec5SDimitry Andric get(const variant<Types...>&); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric template <size_t I, class... Types> 1180b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>> const&& 1190b57cec5SDimitry Andric get(const variant<Types...>&&); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric template <class T, class... Types> 1220b57cec5SDimitry Andric constexpr T& get(variant<Types...>&); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric template <class T, class... Types> 1250b57cec5SDimitry Andric constexpr T&& get(variant<Types...>&&); 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric template <class T, class... Types> 1280b57cec5SDimitry Andric constexpr const T& get(const variant<Types...>&); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric template <class T, class... Types> 1310b57cec5SDimitry Andric constexpr const T&& get(const variant<Types...>&&); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric template <size_t I, class... Types> 1340b57cec5SDimitry Andric constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>> 1350b57cec5SDimitry Andric get_if(variant<Types...>*) noexcept; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric template <size_t I, class... Types> 1380b57cec5SDimitry Andric constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>> 1390b57cec5SDimitry Andric get_if(const variant<Types...>*) noexcept; 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric template <class T, class... Types> 1420b57cec5SDimitry Andric constexpr add_pointer_t<T> 1430b57cec5SDimitry Andric get_if(variant<Types...>*) noexcept; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric template <class T, class... Types> 1460b57cec5SDimitry Andric constexpr add_pointer_t<const T> 1470b57cec5SDimitry Andric get_if(const variant<Types...>*) noexcept; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric // 20.7.5, relational operators 1500b57cec5SDimitry Andric template <class... Types> 1510b57cec5SDimitry Andric constexpr bool operator==(const variant<Types...>&, const variant<Types...>&); 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric template <class... Types> 1540b57cec5SDimitry Andric constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&); 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric template <class... Types> 1570b57cec5SDimitry Andric constexpr bool operator<(const variant<Types...>&, const variant<Types...>&); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric template <class... Types> 1600b57cec5SDimitry Andric constexpr bool operator>(const variant<Types...>&, const variant<Types...>&); 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric template <class... Types> 1630b57cec5SDimitry Andric constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric template <class... Types> 1660b57cec5SDimitry Andric constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&); 1670b57cec5SDimitry Andric 168bdd1243dSDimitry Andric template <class... Types> requires (three_way_comparable<Types> && ...) 169bdd1243dSDimitry Andric constexpr common_comparison_category_t<compare_three_way_result_t<Types>...> 170bdd1243dSDimitry Andric operator<=>(const variant<Types...>&, const variant<Types...>&); // since C++20 171bdd1243dSDimitry Andric 1720b57cec5SDimitry Andric // 20.7.6, visitation 1730b57cec5SDimitry Andric template <class Visitor, class... Variants> 1740b57cec5SDimitry Andric constexpr see below visit(Visitor&&, Variants&&...); 1750b57cec5SDimitry Andric 176e8d8bef9SDimitry Andric template <class R, class Visitor, class... Variants> 177e8d8bef9SDimitry Andric constexpr R visit(Visitor&&, Variants&&...); // since C++20 178e8d8bef9SDimitry Andric 1790b57cec5SDimitry Andric // 20.7.7, class monostate 1800b57cec5SDimitry Andric struct monostate; 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric // 20.7.8, monostate relational operators 1830b57cec5SDimitry Andric constexpr bool operator==(monostate, monostate) noexcept; 184bdd1243dSDimitry Andric constexpr bool operator!=(monostate, monostate) noexcept; // until C++20 185bdd1243dSDimitry Andric constexpr bool operator<(monostate, monostate) noexcept; // until C++20 186bdd1243dSDimitry Andric constexpr bool operator>(monostate, monostate) noexcept; // until C++20 187bdd1243dSDimitry Andric constexpr bool operator<=(monostate, monostate) noexcept; // until C++20 188bdd1243dSDimitry Andric constexpr bool operator>=(monostate, monostate) noexcept; // until C++20 189bdd1243dSDimitry Andric constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric // 20.7.9, specialized algorithms 1920b57cec5SDimitry Andric template <class... Types> 1930b57cec5SDimitry Andric void swap(variant<Types...>&, variant<Types...>&) noexcept(see below); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric // 20.7.10, class bad_variant_access 1960b57cec5SDimitry Andric class bad_variant_access; 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric // 20.7.11, hash support 1990b57cec5SDimitry Andric template <class T> struct hash; 2000b57cec5SDimitry Andric template <class... Types> struct hash<variant<Types...>>; 2010b57cec5SDimitry Andric template <> struct hash<monostate>; 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric} // namespace std 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric*/ 2060b57cec5SDimitry Andric 20781ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 208e8d8bef9SDimitry Andric#include <__availability> 209bdd1243dSDimitry Andric#include <__compare/common_comparison_category.h> 210bdd1243dSDimitry Andric#include <__compare/compare_three_way_result.h> 211bdd1243dSDimitry Andric#include <__compare/three_way_comparable.h> 212fe6060f1SDimitry Andric#include <__config> 21306c3fb27SDimitry Andric#include <__exception/exception.h> 214fe6060f1SDimitry Andric#include <__functional/hash.h> 215bdd1243dSDimitry Andric#include <__functional/invoke.h> 21681ad6265SDimitry Andric#include <__functional/operations.h> 21781ad6265SDimitry Andric#include <__functional/unary_function.h> 21806c3fb27SDimitry Andric#include <__memory/addressof.h> 219bdd1243dSDimitry Andric#include <__type_traits/add_const.h> 220bdd1243dSDimitry Andric#include <__type_traits/add_cv.h> 221bdd1243dSDimitry Andric#include <__type_traits/add_pointer.h> 222bdd1243dSDimitry Andric#include <__type_traits/add_volatile.h> 223bdd1243dSDimitry Andric#include <__type_traits/dependent_type.h> 224bdd1243dSDimitry Andric#include <__type_traits/is_array.h> 225bdd1243dSDimitry Andric#include <__type_traits/is_destructible.h> 226bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_move_constructible.h> 227bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_assignable.h> 228bdd1243dSDimitry Andric#include <__type_traits/is_trivially_copy_constructible.h> 229bdd1243dSDimitry Andric#include <__type_traits/is_trivially_destructible.h> 230bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_assignable.h> 231bdd1243dSDimitry Andric#include <__type_traits/is_trivially_move_constructible.h> 232bdd1243dSDimitry Andric#include <__type_traits/is_void.h> 233bdd1243dSDimitry Andric#include <__type_traits/remove_const.h> 234bdd1243dSDimitry Andric#include <__type_traits/type_identity.h> 235bdd1243dSDimitry Andric#include <__type_traits/void_t.h> 23606c3fb27SDimitry Andric#include <__utility/declval.h> 237fe6060f1SDimitry Andric#include <__utility/forward.h> 23881ad6265SDimitry Andric#include <__utility/in_place.h> 23981ad6265SDimitry Andric#include <__utility/move.h> 24081ad6265SDimitry Andric#include <__utility/swap.h> 241fe6060f1SDimitry Andric#include <__variant/monostate.h> 24206c3fb27SDimitry Andric#include <__verbose_abort> 2430b57cec5SDimitry Andric#include <initializer_list> 244fe6060f1SDimitry Andric#include <limits> 2450b57cec5SDimitry Andric#include <new> 2460b57cec5SDimitry Andric#include <tuple> 2470b57cec5SDimitry Andric#include <version> 2480b57cec5SDimitry Andric 24981ad6265SDimitry Andric// standard-mandated includes 250bdd1243dSDimitry Andric 251bdd1243dSDimitry Andric// [variant.syn] 25281ad6265SDimitry Andric#include <compare> 25381ad6265SDimitry Andric 2540b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 2550b57cec5SDimitry Andric# pragma GCC system_header 2560b57cec5SDimitry Andric#endif 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 2590b57cec5SDimitry Andric#include <__undef_macros> 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andricnamespace std { // explicitly not using versioning namespace 2620b57cec5SDimitry Andric 26306c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception { 2640b57cec5SDimitry Andricpublic: 265bdd1243dSDimitry Andric const char* what() const _NOEXCEPT override; 2660b57cec5SDimitry Andric}; 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric} // namespace std 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 2710b57cec5SDimitry Andric 27206c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17 2730b57cec5SDimitry Andric 274fe6060f1SDimitry Andric// Light N-dimensional array of function pointers. Used in place of std::array to avoid 275fe6060f1SDimitry Andric// adding a dependency. 276fe6060f1SDimitry Andrictemplate<class _Tp, size_t _Size> 277fe6060f1SDimitry Andricstruct __farray { 278fe6060f1SDimitry Andric static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit"); 279fe6060f1SDimitry Andric _Tp __buf_[_Size] = {}; 280fe6060f1SDimitry Andric 281*5f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr 282fe6060f1SDimitry Andric const _Tp &operator[](size_t __n) const noexcept { 283fe6060f1SDimitry Andric return __buf_[__n]; 284fe6060f1SDimitry Andric } 285fe6060f1SDimitry Andric}; 286fe6060f1SDimitry Andric 2870b57cec5SDimitry Andric_LIBCPP_NORETURN 288bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI 2890b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 2900b57cec5SDimitry Andricvoid __throw_bad_variant_access() { 29106c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 2920b57cec5SDimitry Andric throw bad_variant_access(); 2930b57cec5SDimitry Andric#else 29406c3fb27SDimitry Andric _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode"); 2950b57cec5SDimitry Andric#endif 2960b57cec5SDimitry Andric} 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andrictemplate <class... _Types> 2990b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant; 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andrictemplate <class _Tp> 3020b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size; 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andrictemplate <class _Tp> 305349cc55cSDimitry Andricinline constexpr size_t variant_size_v = variant_size<_Tp>::value; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andrictemplate <class _Tp> 3080b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {}; 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andrictemplate <class _Tp> 3110b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {}; 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andrictemplate <class _Tp> 3140b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> 3150b57cec5SDimitry Andric : variant_size<_Tp> {}; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andrictemplate <class... _Types> 3180b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> 3190b57cec5SDimitry Andric : integral_constant<size_t, sizeof...(_Types)> {}; 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 3220b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative; 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 3250b57cec5SDimitry Andricusing variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type; 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 3280b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> 3290b57cec5SDimitry Andric : add_const<variant_alternative_t<_Ip, _Tp>> {}; 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 3320b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> 3330b57cec5SDimitry Andric : add_volatile<variant_alternative_t<_Ip, _Tp>> {}; 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 3360b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> 3370b57cec5SDimitry Andric : add_cv<variant_alternative_t<_Ip, _Tp>> {}; 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 3400b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> { 3410b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>"); 3420b57cec5SDimitry Andric using type = __type_pack_element<_Ip, _Types...>; 3430b57cec5SDimitry Andric}; 3440b57cec5SDimitry Andric 345349cc55cSDimitry Andricinline constexpr size_t variant_npos = static_cast<size_t>(-1); 3460b57cec5SDimitry Andric 347bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int __choose_index_type(unsigned int __num_elem) { 348e8d8bef9SDimitry Andric if (__num_elem < numeric_limits<unsigned char>::max()) 3490b57cec5SDimitry Andric return 0; 350e8d8bef9SDimitry Andric if (__num_elem < numeric_limits<unsigned short>::max()) 3510b57cec5SDimitry Andric return 1; 3520b57cec5SDimitry Andric return 2; 3530b57cec5SDimitry Andric} 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andrictemplate <size_t _NumAlts> 3560b57cec5SDimitry Andricusing __variant_index_t = 3570b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION 3580b57cec5SDimitry Andric unsigned int; 3590b57cec5SDimitry Andric#else 3600b57cec5SDimitry Andric std::tuple_element_t< 3610b57cec5SDimitry Andric __choose_index_type(_NumAlts), 3620b57cec5SDimitry Andric std::tuple<unsigned char, unsigned short, unsigned int> 3630b57cec5SDimitry Andric >; 3640b57cec5SDimitry Andric#endif 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andrictemplate <class _IndexType> 3670b57cec5SDimitry Andricconstexpr _IndexType __variant_npos = static_cast<_IndexType>(-1); 3680b57cec5SDimitry Andric 369fe6060f1SDimitry Andrictemplate <class... _Types> 370fe6060f1SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant; 371fe6060f1SDimitry Andric 372fe6060f1SDimitry Andrictemplate <class... _Types> 373*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& 374fe6060f1SDimitry Andric__as_variant(variant<_Types...>& __vs) noexcept { 375fe6060f1SDimitry Andric return __vs; 376fe6060f1SDimitry Andric} 377fe6060f1SDimitry Andric 378fe6060f1SDimitry Andrictemplate <class... _Types> 379*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& 380fe6060f1SDimitry Andric__as_variant(const variant<_Types...>& __vs) noexcept { 381fe6060f1SDimitry Andric return __vs; 382fe6060f1SDimitry Andric} 383fe6060f1SDimitry Andric 384fe6060f1SDimitry Andrictemplate <class... _Types> 385*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& 386fe6060f1SDimitry Andric__as_variant(variant<_Types...>&& __vs) noexcept { 387*5f757f3fSDimitry Andric return std::move(__vs); 388fe6060f1SDimitry Andric} 389fe6060f1SDimitry Andric 390fe6060f1SDimitry Andrictemplate <class... _Types> 391*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& 392fe6060f1SDimitry Andric__as_variant(const variant<_Types...>&& __vs) noexcept { 393*5f757f3fSDimitry Andric return std::move(__vs); 394fe6060f1SDimitry Andric} 395fe6060f1SDimitry Andric 3960b57cec5SDimitry Andricnamespace __find_detail { 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 399bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 4000b57cec5SDimitry Andricconstexpr size_t __find_index() { 4010b57cec5SDimitry Andric constexpr bool __matches[] = {is_same_v<_Tp, _Types>...}; 4020b57cec5SDimitry Andric size_t __result = __not_found; 4030b57cec5SDimitry Andric for (size_t __i = 0; __i < sizeof...(_Types); ++__i) { 4040b57cec5SDimitry Andric if (__matches[__i]) { 4050b57cec5SDimitry Andric if (__result != __not_found) { 4060b57cec5SDimitry Andric return __ambiguous; 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric __result = __i; 4090b57cec5SDimitry Andric } 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric return __result; 4120b57cec5SDimitry Andric} 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andrictemplate <size_t _Index> 4150b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl 4160b57cec5SDimitry Andric : integral_constant<size_t, _Index> {}; 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andrictemplate <> 4190b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__not_found> {}; 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andrictemplate <> 4220b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__ambiguous> {}; 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 4250b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae 4260b57cec5SDimitry Andric : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {}; 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric} // namespace __find_detail 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andricnamespace __variant_detail { 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andricstruct __valueless_t {}; 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andricenum class _Trait { _TriviallyAvailable, _Available, _Unavailable }; 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andrictemplate <typename _Tp, 4370b57cec5SDimitry Andric template <typename> class _IsTriviallyAvailable, 4380b57cec5SDimitry Andric template <typename> class _IsAvailable> 4390b57cec5SDimitry Andricconstexpr _Trait __trait = 4400b57cec5SDimitry Andric _IsTriviallyAvailable<_Tp>::value 4410b57cec5SDimitry Andric ? _Trait::_TriviallyAvailable 4420b57cec5SDimitry Andric : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable; 4430b57cec5SDimitry Andric 444bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 4450b57cec5SDimitry Andricconstexpr _Trait __common_trait(initializer_list<_Trait> __traits) { 4460b57cec5SDimitry Andric _Trait __result = _Trait::_TriviallyAvailable; 4470b57cec5SDimitry Andric for (_Trait __t : __traits) { 4480b57cec5SDimitry Andric if (static_cast<int>(__t) > static_cast<int>(__result)) { 4490b57cec5SDimitry Andric __result = __t; 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric } 4520b57cec5SDimitry Andric return __result; 4530b57cec5SDimitry Andric} 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andrictemplate <typename... _Types> 4560b57cec5SDimitry Andricstruct __traits { 4570b57cec5SDimitry Andric static constexpr _Trait __copy_constructible_trait = 458bdd1243dSDimitry Andric __variant_detail::__common_trait({__trait<_Types, 4590b57cec5SDimitry Andric is_trivially_copy_constructible, 4600b57cec5SDimitry Andric is_copy_constructible>...}); 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric static constexpr _Trait __move_constructible_trait = 463bdd1243dSDimitry Andric __variant_detail::__common_trait({__trait<_Types, 4640b57cec5SDimitry Andric is_trivially_move_constructible, 4650b57cec5SDimitry Andric is_move_constructible>...}); 4660b57cec5SDimitry Andric 467bdd1243dSDimitry Andric static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait( 4680b57cec5SDimitry Andric {__copy_constructible_trait, 4690b57cec5SDimitry Andric __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...}); 4700b57cec5SDimitry Andric 471bdd1243dSDimitry Andric static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait( 4720b57cec5SDimitry Andric {__move_constructible_trait, 4730b57cec5SDimitry Andric __trait<_Types, is_trivially_move_assignable, is_move_assignable>...}); 4740b57cec5SDimitry Andric 475bdd1243dSDimitry Andric static constexpr _Trait __destructible_trait = __variant_detail::__common_trait( 4760b57cec5SDimitry Andric {__trait<_Types, is_trivially_destructible, is_destructible>...}); 4770b57cec5SDimitry Andric}; 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andricnamespace __access { 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andricstruct __union { 4820b57cec5SDimitry Andric template <class _Vp> 483bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 4840b57cec5SDimitry Andric static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) { 485*5f757f3fSDimitry Andric return std::forward<_Vp>(__v).__head; 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric template <class _Vp, size_t _Ip> 489bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 4900b57cec5SDimitry Andric static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) { 491*5f757f3fSDimitry Andric return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>); 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric}; 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andricstruct __base { 4960b57cec5SDimitry Andric template <size_t _Ip, class _Vp> 497bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 4980b57cec5SDimitry Andric static constexpr auto&& __get_alt(_Vp&& __v) { 499*5f757f3fSDimitry Andric return __union::__get_alt(std::forward<_Vp>(__v).__data, 5000b57cec5SDimitry Andric in_place_index<_Ip>); 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric}; 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andricstruct __variant { 5050b57cec5SDimitry Andric template <size_t _Ip, class _Vp> 506bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5070b57cec5SDimitry Andric static constexpr auto&& __get_alt(_Vp&& __v) { 508*5f757f3fSDimitry Andric return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric}; 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric} // namespace __access 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andricnamespace __visitation { 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andricstruct __base { 5170b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 518bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5190b57cec5SDimitry Andric static constexpr decltype(auto) 5200b57cec5SDimitry Andric __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 5210b57cec5SDimitry Andric constexpr auto __fdiagonal = 5220b57cec5SDimitry Andric __make_fdiagonal<_Visitor&&, 523*5f757f3fSDimitry Andric decltype(std::forward<_Vs>(__vs).__as_base())...>(); 524*5f757f3fSDimitry Andric return __fdiagonal[__index](std::forward<_Visitor>(__visitor), 525*5f757f3fSDimitry Andric std::forward<_Vs>(__vs).__as_base()...); 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 529bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5300b57cec5SDimitry Andric static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, 5310b57cec5SDimitry Andric _Vs&&... __vs) { 5320b57cec5SDimitry Andric constexpr auto __fmatrix = 5330b57cec5SDimitry Andric __make_fmatrix<_Visitor&&, 534*5f757f3fSDimitry Andric decltype(std::forward<_Vs>(__vs).__as_base())...>(); 5350b57cec5SDimitry Andric return __at(__fmatrix, __vs.index()...)( 536*5f757f3fSDimitry Andric std::forward<_Visitor>(__visitor), 537*5f757f3fSDimitry Andric std::forward<_Vs>(__vs).__as_base()...); 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andricprivate: 5410b57cec5SDimitry Andric template <class _Tp> 542bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5430b57cec5SDimitry Andric static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; } 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric template <class _Tp, size_t _Np, typename... _Indices> 546bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 547fe6060f1SDimitry Andric static constexpr auto&& __at(const __farray<_Tp, _Np>& __elems, 5480b57cec5SDimitry Andric size_t __index, _Indices... __indices) { 5490b57cec5SDimitry Andric return __at(__elems[__index], __indices...); 5500b57cec5SDimitry Andric } 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric template <class _Fp, class... _Fs> 55306c3fb27SDimitry Andric static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() { 5540b57cec5SDimitry Andric static_assert( 5550b57cec5SDimitry Andric __all<is_same_v<_Fp, _Fs>...>::value, 5560b57cec5SDimitry Andric "`std::visit` requires the visitor to have a single return type."); 5570b57cec5SDimitry Andric } 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric template <class... _Fs> 560bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5610b57cec5SDimitry Andric static constexpr auto __make_farray(_Fs&&... __fs) { 562bdd1243dSDimitry Andric __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>(); 563bdd1243dSDimitry Andric using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>; 564*5f757f3fSDimitry Andric return __result{{std::forward<_Fs>(__fs)...}}; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric 567e8d8bef9SDimitry Andric template <size_t... _Is> 5680b57cec5SDimitry Andric struct __dispatcher { 5690b57cec5SDimitry Andric template <class _Fp, class... _Vs> 570bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5710b57cec5SDimitry Andric static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) { 572*5f757f3fSDimitry Andric return std::__invoke( 5730b57cec5SDimitry Andric static_cast<_Fp>(__f), 5740b57cec5SDimitry Andric __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...); 5750b57cec5SDimitry Andric } 5760b57cec5SDimitry Andric }; 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is> 579bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5800b57cec5SDimitry Andric static constexpr auto __make_dispatch(index_sequence<_Is...>) { 5810b57cec5SDimitry Andric return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>; 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric template <size_t _Ip, class _Fp, class... _Vs> 585bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5860b57cec5SDimitry Andric static constexpr auto __make_fdiagonal_impl() { 5870b57cec5SDimitry Andric return __make_dispatch<_Fp, _Vs...>( 58881ad6265SDimitry Andric index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{}); 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is> 592bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5930b57cec5SDimitry Andric static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) { 5940b57cec5SDimitry Andric return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...); 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric template <class _Fp, class _Vp, class... _Vs> 598bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 5990b57cec5SDimitry Andric static constexpr auto __make_fdiagonal() { 60006c3fb27SDimitry Andric constexpr size_t __np = __remove_cvref_t<_Vp>::__size(); 60106c3fb27SDimitry Andric static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value); 60206c3fb27SDimitry Andric return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{}); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is> 606bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6070b57cec5SDimitry Andric static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) { 6080b57cec5SDimitry Andric return __make_dispatch<_Fp, _Vs...>(__is); 6090b57cec5SDimitry Andric } 6100b57cec5SDimitry Andric 6110b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls> 612bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6130b57cec5SDimitry Andric static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>, 6140b57cec5SDimitry Andric index_sequence<_Js...>, 6150b57cec5SDimitry Andric _Ls... __ls) { 6160b57cec5SDimitry Andric return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>( 6170b57cec5SDimitry Andric index_sequence<_Is..., _Js>{}, __ls...)...); 6180b57cec5SDimitry Andric } 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric template <class _Fp, class... _Vs> 621bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6220b57cec5SDimitry Andric static constexpr auto __make_fmatrix() { 6230b57cec5SDimitry Andric return __make_fmatrix_impl<_Fp, _Vs...>( 624bdd1243dSDimitry Andric index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...); 6250b57cec5SDimitry Andric } 6260b57cec5SDimitry Andric}; 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andricstruct __variant { 6290b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 630bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6310b57cec5SDimitry Andric static constexpr decltype(auto) 6320b57cec5SDimitry Andric __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 6330b57cec5SDimitry Andric return __base::__visit_alt_at(__index, 634*5f757f3fSDimitry Andric std::forward<_Visitor>(__visitor), 635*5f757f3fSDimitry Andric std::forward<_Vs>(__vs).__impl_...); 6360b57cec5SDimitry Andric } 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 639bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6400b57cec5SDimitry Andric static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, 6410b57cec5SDimitry Andric _Vs&&... __vs) { 642fe6060f1SDimitry Andric return __base::__visit_alt( 643*5f757f3fSDimitry Andric std::forward<_Visitor>(__visitor), 644*5f757f3fSDimitry Andric std::__as_variant(std::forward<_Vs>(__vs)).__impl_...); 6450b57cec5SDimitry Andric } 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 648bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6490b57cec5SDimitry Andric static constexpr decltype(auto) 6500b57cec5SDimitry Andric __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 6510b57cec5SDimitry Andric return __visit_alt_at( 6520b57cec5SDimitry Andric __index, 653*5f757f3fSDimitry Andric __make_value_visitor(std::forward<_Visitor>(__visitor)), 654*5f757f3fSDimitry Andric std::forward<_Vs>(__vs)...); 6550b57cec5SDimitry Andric } 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 658bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6590b57cec5SDimitry Andric static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, 6600b57cec5SDimitry Andric _Vs&&... __vs) { 6610b57cec5SDimitry Andric return __visit_alt( 662*5f757f3fSDimitry Andric __make_value_visitor(std::forward<_Visitor>(__visitor)), 663*5f757f3fSDimitry Andric std::forward<_Vs>(__vs)...); 6640b57cec5SDimitry Andric } 665fe6060f1SDimitry Andric 66606c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20 667e8d8bef9SDimitry Andric template <class _Rp, class _Visitor, class... _Vs> 668bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 669e8d8bef9SDimitry Andric static constexpr _Rp __visit_value(_Visitor&& __visitor, 670e8d8bef9SDimitry Andric _Vs&&... __vs) { 671e8d8bef9SDimitry Andric return __visit_alt( 672*5f757f3fSDimitry Andric __make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), 673*5f757f3fSDimitry Andric std::forward<_Vs>(__vs)...); 674e8d8bef9SDimitry Andric } 675e8d8bef9SDimitry Andric#endif 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andricprivate: 6780b57cec5SDimitry Andric template <class _Visitor, class... _Values> 67906c3fb27SDimitry Andric static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() { 6800b57cec5SDimitry Andric static_assert(is_invocable_v<_Visitor, _Values...>, 6810b57cec5SDimitry Andric "`std::visit` requires the visitor to be exhaustive."); 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric template <class _Visitor> 6850b57cec5SDimitry Andric struct __value_visitor { 6860b57cec5SDimitry Andric template <class... _Alts> 687bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 6880b57cec5SDimitry Andric constexpr decltype(auto) operator()(_Alts&&... __alts) const { 6890b57cec5SDimitry Andric __std_visit_exhaustive_visitor_check< 6900b57cec5SDimitry Andric _Visitor, 691*5f757f3fSDimitry Andric decltype((std::forward<_Alts>(__alts).__value))...>(); 692*5f757f3fSDimitry Andric return std::__invoke(std::forward<_Visitor>(__visitor), 693*5f757f3fSDimitry Andric std::forward<_Alts>(__alts).__value...); 6940b57cec5SDimitry Andric } 6950b57cec5SDimitry Andric _Visitor&& __visitor; 6960b57cec5SDimitry Andric }; 6970b57cec5SDimitry Andric 69806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20 699e8d8bef9SDimitry Andric template <class _Rp, class _Visitor> 700e8d8bef9SDimitry Andric struct __value_visitor_return_type { 701e8d8bef9SDimitry Andric template <class... _Alts> 702bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 703e8d8bef9SDimitry Andric constexpr _Rp operator()(_Alts&&... __alts) const { 704e8d8bef9SDimitry Andric __std_visit_exhaustive_visitor_check< 705e8d8bef9SDimitry Andric _Visitor, 706*5f757f3fSDimitry Andric decltype((std::forward<_Alts>(__alts).__value))...>(); 707e8d8bef9SDimitry Andric if constexpr (is_void_v<_Rp>) { 708*5f757f3fSDimitry Andric std::__invoke(std::forward<_Visitor>(__visitor), 709*5f757f3fSDimitry Andric std::forward<_Alts>(__alts).__value...); 710e8d8bef9SDimitry Andric } 711e8d8bef9SDimitry Andric else { 712*5f757f3fSDimitry Andric return std::__invoke(std::forward<_Visitor>(__visitor), 713*5f757f3fSDimitry Andric std::forward<_Alts>(__alts).__value...); 714e8d8bef9SDimitry Andric } 715e8d8bef9SDimitry Andric } 716e8d8bef9SDimitry Andric 717e8d8bef9SDimitry Andric _Visitor&& __visitor; 718e8d8bef9SDimitry Andric }; 719e8d8bef9SDimitry Andric#endif 720e8d8bef9SDimitry Andric 7210b57cec5SDimitry Andric template <class _Visitor> 722bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 7230b57cec5SDimitry Andric static constexpr auto __make_value_visitor(_Visitor&& __visitor) { 724*5f757f3fSDimitry Andric return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)}; 7250b57cec5SDimitry Andric } 726e8d8bef9SDimitry Andric 72706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20 728e8d8bef9SDimitry Andric template <class _Rp, class _Visitor> 729bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 730e8d8bef9SDimitry Andric static constexpr auto __make_value_visitor(_Visitor&& __visitor) { 731*5f757f3fSDimitry Andric return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)}; 732e8d8bef9SDimitry Andric } 733e8d8bef9SDimitry Andric#endif 7340b57cec5SDimitry Andric}; 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric} // namespace __visitation 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andrictemplate <size_t _Index, class _Tp> 7390b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __alt { 7400b57cec5SDimitry Andric using __value_type = _Tp; 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric template <class... _Args> 743bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 7440b57cec5SDimitry Andric explicit constexpr __alt(in_place_t, _Args&&... __args) 745*5f757f3fSDimitry Andric : __value(std::forward<_Args>(__args)...) {} 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric __value_type __value; 7480b57cec5SDimitry Andric}; 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index, class... _Types> 7510b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union; 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index> 7540b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {}; 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \ 7570b57cec5SDimitry Andric template <size_t _Index, class _Tp, class... _Types> \ 7580b57cec5SDimitry Andric union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \ 7590b57cec5SDimitry Andric _Index, \ 7600b57cec5SDimitry Andric _Tp, \ 7610b57cec5SDimitry Andric _Types...> { \ 7620b57cec5SDimitry Andric public: \ 763bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI \ 7640b57cec5SDimitry Andric explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \ 7650b57cec5SDimitry Andric \ 7660b57cec5SDimitry Andric template <class... _Args> \ 767bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI \ 7680b57cec5SDimitry Andric explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \ 769*5f757f3fSDimitry Andric : __head(in_place, std::forward<_Args>(__args)...) {} \ 7700b57cec5SDimitry Andric \ 7710b57cec5SDimitry Andric template <size_t _Ip, class... _Args> \ 772bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI \ 7730b57cec5SDimitry Andric explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \ 774*5f757f3fSDimitry Andric : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {} \ 7750b57cec5SDimitry Andric \ 7760b57cec5SDimitry Andric __union(const __union&) = default; \ 7770b57cec5SDimitry Andric __union(__union&&) = default; \ 7780b57cec5SDimitry Andric \ 7790b57cec5SDimitry Andric destructor \ 7800b57cec5SDimitry Andric \ 7810b57cec5SDimitry Andric __union& operator=(const __union&) = default; \ 7820b57cec5SDimitry Andric __union& operator=(__union&&) = default; \ 7830b57cec5SDimitry Andric \ 7840b57cec5SDimitry Andric private: \ 7850b57cec5SDimitry Andric char __dummy; \ 7860b57cec5SDimitry Andric __alt<_Index, _Tp> __head; \ 7870b57cec5SDimitry Andric __union<destructible_trait, _Index + 1, _Types...> __tail; \ 7880b57cec5SDimitry Andric \ 7890b57cec5SDimitry Andric friend struct __access::__union; \ 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;); 7930b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {}); 7940b57cec5SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;); 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_UNION 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, class... _Types> 7990b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __base { 8000b57cec5SDimitry Andricpublic: 8010b57cec5SDimitry Andric using __index_t = __variant_index_t<sizeof...(_Types)>; 8020b57cec5SDimitry Andric 803bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 804753f127fSDimitry Andric explicit constexpr __base(__valueless_t __tag) noexcept 805753f127fSDimitry Andric : __data(__tag), __index(__variant_npos<__index_t>) {} 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric template <size_t _Ip, class... _Args> 808bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 8090b57cec5SDimitry Andric explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args) 8100b57cec5SDimitry Andric : 811*5f757f3fSDimitry Andric __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), 8120b57cec5SDimitry Andric __index(_Ip) {} 8130b57cec5SDimitry Andric 814bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 8150b57cec5SDimitry Andric constexpr bool valueless_by_exception() const noexcept { 8160b57cec5SDimitry Andric return index() == variant_npos; 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric 819bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 8200b57cec5SDimitry Andric constexpr size_t index() const noexcept { 8210b57cec5SDimitry Andric return __index == __variant_npos<__index_t> ? variant_npos : __index; 8220b57cec5SDimitry Andric } 8230b57cec5SDimitry Andric 8240b57cec5SDimitry Andricprotected: 825bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 8260b57cec5SDimitry Andric constexpr auto&& __as_base() & { return *this; } 8270b57cec5SDimitry Andric 828bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 829*5f757f3fSDimitry Andric constexpr auto&& __as_base() && { return std::move(*this); } 8300b57cec5SDimitry Andric 831bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 8320b57cec5SDimitry Andric constexpr auto&& __as_base() const & { return *this; } 8330b57cec5SDimitry Andric 834bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 835*5f757f3fSDimitry Andric constexpr auto&& __as_base() const && { return std::move(*this); } 8360b57cec5SDimitry Andric 837bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 8380b57cec5SDimitry Andric static constexpr size_t __size() { return sizeof...(_Types); } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric __union<_DestructibleTrait, 0, _Types...> __data; 8410b57cec5SDimitry Andric __index_t __index; 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric friend struct __access::__base; 8440b57cec5SDimitry Andric friend struct __visitation::__base; 8450b57cec5SDimitry Andric}; 8460b57cec5SDimitry Andric 8470b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__destructible_trait> 848e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __dtor; 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \ 8510b57cec5SDimitry Andric template <class... _Types> \ 852e8d8bef9SDimitry Andric class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, \ 8530b57cec5SDimitry Andric destructible_trait> \ 8540b57cec5SDimitry Andric : public __base<destructible_trait, _Types...> { \ 8550b57cec5SDimitry Andric using __base_type = __base<destructible_trait, _Types...>; \ 8560b57cec5SDimitry Andric using __index_t = typename __base_type::__index_t; \ 8570b57cec5SDimitry Andric \ 8580b57cec5SDimitry Andric public: \ 8590b57cec5SDimitry Andric using __base_type::__base_type; \ 8600b57cec5SDimitry Andric using __base_type::operator=; \ 8610b57cec5SDimitry Andric \ 862e8d8bef9SDimitry Andric __dtor(const __dtor&) = default; \ 863e8d8bef9SDimitry Andric __dtor(__dtor&&) = default; \ 8640b57cec5SDimitry Andric destructor \ 865e8d8bef9SDimitry Andric __dtor& operator=(const __dtor&) = default; \ 866e8d8bef9SDimitry Andric __dtor& operator=(__dtor&&) = default; \ 8670b57cec5SDimitry Andric \ 8680b57cec5SDimitry Andric protected: \ 869bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI \ 8700b57cec5SDimitry Andric destroy \ 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR( 8740b57cec5SDimitry Andric _Trait::_TriviallyAvailable, 875e8d8bef9SDimitry Andric ~__dtor() = default;, 8760b57cec5SDimitry Andric void __destroy() noexcept { this->__index = __variant_npos<__index_t>; }); 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR( 8790b57cec5SDimitry Andric _Trait::_Available, 880e8d8bef9SDimitry Andric ~__dtor() { __destroy(); }, 8810b57cec5SDimitry Andric void __destroy() noexcept { 8820b57cec5SDimitry Andric if (!this->valueless_by_exception()) { 8830b57cec5SDimitry Andric __visitation::__base::__visit_alt( 8840b57cec5SDimitry Andric [](auto& __alt) noexcept { 885bdd1243dSDimitry Andric using __alt_type = __remove_cvref_t<decltype(__alt)>; 8860b57cec5SDimitry Andric __alt.~__alt_type(); 8870b57cec5SDimitry Andric }, 8880b57cec5SDimitry Andric *this); 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric this->__index = __variant_npos<__index_t>; 8910b57cec5SDimitry Andric }); 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR( 8940b57cec5SDimitry Andric _Trait::_Unavailable, 895e8d8bef9SDimitry Andric ~__dtor() = delete;, 8960b57cec5SDimitry Andric void __destroy() noexcept = delete;); 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_DESTRUCTOR 8990b57cec5SDimitry Andric 9000b57cec5SDimitry Andrictemplate <class _Traits> 901e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> { 902e8d8bef9SDimitry Andric using __base_type = __dtor<_Traits>; 9030b57cec5SDimitry Andric 9040b57cec5SDimitry Andricpublic: 9050b57cec5SDimitry Andric using __base_type::__base_type; 9060b57cec5SDimitry Andric using __base_type::operator=; 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andricprotected: 9090b57cec5SDimitry Andric template <size_t _Ip, class _Tp, class... _Args> 910bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 9110b57cec5SDimitry Andric static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) { 912*5f757f3fSDimitry Andric ::new ((void*)std::addressof(__a)) 913*5f757f3fSDimitry Andric __alt<_Ip, _Tp>(in_place, std::forward<_Args>(__args)...); 9140b57cec5SDimitry Andric return __a.__value; 9150b57cec5SDimitry Andric } 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric template <class _Rhs> 918bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 919e8d8bef9SDimitry Andric static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) { 9200b57cec5SDimitry Andric __lhs.__destroy(); 9210b57cec5SDimitry Andric if (!__rhs.valueless_by_exception()) { 9220b57cec5SDimitry Andric __visitation::__base::__visit_alt_at( 9230b57cec5SDimitry Andric __rhs.index(), 9240b57cec5SDimitry Andric [](auto& __lhs_alt, auto&& __rhs_alt) { 9250b57cec5SDimitry Andric __construct_alt( 9260b57cec5SDimitry Andric __lhs_alt, 927*5f757f3fSDimitry Andric std::forward<decltype(__rhs_alt)>(__rhs_alt).__value); 9280b57cec5SDimitry Andric }, 929*5f757f3fSDimitry Andric __lhs, std::forward<_Rhs>(__rhs)); 9300b57cec5SDimitry Andric __lhs.__index = __rhs.index(); 9310b57cec5SDimitry Andric } 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric}; 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_constructible_trait> 9360b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_constructor; 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \ 9390b57cec5SDimitry Andric move_constructor) \ 9400b57cec5SDimitry Andric template <class... _Types> \ 9410b57cec5SDimitry Andric class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \ 9420b57cec5SDimitry Andric move_constructible_trait> \ 943e8d8bef9SDimitry Andric : public __ctor<__traits<_Types...>> { \ 944e8d8bef9SDimitry Andric using __base_type = __ctor<__traits<_Types...>>; \ 9450b57cec5SDimitry Andric \ 9460b57cec5SDimitry Andric public: \ 9470b57cec5SDimitry Andric using __base_type::__base_type; \ 9480b57cec5SDimitry Andric using __base_type::operator=; \ 9490b57cec5SDimitry Andric \ 9500b57cec5SDimitry Andric __move_constructor(const __move_constructor&) = default; \ 9510b57cec5SDimitry Andric move_constructor \ 9520b57cec5SDimitry Andric ~__move_constructor() = default; \ 9530b57cec5SDimitry Andric __move_constructor& operator=(const __move_constructor&) = default; \ 9540b57cec5SDimitry Andric __move_constructor& operator=(__move_constructor&&) = default; \ 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 9580b57cec5SDimitry Andric _Trait::_TriviallyAvailable, 9590b57cec5SDimitry Andric __move_constructor(__move_constructor&& __that) = default;); 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 9620b57cec5SDimitry Andric _Trait::_Available, 9630b57cec5SDimitry Andric __move_constructor(__move_constructor&& __that) noexcept( 9640b57cec5SDimitry Andric __all<is_nothrow_move_constructible_v<_Types>...>::value) 9650b57cec5SDimitry Andric : __move_constructor(__valueless_t{}) { 966*5f757f3fSDimitry Andric this->__generic_construct(*this, std::move(__that)); 9670b57cec5SDimitry Andric }); 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 9700b57cec5SDimitry Andric _Trait::_Unavailable, 9710b57cec5SDimitry Andric __move_constructor(__move_constructor&&) = delete;); 9720b57cec5SDimitry Andric 9730b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_constructible_trait> 9760b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_constructor; 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \ 9790b57cec5SDimitry Andric copy_constructor) \ 9800b57cec5SDimitry Andric template <class... _Types> \ 9810b57cec5SDimitry Andric class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \ 9820b57cec5SDimitry Andric copy_constructible_trait> \ 9830b57cec5SDimitry Andric : public __move_constructor<__traits<_Types...>> { \ 9840b57cec5SDimitry Andric using __base_type = __move_constructor<__traits<_Types...>>; \ 9850b57cec5SDimitry Andric \ 9860b57cec5SDimitry Andric public: \ 9870b57cec5SDimitry Andric using __base_type::__base_type; \ 9880b57cec5SDimitry Andric using __base_type::operator=; \ 9890b57cec5SDimitry Andric \ 9900b57cec5SDimitry Andric copy_constructor \ 9910b57cec5SDimitry Andric __copy_constructor(__copy_constructor&&) = default; \ 9920b57cec5SDimitry Andric ~__copy_constructor() = default; \ 9930b57cec5SDimitry Andric __copy_constructor& operator=(const __copy_constructor&) = default; \ 9940b57cec5SDimitry Andric __copy_constructor& operator=(__copy_constructor&&) = default; \ 9950b57cec5SDimitry Andric } 9960b57cec5SDimitry Andric 9970b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 9980b57cec5SDimitry Andric _Trait::_TriviallyAvailable, 9990b57cec5SDimitry Andric __copy_constructor(const __copy_constructor& __that) = default;); 10000b57cec5SDimitry Andric 10010b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 10020b57cec5SDimitry Andric _Trait::_Available, 10030b57cec5SDimitry Andric __copy_constructor(const __copy_constructor& __that) 10040b57cec5SDimitry Andric : __copy_constructor(__valueless_t{}) { 10050b57cec5SDimitry Andric this->__generic_construct(*this, __that); 10060b57cec5SDimitry Andric }); 10070b57cec5SDimitry Andric 10080b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 10090b57cec5SDimitry Andric _Trait::_Unavailable, 10100b57cec5SDimitry Andric __copy_constructor(const __copy_constructor&) = delete;); 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andrictemplate <class _Traits> 10150b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> { 10160b57cec5SDimitry Andric using __base_type = __copy_constructor<_Traits>; 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andricpublic: 10190b57cec5SDimitry Andric using __base_type::__base_type; 10200b57cec5SDimitry Andric using __base_type::operator=; 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric template <size_t _Ip, class... _Args> 1023bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 10240b57cec5SDimitry Andric auto& __emplace(_Args&&... __args) { 10250b57cec5SDimitry Andric this->__destroy(); 10260b57cec5SDimitry Andric auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this), 1027*5f757f3fSDimitry Andric std::forward<_Args>(__args)...); 10280b57cec5SDimitry Andric this->__index = _Ip; 10290b57cec5SDimitry Andric return __res; 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andricprotected: 10330b57cec5SDimitry Andric template <size_t _Ip, class _Tp, class _Arg> 1034bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 10350b57cec5SDimitry Andric void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) { 10360b57cec5SDimitry Andric if (this->index() == _Ip) { 1037*5f757f3fSDimitry Andric __a.__value = std::forward<_Arg>(__arg); 10380b57cec5SDimitry Andric } else { 10390b57cec5SDimitry Andric struct { 104006c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const { 1041*5f757f3fSDimitry Andric __this->__emplace<_Ip>(std::forward<_Arg>(__arg)); 10420b57cec5SDimitry Andric } 104306c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const { 1044*5f757f3fSDimitry Andric __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg))); 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric __assignment* __this; 10470b57cec5SDimitry Andric _Arg&& __arg; 1048*5f757f3fSDimitry Andric } __impl{this, std::forward<_Arg>(__arg)}; 10490b57cec5SDimitry Andric __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> || 10500b57cec5SDimitry Andric !is_nothrow_move_constructible_v<_Tp>>{}); 10510b57cec5SDimitry Andric } 10520b57cec5SDimitry Andric } 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric template <class _That> 1055bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 10560b57cec5SDimitry Andric void __generic_assign(_That&& __that) { 10570b57cec5SDimitry Andric if (this->valueless_by_exception() && __that.valueless_by_exception()) { 10580b57cec5SDimitry Andric // do nothing. 10590b57cec5SDimitry Andric } else if (__that.valueless_by_exception()) { 10600b57cec5SDimitry Andric this->__destroy(); 10610b57cec5SDimitry Andric } else { 10620b57cec5SDimitry Andric __visitation::__base::__visit_alt_at( 10630b57cec5SDimitry Andric __that.index(), 10640b57cec5SDimitry Andric [this](auto& __this_alt, auto&& __that_alt) { 10650b57cec5SDimitry Andric this->__assign_alt( 10660b57cec5SDimitry Andric __this_alt, 1067*5f757f3fSDimitry Andric std::forward<decltype(__that_alt)>(__that_alt).__value); 10680b57cec5SDimitry Andric }, 1069*5f757f3fSDimitry Andric *this, std::forward<_That>(__that)); 10700b57cec5SDimitry Andric } 10710b57cec5SDimitry Andric } 10720b57cec5SDimitry Andric}; 10730b57cec5SDimitry Andric 10740b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_assignable_trait> 10750b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_assignment; 10760b57cec5SDimitry Andric 10770b57cec5SDimitry Andric#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \ 10780b57cec5SDimitry Andric move_assignment) \ 10790b57cec5SDimitry Andric template <class... _Types> \ 10800b57cec5SDimitry Andric class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \ 10810b57cec5SDimitry Andric move_assignable_trait> \ 10820b57cec5SDimitry Andric : public __assignment<__traits<_Types...>> { \ 10830b57cec5SDimitry Andric using __base_type = __assignment<__traits<_Types...>>; \ 10840b57cec5SDimitry Andric \ 10850b57cec5SDimitry Andric public: \ 10860b57cec5SDimitry Andric using __base_type::__base_type; \ 10870b57cec5SDimitry Andric using __base_type::operator=; \ 10880b57cec5SDimitry Andric \ 10890b57cec5SDimitry Andric __move_assignment(const __move_assignment&) = default; \ 10900b57cec5SDimitry Andric __move_assignment(__move_assignment&&) = default; \ 10910b57cec5SDimitry Andric ~__move_assignment() = default; \ 10920b57cec5SDimitry Andric __move_assignment& operator=(const __move_assignment&) = default; \ 10930b57cec5SDimitry Andric move_assignment \ 10940b57cec5SDimitry Andric } 10950b57cec5SDimitry Andric 10960b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 10970b57cec5SDimitry Andric _Trait::_TriviallyAvailable, 10980b57cec5SDimitry Andric __move_assignment& operator=(__move_assignment&& __that) = default;); 10990b57cec5SDimitry Andric 11000b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 11010b57cec5SDimitry Andric _Trait::_Available, 11020b57cec5SDimitry Andric __move_assignment& operator=(__move_assignment&& __that) noexcept( 11030b57cec5SDimitry Andric __all<(is_nothrow_move_constructible_v<_Types> && 11040b57cec5SDimitry Andric is_nothrow_move_assignable_v<_Types>)...>::value) { 1105*5f757f3fSDimitry Andric this->__generic_assign(std::move(__that)); 11060b57cec5SDimitry Andric return *this; 11070b57cec5SDimitry Andric }); 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 11100b57cec5SDimitry Andric _Trait::_Unavailable, 11110b57cec5SDimitry Andric __move_assignment& operator=(__move_assignment&&) = delete;); 11120b57cec5SDimitry Andric 11130b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT 11140b57cec5SDimitry Andric 11150b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_assignable_trait> 11160b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_assignment; 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \ 11190b57cec5SDimitry Andric copy_assignment) \ 11200b57cec5SDimitry Andric template <class... _Types> \ 11210b57cec5SDimitry Andric class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \ 11220b57cec5SDimitry Andric copy_assignable_trait> \ 11230b57cec5SDimitry Andric : public __move_assignment<__traits<_Types...>> { \ 11240b57cec5SDimitry Andric using __base_type = __move_assignment<__traits<_Types...>>; \ 11250b57cec5SDimitry Andric \ 11260b57cec5SDimitry Andric public: \ 11270b57cec5SDimitry Andric using __base_type::__base_type; \ 11280b57cec5SDimitry Andric using __base_type::operator=; \ 11290b57cec5SDimitry Andric \ 11300b57cec5SDimitry Andric __copy_assignment(const __copy_assignment&) = default; \ 11310b57cec5SDimitry Andric __copy_assignment(__copy_assignment&&) = default; \ 11320b57cec5SDimitry Andric ~__copy_assignment() = default; \ 11330b57cec5SDimitry Andric copy_assignment \ 11340b57cec5SDimitry Andric __copy_assignment& operator=(__copy_assignment&&) = default; \ 11350b57cec5SDimitry Andric } 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT( 11380b57cec5SDimitry Andric _Trait::_TriviallyAvailable, 11390b57cec5SDimitry Andric __copy_assignment& operator=(const __copy_assignment& __that) = default;); 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT( 11420b57cec5SDimitry Andric _Trait::_Available, 11430b57cec5SDimitry Andric __copy_assignment& operator=(const __copy_assignment& __that) { 11440b57cec5SDimitry Andric this->__generic_assign(__that); 11450b57cec5SDimitry Andric return *this; 11460b57cec5SDimitry Andric }); 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT( 11490b57cec5SDimitry Andric _Trait::_Unavailable, 11500b57cec5SDimitry Andric __copy_assignment& operator=(const __copy_assignment&) = delete;); 11510b57cec5SDimitry Andric 11520b57cec5SDimitry Andric#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andrictemplate <class... _Types> 11550b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __impl 11560b57cec5SDimitry Andric : public __copy_assignment<__traits<_Types...>> { 11570b57cec5SDimitry Andric using __base_type = __copy_assignment<__traits<_Types...>>; 11580b57cec5SDimitry Andric 11590b57cec5SDimitry Andricpublic: 116081ad6265SDimitry Andric using __base_type::__base_type; // get in_place_index_t constructor & friends 116106c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default; 116206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default; 116306c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default; 116406c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default; 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric template <size_t _Ip, class _Arg> 1167bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 11680b57cec5SDimitry Andric void __assign(_Arg&& __arg) { 11690b57cec5SDimitry Andric this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), 1170*5f757f3fSDimitry Andric std::forward<_Arg>(__arg)); 11710b57cec5SDimitry Andric } 11720b57cec5SDimitry Andric 1173bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI 11740b57cec5SDimitry Andric void __swap(__impl& __that) { 11750b57cec5SDimitry Andric if (this->valueless_by_exception() && __that.valueless_by_exception()) { 11760b57cec5SDimitry Andric // do nothing. 11770b57cec5SDimitry Andric } else if (this->index() == __that.index()) { 11780b57cec5SDimitry Andric __visitation::__base::__visit_alt_at( 11790b57cec5SDimitry Andric this->index(), 11800b57cec5SDimitry Andric [](auto& __this_alt, auto& __that_alt) { 1181*5f757f3fSDimitry Andric using std::swap; 11820b57cec5SDimitry Andric swap(__this_alt.__value, __that_alt.__value); 11830b57cec5SDimitry Andric }, 11840b57cec5SDimitry Andric *this, 11850b57cec5SDimitry Andric __that); 11860b57cec5SDimitry Andric } else { 11870b57cec5SDimitry Andric __impl* __lhs = this; 1188*5f757f3fSDimitry Andric __impl* __rhs = std::addressof(__that); 11890b57cec5SDimitry Andric if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) { 1190*5f757f3fSDimitry Andric std::swap(__lhs, __rhs); 11910b57cec5SDimitry Andric } 1192*5f757f3fSDimitry Andric __impl __tmp(std::move(*__rhs)); 119306c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 11945ffd83dbSDimitry Andric if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) { 1195*5f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(*__lhs)); 11965ffd83dbSDimitry Andric } else { 11970b57cec5SDimitry Andric // EXTENSION: When the move construction of `__lhs` into `__rhs` throws 11980b57cec5SDimitry Andric // and `__tmp` is nothrow move constructible then we move `__tmp` back 11990b57cec5SDimitry Andric // into `__rhs` and provide the strong exception safety guarantee. 12000b57cec5SDimitry Andric try { 1201*5f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(*__lhs)); 12020b57cec5SDimitry Andric } catch (...) { 12030b57cec5SDimitry Andric if (__tmp.__move_nothrow()) { 1204*5f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(__tmp)); 12050b57cec5SDimitry Andric } 12060b57cec5SDimitry Andric throw; 12070b57cec5SDimitry Andric } 12085ffd83dbSDimitry Andric } 12090b57cec5SDimitry Andric#else 12105ffd83dbSDimitry Andric // this isn't consolidated with the `if constexpr` branch above due to 12115ffd83dbSDimitry Andric // `throw` being ill-formed with exceptions disabled even when discarded. 1212*5f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(*__lhs)); 12130b57cec5SDimitry Andric#endif 1214*5f757f3fSDimitry Andric this->__generic_construct(*__lhs, std::move(__tmp)); 12150b57cec5SDimitry Andric } 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andricprivate: 1219bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI 12200b57cec5SDimitry Andric bool __move_nothrow() const { 12210b57cec5SDimitry Andric constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...}; 12220b57cec5SDimitry Andric return this->valueless_by_exception() || __results[this->index()]; 12230b57cec5SDimitry Andric } 12240b57cec5SDimitry Andric}; 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andricstruct __no_narrowing_check { 12270b57cec5SDimitry Andric template <class _Dest, class _Source> 122881ad6265SDimitry Andric using _Apply = __type_identity<_Dest>; 12290b57cec5SDimitry Andric}; 12300b57cec5SDimitry Andric 12310b57cec5SDimitry Andricstruct __narrowing_check { 12320b57cec5SDimitry Andric template <class _Dest> 123381ad6265SDimitry Andric static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>; 12340b57cec5SDimitry Andric template <class _Dest, class _Source> 1235bdd1243dSDimitry Andric using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()})); 12360b57cec5SDimitry Andric}; 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andrictemplate <class _Dest, class _Source> 1239349cc55cSDimitry Andricusing __check_for_narrowing _LIBCPP_NODEBUG = 12400b57cec5SDimitry Andric typename _If< 12410b57cec5SDimitry Andric#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT 12420b57cec5SDimitry Andric false && 12430b57cec5SDimitry Andric#endif 12440b57cec5SDimitry Andric is_arithmetic<_Dest>::value, 12450b57cec5SDimitry Andric __narrowing_check, 12460b57cec5SDimitry Andric __no_narrowing_check 12470b57cec5SDimitry Andric >::template _Apply<_Dest, _Source>; 12480b57cec5SDimitry Andric 12490b57cec5SDimitry Andrictemplate <class _Tp, size_t _Idx> 12500b57cec5SDimitry Andricstruct __overload { 12510b57cec5SDimitry Andric template <class _Up> 12520b57cec5SDimitry Andric auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>; 12530b57cec5SDimitry Andric}; 12540b57cec5SDimitry Andric 1255*5f757f3fSDimitry Andric// TODO(LLVM-19): Remove all occurrences of this macro. 1256*5f757f3fSDimitry Andric#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT 12570b57cec5SDimitry Andrictemplate <class _Tp, size_t> 12580b57cec5SDimitry Andricstruct __overload_bool { 1259bdd1243dSDimitry Andric template <class _Up, class _Ap = __remove_cvref_t<_Up>> 12600b57cec5SDimitry Andric auto operator()(bool, _Up&&) const 126181ad6265SDimitry Andric -> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>; 12620b57cec5SDimitry Andric}; 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andrictemplate <size_t _Idx> 12650b57cec5SDimitry Andricstruct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {}; 12660b57cec5SDimitry Andrictemplate <size_t _Idx> 12670b57cec5SDimitry Andricstruct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {}; 12680b57cec5SDimitry Andrictemplate <size_t _Idx> 12690b57cec5SDimitry Andricstruct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {}; 12700b57cec5SDimitry Andrictemplate <size_t _Idx> 12710b57cec5SDimitry Andricstruct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {}; 1272*5f757f3fSDimitry Andric#endif 12730b57cec5SDimitry Andric 12740b57cec5SDimitry Andrictemplate <class ..._Bases> 12750b57cec5SDimitry Andricstruct __all_overloads : _Bases... { 12760b57cec5SDimitry Andric void operator()() const; 12770b57cec5SDimitry Andric using _Bases::operator()...; 12780b57cec5SDimitry Andric}; 12790b57cec5SDimitry Andric 128006c3fb27SDimitry Andrictemplate <class _IdxSeq> 12810b57cec5SDimitry Andricstruct __make_overloads_imp; 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andrictemplate <size_t ..._Idx> 12840b57cec5SDimitry Andricstruct __make_overloads_imp<__tuple_indices<_Idx...> > { 12850b57cec5SDimitry Andric template <class ..._Types> 1286349cc55cSDimitry Andric using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>; 12870b57cec5SDimitry Andric}; 12880b57cec5SDimitry Andric 12890b57cec5SDimitry Andrictemplate <class ..._Types> 1290349cc55cSDimitry Andricusing _MakeOverloads _LIBCPP_NODEBUG = typename __make_overloads_imp< 12910b57cec5SDimitry Andric __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>; 12920b57cec5SDimitry Andric 12930b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 12940b57cec5SDimitry Andricusing __best_match_t = 12950b57cec5SDimitry Andric typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type; 12960b57cec5SDimitry Andric 12971fd87a68SDimitry Andric} // namespace __variant_detail 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andrictemplate <class... _Types> 130006c3fb27SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant 13010b57cec5SDimitry Andric : private __sfinae_ctor_base< 13020b57cec5SDimitry Andric __all<is_copy_constructible_v<_Types>...>::value, 13030b57cec5SDimitry Andric __all<is_move_constructible_v<_Types>...>::value>, 13040b57cec5SDimitry Andric private __sfinae_assign_base< 13050b57cec5SDimitry Andric __all<(is_copy_constructible_v<_Types> && 13060b57cec5SDimitry Andric is_copy_assignable_v<_Types>)...>::value, 13070b57cec5SDimitry Andric __all<(is_move_constructible_v<_Types> && 13080b57cec5SDimitry Andric is_move_assignable_v<_Types>)...>::value> { 13090b57cec5SDimitry Andric static_assert(0 < sizeof...(_Types), 13100b57cec5SDimitry Andric "variant must consist of at least one alternative."); 13110b57cec5SDimitry Andric 13120b57cec5SDimitry Andric static_assert(__all<!is_array_v<_Types>...>::value, 13130b57cec5SDimitry Andric "variant can not have an array type as an alternative."); 13140b57cec5SDimitry Andric 13150b57cec5SDimitry Andric static_assert(__all<!is_reference_v<_Types>...>::value, 13160b57cec5SDimitry Andric "variant can not have a reference type as an alternative."); 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric static_assert(__all<!is_void_v<_Types>...>::value, 13190b57cec5SDimitry Andric "variant can not have a void type as an alternative."); 13200b57cec5SDimitry Andric 13210b57cec5SDimitry Andric using __first_type = variant_alternative_t<0, variant>; 13220b57cec5SDimitry Andric 13230b57cec5SDimitry Andricpublic: 13240b57cec5SDimitry Andric template <bool _Dummy = true, 13250b57cec5SDimitry Andric enable_if_t<__dependent_type<is_default_constructible<__first_type>, 13260b57cec5SDimitry Andric _Dummy>::value, 13270b57cec5SDimitry Andric int> = 0> 1328bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 13290b57cec5SDimitry Andric constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>) 1330bdd1243dSDimitry Andric : __impl_(in_place_index<0>) {} 13310b57cec5SDimitry Andric 133206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default; 133306c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default; 13340b57cec5SDimitry Andric 13350b57cec5SDimitry Andric template < 13360b57cec5SDimitry Andric class _Arg, 1337bdd1243dSDimitry Andric enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0, 1338bdd1243dSDimitry Andric enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0, 1339bdd1243dSDimitry Andric enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0, 13400b57cec5SDimitry Andric class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, 13410b57cec5SDimitry Andric size_t _Ip = 13420b57cec5SDimitry Andric __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 13430b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0> 1344bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 13450b57cec5SDimitry Andric constexpr variant(_Arg&& __arg) noexcept( 13460b57cec5SDimitry Andric is_nothrow_constructible_v<_Tp, _Arg>) 1347*5f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {} 13480b57cec5SDimitry Andric 13490b57cec5SDimitry Andric template <size_t _Ip, class... _Args, 13500b57cec5SDimitry Andric class = enable_if_t<(_Ip < sizeof...(_Types)), int>, 13510b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 13520b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1353bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 13540b57cec5SDimitry Andric explicit constexpr variant( 13550b57cec5SDimitry Andric in_place_index_t<_Ip>, 13560b57cec5SDimitry Andric _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>) 1357*5f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {} 13580b57cec5SDimitry Andric 13590b57cec5SDimitry Andric template < 13600b57cec5SDimitry Andric size_t _Ip, 13610b57cec5SDimitry Andric class _Up, 13620b57cec5SDimitry Andric class... _Args, 13630b57cec5SDimitry Andric enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 13640b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 13650b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 13660b57cec5SDimitry Andric int> = 0> 1367bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 13680b57cec5SDimitry Andric explicit constexpr variant( 13690b57cec5SDimitry Andric in_place_index_t<_Ip>, 13700b57cec5SDimitry Andric initializer_list<_Up> __il, 13710b57cec5SDimitry Andric _Args&&... __args) noexcept( 13720b57cec5SDimitry Andric is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) 1373*5f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {} 13740b57cec5SDimitry Andric 13750b57cec5SDimitry Andric template < 13760b57cec5SDimitry Andric class _Tp, 13770b57cec5SDimitry Andric class... _Args, 13780b57cec5SDimitry Andric size_t _Ip = 13790b57cec5SDimitry Andric __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 13800b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1381bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 13820b57cec5SDimitry Andric explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept( 13830b57cec5SDimitry Andric is_nothrow_constructible_v<_Tp, _Args...>) 1384*5f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {} 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric template < 13870b57cec5SDimitry Andric class _Tp, 13880b57cec5SDimitry Andric class _Up, 13890b57cec5SDimitry Andric class... _Args, 13900b57cec5SDimitry Andric size_t _Ip = 13910b57cec5SDimitry Andric __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 13920b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 13930b57cec5SDimitry Andric int> = 0> 1394bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 13950b57cec5SDimitry Andric explicit constexpr variant( 13960b57cec5SDimitry Andric in_place_type_t<_Tp>, 13970b57cec5SDimitry Andric initializer_list<_Up> __il, 13980b57cec5SDimitry Andric _Args&&... __args) noexcept( 13990b57cec5SDimitry Andric is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>) 1400*5f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {} 14010b57cec5SDimitry Andric 140206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~variant() = default; 14030b57cec5SDimitry Andric 140406c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default; 140506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default; 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric template < 14080b57cec5SDimitry Andric class _Arg, 1409bdd1243dSDimitry Andric enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0, 14100b57cec5SDimitry Andric class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, 14110b57cec5SDimitry Andric size_t _Ip = 14120b57cec5SDimitry Andric __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 14130b57cec5SDimitry Andric enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, 14140b57cec5SDimitry Andric int> = 0> 1415bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 14160b57cec5SDimitry Andric variant& operator=(_Arg&& __arg) noexcept( 14170b57cec5SDimitry Andric is_nothrow_assignable_v<_Tp&, _Arg> && 14180b57cec5SDimitry Andric is_nothrow_constructible_v<_Tp, _Arg>) { 1419*5f757f3fSDimitry Andric __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg)); 14200b57cec5SDimitry Andric return *this; 14210b57cec5SDimitry Andric } 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric template < 14240b57cec5SDimitry Andric size_t _Ip, 14250b57cec5SDimitry Andric class... _Args, 14260b57cec5SDimitry Andric enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 14270b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 14280b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1429bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 14300b57cec5SDimitry Andric _Tp& emplace(_Args&&... __args) { 1431*5f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...); 14320b57cec5SDimitry Andric } 14330b57cec5SDimitry Andric 14340b57cec5SDimitry Andric template < 14350b57cec5SDimitry Andric size_t _Ip, 14360b57cec5SDimitry Andric class _Up, 14370b57cec5SDimitry Andric class... _Args, 14380b57cec5SDimitry Andric enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 14390b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 14400b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 14410b57cec5SDimitry Andric int> = 0> 1442bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 14430b57cec5SDimitry Andric _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { 1444*5f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...); 14450b57cec5SDimitry Andric } 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric template < 14480b57cec5SDimitry Andric class _Tp, 14490b57cec5SDimitry Andric class... _Args, 14500b57cec5SDimitry Andric size_t _Ip = 14510b57cec5SDimitry Andric __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 14520b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1453bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 14540b57cec5SDimitry Andric _Tp& emplace(_Args&&... __args) { 1455*5f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...); 14560b57cec5SDimitry Andric } 14570b57cec5SDimitry Andric 14580b57cec5SDimitry Andric template < 14590b57cec5SDimitry Andric class _Tp, 14600b57cec5SDimitry Andric class _Up, 14610b57cec5SDimitry Andric class... _Args, 14620b57cec5SDimitry Andric size_t _Ip = 14630b57cec5SDimitry Andric __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 14640b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 14650b57cec5SDimitry Andric int> = 0> 1466bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 14670b57cec5SDimitry Andric _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { 1468*5f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...); 14690b57cec5SDimitry Andric } 14700b57cec5SDimitry Andric 1471bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 14720b57cec5SDimitry Andric constexpr bool valueless_by_exception() const noexcept { 1473bdd1243dSDimitry Andric return __impl_.valueless_by_exception(); 14740b57cec5SDimitry Andric } 14750b57cec5SDimitry Andric 1476bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 1477bdd1243dSDimitry Andric constexpr size_t index() const noexcept { return __impl_.index(); } 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric template < 14800b57cec5SDimitry Andric bool _Dummy = true, 14810b57cec5SDimitry Andric enable_if_t< 14820b57cec5SDimitry Andric __all<( 14830b57cec5SDimitry Andric __dependent_type<is_move_constructible<_Types>, _Dummy>::value && 14840b57cec5SDimitry Andric __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value, 14850b57cec5SDimitry Andric int> = 0> 1486bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 14870b57cec5SDimitry Andric void swap(variant& __that) noexcept( 14880b57cec5SDimitry Andric __all<(is_nothrow_move_constructible_v<_Types> && 14890b57cec5SDimitry Andric is_nothrow_swappable_v<_Types>)...>::value) { 1490bdd1243dSDimitry Andric __impl_.__swap(__that.__impl_); 14910b57cec5SDimitry Andric } 14920b57cec5SDimitry Andric 14930b57cec5SDimitry Andricprivate: 1494bdd1243dSDimitry Andric __variant_detail::__impl<_Types...> __impl_; 14950b57cec5SDimitry Andric 14960b57cec5SDimitry Andric friend struct __variant_detail::__access::__variant; 14970b57cec5SDimitry Andric friend struct __variant_detail::__visitation::__variant; 14980b57cec5SDimitry Andric}; 14990b57cec5SDimitry Andric 15000b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1501bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15020b57cec5SDimitry Andricconstexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept { 15030b57cec5SDimitry Andric return __v.index() == _Ip; 15040b57cec5SDimitry Andric} 15050b57cec5SDimitry Andric 15060b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1507bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15080b57cec5SDimitry Andricconstexpr bool holds_alternative(const variant<_Types...>& __v) noexcept { 1509bdd1243dSDimitry Andric return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 15100b57cec5SDimitry Andric} 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp> 1513bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15140b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15150b57cec5SDimitry Andricconstexpr auto&& __generic_get(_Vp&& __v) { 15160b57cec5SDimitry Andric using __variant_detail::__access::__variant; 1517bdd1243dSDimitry Andric if (!std::__holds_alternative<_Ip>(__v)) { 15180b57cec5SDimitry Andric __throw_bad_variant_access(); 15190b57cec5SDimitry Andric } 1520*5f757f3fSDimitry Andric return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value; 15210b57cec5SDimitry Andric} 15220b57cec5SDimitry Andric 15230b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1524bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15250b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15260b57cec5SDimitry Andricconstexpr variant_alternative_t<_Ip, variant<_Types...>>& get( 15270b57cec5SDimitry Andric variant<_Types...>& __v) { 15280b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 15290b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1530bdd1243dSDimitry Andric return std::__generic_get<_Ip>(__v); 15310b57cec5SDimitry Andric} 15320b57cec5SDimitry Andric 15330b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1534bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15350b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15360b57cec5SDimitry Andricconstexpr variant_alternative_t<_Ip, variant<_Types...>>&& get( 15370b57cec5SDimitry Andric variant<_Types...>&& __v) { 15380b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 15390b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1540*5f757f3fSDimitry Andric return std::__generic_get<_Ip>(std::move(__v)); 15410b57cec5SDimitry Andric} 15420b57cec5SDimitry Andric 15430b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1544bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15450b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15460b57cec5SDimitry Andricconstexpr const variant_alternative_t<_Ip, variant<_Types...>>& get( 15470b57cec5SDimitry Andric const variant<_Types...>& __v) { 15480b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 15490b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1550bdd1243dSDimitry Andric return std::__generic_get<_Ip>(__v); 15510b57cec5SDimitry Andric} 15520b57cec5SDimitry Andric 15530b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1554bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15550b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15560b57cec5SDimitry Andricconstexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get( 15570b57cec5SDimitry Andric const variant<_Types...>&& __v) { 15580b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 15590b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1560*5f757f3fSDimitry Andric return std::__generic_get<_Ip>(std::move(__v)); 15610b57cec5SDimitry Andric} 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1564bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15650b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15660b57cec5SDimitry Andricconstexpr _Tp& get(variant<_Types...>& __v) { 15670b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1568*5f757f3fSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 15690b57cec5SDimitry Andric} 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1572bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15730b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15740b57cec5SDimitry Andricconstexpr _Tp&& get(variant<_Types...>&& __v) { 15750b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1576*5f757f3fSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>( 1577*5f757f3fSDimitry Andric std::move(__v)); 15780b57cec5SDimitry Andric} 15790b57cec5SDimitry Andric 15800b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1581bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15820b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15830b57cec5SDimitry Andricconstexpr const _Tp& get(const variant<_Types...>& __v) { 15840b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1585*5f757f3fSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 15860b57cec5SDimitry Andric} 15870b57cec5SDimitry Andric 15880b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1589bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15900b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 15910b57cec5SDimitry Andricconstexpr const _Tp&& get(const variant<_Types...>&& __v) { 15920b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1593*5f757f3fSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>( 1594*5f757f3fSDimitry Andric std::move(__v)); 15950b57cec5SDimitry Andric} 15960b57cec5SDimitry Andric 15970b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp> 1598bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 15990b57cec5SDimitry Andricconstexpr auto* __generic_get_if(_Vp* __v) noexcept { 16000b57cec5SDimitry Andric using __variant_detail::__access::__variant; 1601bdd1243dSDimitry Andric return __v && std::__holds_alternative<_Ip>(*__v) 1602*5f757f3fSDimitry Andric ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) 16030b57cec5SDimitry Andric : nullptr; 16040b57cec5SDimitry Andric} 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1607bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 16080b57cec5SDimitry Andricconstexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>> 16090b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept { 16100b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 16110b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1612bdd1243dSDimitry Andric return std::__generic_get_if<_Ip>(__v); 16130b57cec5SDimitry Andric} 16140b57cec5SDimitry Andric 16150b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1616bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 16170b57cec5SDimitry Andricconstexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>> 16180b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept { 16190b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 16200b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1621bdd1243dSDimitry Andric return std::__generic_get_if<_Ip>(__v); 16220b57cec5SDimitry Andric} 16230b57cec5SDimitry Andric 16240b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1625bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 16260b57cec5SDimitry Andricconstexpr add_pointer_t<_Tp> 16270b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept { 16280b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1629*5f757f3fSDimitry Andric return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 16300b57cec5SDimitry Andric} 16310b57cec5SDimitry Andric 16320b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1633bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 16340b57cec5SDimitry Andricconstexpr add_pointer_t<const _Tp> 16350b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept { 16360b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1637*5f757f3fSDimitry Andric return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 16380b57cec5SDimitry Andric} 16390b57cec5SDimitry Andric 16400b57cec5SDimitry Andrictemplate <class _Operator> 16410b57cec5SDimitry Andricstruct __convert_to_bool { 16420b57cec5SDimitry Andric template <class _T1, class _T2> 1643bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 1644bdd1243dSDimitry Andric constexpr bool operator()(_T1 && __t1, _T2&& __t2) const { 1645*5f757f3fSDimitry Andric static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value, 16460b57cec5SDimitry Andric "the relational operator does not return a type which is implicitly convertible to bool"); 1647*5f757f3fSDimitry Andric return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2)); 16480b57cec5SDimitry Andric } 16490b57cec5SDimitry Andric}; 16500b57cec5SDimitry Andric 16510b57cec5SDimitry Andrictemplate <class... _Types> 1652bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 16530b57cec5SDimitry Andricconstexpr bool operator==(const variant<_Types...>& __lhs, 16540b57cec5SDimitry Andric const variant<_Types...>& __rhs) { 16550b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 16560b57cec5SDimitry Andric if (__lhs.index() != __rhs.index()) return false; 16570b57cec5SDimitry Andric if (__lhs.valueless_by_exception()) return true; 16580b57cec5SDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs); 16590b57cec5SDimitry Andric} 16600b57cec5SDimitry Andric 166106c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 20 1662bdd1243dSDimitry Andric 1663bdd1243dSDimitry Andrictemplate <class... _Types> requires (three_way_comparable<_Types> && ...) 1664bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...> 1665bdd1243dSDimitry Andricoperator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 1666bdd1243dSDimitry Andric using __variant_detail::__visitation::__variant; 1667bdd1243dSDimitry Andric using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>; 1668bdd1243dSDimitry Andric if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception()) 1669bdd1243dSDimitry Andric return strong_ordering::equal; 1670bdd1243dSDimitry Andric if (__lhs.valueless_by_exception()) 1671bdd1243dSDimitry Andric return strong_ordering::less; 1672bdd1243dSDimitry Andric if (__rhs.valueless_by_exception()) 1673bdd1243dSDimitry Andric return strong_ordering::greater; 1674bdd1243dSDimitry Andric if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0) 1675bdd1243dSDimitry Andric return __c; 1676bdd1243dSDimitry Andric auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; }; 1677bdd1243dSDimitry Andric return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs); 1678bdd1243dSDimitry Andric} 1679bdd1243dSDimitry Andric 168006c3fb27SDimitry Andric# endif // _LIBCPP_STD_VER >= 20 1681bdd1243dSDimitry Andric 16820b57cec5SDimitry Andrictemplate <class... _Types> 1683bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 16840b57cec5SDimitry Andricconstexpr bool operator!=(const variant<_Types...>& __lhs, 16850b57cec5SDimitry Andric const variant<_Types...>& __rhs) { 16860b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 16870b57cec5SDimitry Andric if (__lhs.index() != __rhs.index()) return true; 16880b57cec5SDimitry Andric if (__lhs.valueless_by_exception()) return false; 16890b57cec5SDimitry Andric return __variant::__visit_value_at( 16900b57cec5SDimitry Andric __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs); 16910b57cec5SDimitry Andric} 16920b57cec5SDimitry Andric 16930b57cec5SDimitry Andrictemplate <class... _Types> 1694bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 16950b57cec5SDimitry Andricconstexpr bool operator<(const variant<_Types...>& __lhs, 16960b57cec5SDimitry Andric const variant<_Types...>& __rhs) { 16970b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 16980b57cec5SDimitry Andric if (__rhs.valueless_by_exception()) return false; 16990b57cec5SDimitry Andric if (__lhs.valueless_by_exception()) return true; 17000b57cec5SDimitry Andric if (__lhs.index() < __rhs.index()) return true; 17010b57cec5SDimitry Andric if (__lhs.index() > __rhs.index()) return false; 17020b57cec5SDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs); 17030b57cec5SDimitry Andric} 17040b57cec5SDimitry Andric 17050b57cec5SDimitry Andrictemplate <class... _Types> 1706bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 17070b57cec5SDimitry Andricconstexpr bool operator>(const variant<_Types...>& __lhs, 17080b57cec5SDimitry Andric const variant<_Types...>& __rhs) { 17090b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 17100b57cec5SDimitry Andric if (__lhs.valueless_by_exception()) return false; 17110b57cec5SDimitry Andric if (__rhs.valueless_by_exception()) return true; 17120b57cec5SDimitry Andric if (__lhs.index() > __rhs.index()) return true; 17130b57cec5SDimitry Andric if (__lhs.index() < __rhs.index()) return false; 17140b57cec5SDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs); 17150b57cec5SDimitry Andric} 17160b57cec5SDimitry Andric 17170b57cec5SDimitry Andrictemplate <class... _Types> 1718bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 17190b57cec5SDimitry Andricconstexpr bool operator<=(const variant<_Types...>& __lhs, 17200b57cec5SDimitry Andric const variant<_Types...>& __rhs) { 17210b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 17220b57cec5SDimitry Andric if (__lhs.valueless_by_exception()) return true; 17230b57cec5SDimitry Andric if (__rhs.valueless_by_exception()) return false; 17240b57cec5SDimitry Andric if (__lhs.index() < __rhs.index()) return true; 17250b57cec5SDimitry Andric if (__lhs.index() > __rhs.index()) return false; 17260b57cec5SDimitry Andric return __variant::__visit_value_at( 17270b57cec5SDimitry Andric __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs); 17280b57cec5SDimitry Andric} 17290b57cec5SDimitry Andric 17300b57cec5SDimitry Andrictemplate <class... _Types> 1731bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 17320b57cec5SDimitry Andricconstexpr bool operator>=(const variant<_Types...>& __lhs, 17330b57cec5SDimitry Andric const variant<_Types...>& __rhs) { 17340b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 17350b57cec5SDimitry Andric if (__rhs.valueless_by_exception()) return true; 17360b57cec5SDimitry Andric if (__lhs.valueless_by_exception()) return false; 17370b57cec5SDimitry Andric if (__lhs.index() > __rhs.index()) return true; 17380b57cec5SDimitry Andric if (__lhs.index() < __rhs.index()) return false; 17390b57cec5SDimitry Andric return __variant::__visit_value_at( 17400b57cec5SDimitry Andric __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs); 17410b57cec5SDimitry Andric} 17420b57cec5SDimitry Andric 1743e8d8bef9SDimitry Andrictemplate <class... _Vs> 1744bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1745bdd1243dSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 1746bdd1243dSDimitry Andricconstexpr void __throw_if_valueless(_Vs&&... __vs) { 1747fe6060f1SDimitry Andric const bool __valueless = 1748*5f757f3fSDimitry Andric (... || std::__as_variant(__vs).valueless_by_exception()); 1749e8d8bef9SDimitry Andric if (__valueless) { 1750e8d8bef9SDimitry Andric __throw_bad_variant_access(); 1751e8d8bef9SDimitry Andric } 1752e8d8bef9SDimitry Andric} 1753e8d8bef9SDimitry Andric 1754fe6060f1SDimitry Andrictemplate < 1755fe6060f1SDimitry Andric class _Visitor, class... _Vs, 1756*5f757f3fSDimitry Andric typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...> > 1757bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1758bdd1243dSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 1759bdd1243dSDimitry Andricconstexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) { 17600b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 1761*5f757f3fSDimitry Andric std::__throw_if_valueless(std::forward<_Vs>(__vs)...); 1762*5f757f3fSDimitry Andric return __variant::__visit_value(std::forward<_Visitor>(__visitor), 1763*5f757f3fSDimitry Andric std::forward<_Vs>(__vs)...); 17640b57cec5SDimitry Andric} 17650b57cec5SDimitry Andric 176606c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20 1767fe6060f1SDimitry Andrictemplate < 1768fe6060f1SDimitry Andric class _Rp, class _Visitor, class... _Vs, 1769*5f757f3fSDimitry Andric typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...> > 1770bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1771bdd1243dSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS 1772bdd1243dSDimitry Andricconstexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs) { 1773e8d8bef9SDimitry Andric using __variant_detail::__visitation::__variant; 1774*5f757f3fSDimitry Andric std::__throw_if_valueless(std::forward<_Vs>(__vs)...); 1775*5f757f3fSDimitry Andric return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), 1776*5f757f3fSDimitry Andric std::forward<_Vs>(__vs)...); 1777e8d8bef9SDimitry Andric} 1778e8d8bef9SDimitry Andric#endif 1779e8d8bef9SDimitry Andric 17800b57cec5SDimitry Andrictemplate <class... _Types> 1781bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1782349cc55cSDimitry Andricauto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) 1783349cc55cSDimitry Andric noexcept(noexcept(__lhs.swap(__rhs))) 1784349cc55cSDimitry Andric -> decltype( __lhs.swap(__rhs)) 1785349cc55cSDimitry Andric { return __lhs.swap(__rhs); } 17860b57cec5SDimitry Andric 17870b57cec5SDimitry Andrictemplate <class... _Types> 17880b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash< 17890b57cec5SDimitry Andric __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> { 17900b57cec5SDimitry Andric using argument_type = variant<_Types...>; 17910b57cec5SDimitry Andric using result_type = size_t; 17920b57cec5SDimitry Andric 1793bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI 17940b57cec5SDimitry Andric result_type operator()(const argument_type& __v) const { 17950b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 17960b57cec5SDimitry Andric size_t __res = 17970b57cec5SDimitry Andric __v.valueless_by_exception() 17980b57cec5SDimitry Andric ? 299792458 // Random value chosen by the universe upon creation 17990b57cec5SDimitry Andric : __variant::__visit_alt( 18000b57cec5SDimitry Andric [](const auto& __alt) { 1801bdd1243dSDimitry Andric using __alt_type = __remove_cvref_t<decltype(__alt)>; 18020b57cec5SDimitry Andric using __value_type = remove_const_t< 18030b57cec5SDimitry Andric typename __alt_type::__value_type>; 18040b57cec5SDimitry Andric return hash<__value_type>{}(__alt.__value); 18050b57cec5SDimitry Andric }, 18060b57cec5SDimitry Andric __v); 1807bdd1243dSDimitry Andric return std::__hash_combine(__res, hash<size_t>{}(__v.index())); 18080b57cec5SDimitry Andric } 18090b57cec5SDimitry Andric}; 18100b57cec5SDimitry Andric 1811fe6060f1SDimitry Andric// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong 1812fe6060f1SDimitry Andric// type whereas std::get will throw or returning nullptr. This makes it faster than 1813fe6060f1SDimitry Andric// std::get. 1814fe6060f1SDimitry Andrictemplate <size_t _Ip, class _Vp> 1815bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1816fe6060f1SDimitry Andricconstexpr auto&& __unchecked_get(_Vp&& __v) noexcept { 1817fe6060f1SDimitry Andric using __variant_detail::__access::__variant; 1818*5f757f3fSDimitry Andric return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value; 18190b57cec5SDimitry Andric} 1820fe6060f1SDimitry Andric 1821fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types> 1822bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1823fe6060f1SDimitry Andricconstexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept { 1824bdd1243dSDimitry Andric return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1825fe6060f1SDimitry Andric} 1826fe6060f1SDimitry Andric 1827fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types> 1828bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1829fe6060f1SDimitry Andricconstexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept { 1830bdd1243dSDimitry Andric return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1831fe6060f1SDimitry Andric} 18320b57cec5SDimitry Andric 183306c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 17 18340b57cec5SDimitry Andric 18350b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 18360b57cec5SDimitry Andric 18370b57cec5SDimitry Andric_LIBCPP_POP_MACROS 18380b57cec5SDimitry Andric 1839bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 184006c3fb27SDimitry Andric# include <exception> 1841bdd1243dSDimitry Andric# include <type_traits> 1842bdd1243dSDimitry Andric# include <typeinfo> 1843bdd1243dSDimitry Andric# include <utility> 1844bdd1243dSDimitry Andric#endif 1845bdd1243dSDimitry Andric 18460b57cec5SDimitry Andric#endif // _LIBCPP_VARIANT 1847