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