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