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 45*0fca6ea1SDimitry Andric constexpr ~variant(); // constexpr since c++20 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 51*0fca6ea1SDimitry Andric template <class T> 52*0fca6ea1SDimitry Andric constexpr variant& operator=(T&&) noexcept(see below); // constexpr since c++20 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric // 20.7.2.4, modifiers 550b57cec5SDimitry Andric template <class T, class... Args> 56*0fca6ea1SDimitry Andric constexpr T& emplace(Args&&...); // constexpr since c++20 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric template <class T, class U, class... Args> 59*0fca6ea1SDimitry Andric constexpr T& emplace(initializer_list<U>, Args&&...); // constexpr since c++20 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric template <size_t I, class... Args> 62*0fca6ea1SDimitry Andric constexpr variant_alternative_t<I, variant>& emplace(Args&&...); // constexpr since c++20 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric template <size_t I, class U, class... Args> 65*0fca6ea1SDimitry Andric constexpr variant_alternative_t<I, variant>& 66*0fca6ea1SDimitry Andric emplace(initializer_list<U>, Args&&...); // constexpr since c++20 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // 20.7.2.5, value status 690b57cec5SDimitry Andric constexpr bool valueless_by_exception() const noexcept; 700b57cec5SDimitry Andric constexpr size_t index() const noexcept; 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric // 20.7.2.6, swap 730b57cec5SDimitry Andric void swap(variant&) noexcept(see below); 747a6dacacSDimitry Andric 757a6dacacSDimitry Andric // [variant.visit], visitation 767a6dacacSDimitry Andric template<class Self, class Visitor> 777a6dacacSDimitry Andric constexpr decltype(auto) visit(this Self&&, Visitor&&); // Since C++26 787a6dacacSDimitry Andric template<class R, class Self, class Visitor> 797a6dacacSDimitry Andric constexpr R visit(this Self&&, Visitor&&); // Since C++26 800b57cec5SDimitry Andric }; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // 20.7.3, variant helper classes 830b57cec5SDimitry Andric template <class T> struct variant_size; // undefined 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric template <class T> 860b57cec5SDimitry Andric inline constexpr size_t variant_size_v = variant_size<T>::value; 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric template <class T> struct variant_size<const T>; 890b57cec5SDimitry Andric template <class T> struct variant_size<volatile T>; 900b57cec5SDimitry Andric template <class T> struct variant_size<const volatile T>; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric template <class... Types> 930b57cec5SDimitry Andric struct variant_size<variant<Types...>>; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative; // undefined 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric template <size_t I, class T> 980b57cec5SDimitry Andric using variant_alternative_t = typename variant_alternative<I, T>::type; 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative<I, const T>; 1010b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative<I, volatile T>; 1020b57cec5SDimitry Andric template <size_t I, class T> struct variant_alternative<I, const volatile T>; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric template <size_t I, class... Types> 1050b57cec5SDimitry Andric struct variant_alternative<I, variant<Types...>>; 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric inline constexpr size_t variant_npos = -1; 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric // 20.7.4, value access 1100b57cec5SDimitry Andric template <class T, class... Types> 1110b57cec5SDimitry Andric constexpr bool holds_alternative(const variant<Types...>&) noexcept; 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric template <size_t I, class... Types> 1140b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>>& 1150b57cec5SDimitry Andric get(variant<Types...>&); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric template <size_t I, class... Types> 1180b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>>&& 1190b57cec5SDimitry Andric get(variant<Types...>&&); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric template <size_t I, class... Types> 1220b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>> const& 1230b57cec5SDimitry Andric get(const variant<Types...>&); 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric template <size_t I, class... Types> 1260b57cec5SDimitry Andric constexpr variant_alternative_t<I, variant<Types...>> const&& 1270b57cec5SDimitry Andric get(const variant<Types...>&&); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric template <class T, class... Types> 1300b57cec5SDimitry Andric constexpr T& get(variant<Types...>&); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric template <class T, class... Types> 1330b57cec5SDimitry Andric constexpr T&& get(variant<Types...>&&); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric template <class T, class... Types> 1360b57cec5SDimitry Andric constexpr const T& get(const variant<Types...>&); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric template <class T, class... Types> 1390b57cec5SDimitry Andric constexpr const T&& get(const variant<Types...>&&); 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric template <size_t I, class... Types> 1420b57cec5SDimitry Andric constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>> 1430b57cec5SDimitry Andric get_if(variant<Types...>*) noexcept; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric template <size_t I, class... Types> 1460b57cec5SDimitry Andric constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>> 1470b57cec5SDimitry Andric get_if(const variant<Types...>*) noexcept; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric template <class T, class... Types> 1500b57cec5SDimitry Andric constexpr add_pointer_t<T> 1510b57cec5SDimitry Andric get_if(variant<Types...>*) noexcept; 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric template <class T, class... Types> 1540b57cec5SDimitry Andric constexpr add_pointer_t<const T> 1550b57cec5SDimitry Andric get_if(const variant<Types...>*) noexcept; 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric // 20.7.5, relational operators 1580b57cec5SDimitry Andric template <class... Types> 1590b57cec5SDimitry Andric constexpr bool operator==(const variant<Types...>&, const variant<Types...>&); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric template <class... Types> 1620b57cec5SDimitry Andric constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric template <class... Types> 1650b57cec5SDimitry Andric constexpr bool operator<(const variant<Types...>&, const variant<Types...>&); 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric template <class... Types> 1680b57cec5SDimitry Andric constexpr bool operator>(const variant<Types...>&, const variant<Types...>&); 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric template <class... Types> 1710b57cec5SDimitry Andric constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&); 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric template <class... Types> 1740b57cec5SDimitry Andric constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&); 1750b57cec5SDimitry Andric 176bdd1243dSDimitry Andric template <class... Types> requires (three_way_comparable<Types> && ...) 177bdd1243dSDimitry Andric constexpr common_comparison_category_t<compare_three_way_result_t<Types>...> 178bdd1243dSDimitry Andric operator<=>(const variant<Types...>&, const variant<Types...>&); // since C++20 179bdd1243dSDimitry Andric 1800b57cec5SDimitry Andric // 20.7.6, visitation 1810b57cec5SDimitry Andric template <class Visitor, class... Variants> 1820b57cec5SDimitry Andric constexpr see below visit(Visitor&&, Variants&&...); 1830b57cec5SDimitry Andric 184e8d8bef9SDimitry Andric template <class R, class Visitor, class... Variants> 185e8d8bef9SDimitry Andric constexpr R visit(Visitor&&, Variants&&...); // since C++20 186e8d8bef9SDimitry Andric 1870b57cec5SDimitry Andric // 20.7.7, class monostate 1880b57cec5SDimitry Andric struct monostate; 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric // 20.7.8, monostate relational operators 1910b57cec5SDimitry Andric constexpr bool operator==(monostate, monostate) noexcept; 192bdd1243dSDimitry Andric constexpr bool operator!=(monostate, monostate) noexcept; // until C++20 193bdd1243dSDimitry Andric constexpr bool operator<(monostate, monostate) noexcept; // until C++20 194bdd1243dSDimitry Andric constexpr bool operator>(monostate, monostate) noexcept; // until C++20 195bdd1243dSDimitry Andric constexpr bool operator<=(monostate, monostate) noexcept; // until C++20 196bdd1243dSDimitry Andric constexpr bool operator>=(monostate, monostate) noexcept; // until C++20 197bdd1243dSDimitry Andric constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // 20.7.9, specialized algorithms 2000b57cec5SDimitry Andric template <class... Types> 2010b57cec5SDimitry Andric void swap(variant<Types...>&, variant<Types...>&) noexcept(see below); 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric // 20.7.10, class bad_variant_access 2040b57cec5SDimitry Andric class bad_variant_access; 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric // 20.7.11, hash support 2070b57cec5SDimitry Andric template <class T> struct hash; 2080b57cec5SDimitry Andric template <class... Types> struct hash<variant<Types...>>; 2090b57cec5SDimitry Andric template <> struct hash<monostate>; 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric} // namespace std 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric*/ 2140b57cec5SDimitry Andric 215bdd1243dSDimitry Andric#include <__compare/common_comparison_category.h> 216bdd1243dSDimitry Andric#include <__compare/compare_three_way_result.h> 217bdd1243dSDimitry Andric#include <__compare/three_way_comparable.h> 218fe6060f1SDimitry Andric#include <__config> 21906c3fb27SDimitry Andric#include <__exception/exception.h> 220fe6060f1SDimitry Andric#include <__functional/hash.h> 221bdd1243dSDimitry Andric#include <__functional/invoke.h> 22281ad6265SDimitry Andric#include <__functional/operations.h> 22381ad6265SDimitry Andric#include <__functional/unary_function.h> 22406c3fb27SDimitry Andric#include <__memory/addressof.h> 225*0fca6ea1SDimitry Andric#include <__memory/construct_at.h> 226*0fca6ea1SDimitry Andric#include <__tuple/find_index.h> 227*0fca6ea1SDimitry Andric#include <__tuple/sfinae_helpers.h> 228bdd1243dSDimitry Andric#include <__type_traits/add_const.h> 229bdd1243dSDimitry Andric#include <__type_traits/add_cv.h> 230bdd1243dSDimitry Andric#include <__type_traits/add_pointer.h> 231bdd1243dSDimitry Andric#include <__type_traits/add_volatile.h> 232*0fca6ea1SDimitry Andric#include <__type_traits/common_type.h> 233*0fca6ea1SDimitry Andric#include <__type_traits/conjunction.h> 234bdd1243dSDimitry Andric#include <__type_traits/dependent_type.h> 235bdd1243dSDimitry Andric#include <__type_traits/is_array.h> 236*0fca6ea1SDimitry Andric#include <__type_traits/is_constructible.h> 237bdd1243dSDimitry Andric#include <__type_traits/is_destructible.h> 238*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_assignable.h> 239*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_constructible.h> 240*0fca6ea1SDimitry Andric#include <__type_traits/is_reference.h> 241*0fca6ea1SDimitry Andric#include <__type_traits/is_trivially_assignable.h> 242*0fca6ea1SDimitry Andric#include <__type_traits/is_trivially_constructible.h> 243bdd1243dSDimitry Andric#include <__type_traits/is_trivially_destructible.h> 244*0fca6ea1SDimitry Andric#include <__type_traits/is_trivially_relocatable.h> 245bdd1243dSDimitry Andric#include <__type_traits/is_void.h> 246bdd1243dSDimitry Andric#include <__type_traits/remove_const.h> 247*0fca6ea1SDimitry Andric#include <__type_traits/remove_cvref.h> 248bdd1243dSDimitry Andric#include <__type_traits/type_identity.h> 249bdd1243dSDimitry Andric#include <__type_traits/void_t.h> 25006c3fb27SDimitry Andric#include <__utility/declval.h> 251fe6060f1SDimitry Andric#include <__utility/forward.h> 2527a6dacacSDimitry Andric#include <__utility/forward_like.h> 25381ad6265SDimitry Andric#include <__utility/in_place.h> 254*0fca6ea1SDimitry Andric#include <__utility/integer_sequence.h> 25581ad6265SDimitry Andric#include <__utility/move.h> 25681ad6265SDimitry Andric#include <__utility/swap.h> 257fe6060f1SDimitry Andric#include <__variant/monostate.h> 25806c3fb27SDimitry Andric#include <__verbose_abort> 2590b57cec5SDimitry Andric#include <initializer_list> 260fe6060f1SDimitry Andric#include <limits> 2610b57cec5SDimitry Andric#include <new> 2620b57cec5SDimitry Andric#include <version> 2630b57cec5SDimitry Andric 26481ad6265SDimitry Andric// standard-mandated includes 265bdd1243dSDimitry Andric 266bdd1243dSDimitry Andric// [variant.syn] 26781ad6265SDimitry Andric#include <compare> 26881ad6265SDimitry Andric 2690b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 2700b57cec5SDimitry Andric# pragma GCC system_header 2710b57cec5SDimitry Andric#endif 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 2740b57cec5SDimitry Andric#include <__undef_macros> 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andricnamespace std { // explicitly not using versioning namespace 2770b57cec5SDimitry Andric 27806c3fb27SDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception { 2790b57cec5SDimitry Andricpublic: 280bdd1243dSDimitry Andric const char* what() const _NOEXCEPT override; 2810b57cec5SDimitry Andric}; 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric} // namespace std 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 2860b57cec5SDimitry Andric 28706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17 2880b57cec5SDimitry Andric 289fe6060f1SDimitry Andric// Light N-dimensional array of function pointers. Used in place of std::array to avoid 290fe6060f1SDimitry Andric// adding a dependency. 291fe6060f1SDimitry Andrictemplate <class _Tp, size_t _Size> 292fe6060f1SDimitry Andricstruct __farray { 293fe6060f1SDimitry Andric static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit"); 294fe6060f1SDimitry Andric _Tp __buf_[_Size] = {}; 295fe6060f1SDimitry Andric 296cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __n) const noexcept { return __buf_[__n]; } 297fe6060f1SDimitry Andric}; 298fe6060f1SDimitry Andric 299cb14a3feSDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS void 300cb14a3feSDimitry Andric__throw_bad_variant_access() { 30106c3fb27SDimitry Andric# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 3020b57cec5SDimitry Andric throw bad_variant_access(); 3030b57cec5SDimitry Andric# else 30406c3fb27SDimitry Andric _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode"); 3050b57cec5SDimitry Andric# endif 3060b57cec5SDimitry Andric} 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andrictemplate <class... _Types> 3090b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant; 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andrictemplate <class _Tp> 3120b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size; 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andrictemplate <class _Tp> 315349cc55cSDimitry Andricinline constexpr size_t variant_size_v = variant_size<_Tp>::value; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andrictemplate <class _Tp> 3180b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {}; 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andrictemplate <class _Tp> 3210b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {}; 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andrictemplate <class _Tp> 324cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> : variant_size<_Tp> {}; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andrictemplate <class... _Types> 327cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {}; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 3300b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative; 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 3330b57cec5SDimitry Andricusing variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type; 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 336cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {}; 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 339cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> : add_volatile<variant_alternative_t<_Ip, _Tp>> {}; 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andrictemplate <size_t _Ip, class _Tp> 342cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> : add_cv<variant_alternative_t<_Ip, _Tp>> {}; 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 3450b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> { 3460b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>"); 3470b57cec5SDimitry Andric using type = __type_pack_element<_Ip, _Types...>; 3480b57cec5SDimitry Andric}; 3490b57cec5SDimitry Andric 350349cc55cSDimitry Andricinline constexpr size_t variant_npos = static_cast<size_t>(-1); 3510b57cec5SDimitry Andric 352*0fca6ea1SDimitry Andrictemplate <size_t _NumAlternatives> 353*0fca6ea1SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto __choose_index_type() { 354*0fca6ea1SDimitry Andric# ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION 355*0fca6ea1SDimitry Andric if constexpr (_NumAlternatives < numeric_limits<unsigned char>::max()) 356*0fca6ea1SDimitry Andric return static_cast<unsigned char>(0); 357*0fca6ea1SDimitry Andric else if constexpr (_NumAlternatives < numeric_limits<unsigned short>::max()) 358*0fca6ea1SDimitry Andric return static_cast<unsigned short>(0); 359*0fca6ea1SDimitry Andric else 360*0fca6ea1SDimitry Andric# endif // _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION 361*0fca6ea1SDimitry Andric return static_cast<unsigned int>(0); 3620b57cec5SDimitry Andric} 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andrictemplate <size_t _NumAlts> 365*0fca6ea1SDimitry Andricusing __variant_index_t = decltype(std::__choose_index_type<_NumAlts>()); 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andrictemplate <class _IndexType> 3680b57cec5SDimitry Andricconstexpr _IndexType __variant_npos = static_cast<_IndexType>(-1); 3690b57cec5SDimitry Andric 370fe6060f1SDimitry Andrictemplate <class... _Types> 371fe6060f1SDimitry Andricclass _LIBCPP_TEMPLATE_VIS variant; 372fe6060f1SDimitry Andric 373fe6060f1SDimitry Andrictemplate <class... _Types> 374cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept { 375fe6060f1SDimitry Andric return __vs; 376fe6060f1SDimitry Andric} 377fe6060f1SDimitry Andric 378fe6060f1SDimitry Andrictemplate <class... _Types> 379cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& __as_variant(const variant<_Types...>& __vs) noexcept { 380fe6060f1SDimitry Andric return __vs; 381fe6060f1SDimitry Andric} 382fe6060f1SDimitry Andric 383fe6060f1SDimitry Andrictemplate <class... _Types> 384cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& __as_variant(variant<_Types...>&& __vs) noexcept { 3855f757f3fSDimitry Andric return std::move(__vs); 386fe6060f1SDimitry Andric} 387fe6060f1SDimitry Andric 388fe6060f1SDimitry Andrictemplate <class... _Types> 389cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& __as_variant(const variant<_Types...>&& __vs) noexcept { 3905f757f3fSDimitry Andric return std::move(__vs); 391fe6060f1SDimitry Andric} 392fe6060f1SDimitry Andric 3930b57cec5SDimitry Andricnamespace __find_detail { 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 396cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr size_t __find_index() { 3970b57cec5SDimitry Andric constexpr bool __matches[] = {is_same_v<_Tp, _Types>...}; 3980b57cec5SDimitry Andric size_t __result = __not_found; 3990b57cec5SDimitry Andric for (size_t __i = 0; __i < sizeof...(_Types); ++__i) { 4000b57cec5SDimitry Andric if (__matches[__i]) { 4010b57cec5SDimitry Andric if (__result != __not_found) { 4020b57cec5SDimitry Andric return __ambiguous; 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric __result = __i; 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric return __result; 4080b57cec5SDimitry Andric} 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andrictemplate <size_t _Index> 411cb14a3feSDimitry Andricstruct __find_unambiguous_index_sfinae_impl : integral_constant<size_t, _Index> {}; 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andrictemplate <> 4140b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__not_found> {}; 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andrictemplate <> 4170b57cec5SDimitry Andricstruct __find_unambiguous_index_sfinae_impl<__ambiguous> {}; 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 420cb14a3feSDimitry Andricstruct __find_unambiguous_index_sfinae : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {}; 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric} // namespace __find_detail 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andricnamespace __variant_detail { 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andricstruct __valueless_t {}; 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andricenum class _Trait { _TriviallyAvailable, _Available, _Unavailable }; 4290b57cec5SDimitry Andric 430cb14a3feSDimitry Andrictemplate <typename _Tp, template <typename> class _IsTriviallyAvailable, template <typename> class _IsAvailable> 4310b57cec5SDimitry Andricconstexpr _Trait __trait = 432cb14a3feSDimitry Andric _IsTriviallyAvailable<_Tp>::value ? _Trait::_TriviallyAvailable 433cb14a3feSDimitry Andric : _IsAvailable<_Tp>::value 434cb14a3feSDimitry Andric ? _Trait::_Available 435cb14a3feSDimitry Andric : _Trait::_Unavailable; 4360b57cec5SDimitry Andric 437cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Trait __common_trait(initializer_list<_Trait> __traits) { 4380b57cec5SDimitry Andric _Trait __result = _Trait::_TriviallyAvailable; 4390b57cec5SDimitry Andric for (_Trait __t : __traits) { 4400b57cec5SDimitry Andric if (static_cast<int>(__t) > static_cast<int>(__result)) { 4410b57cec5SDimitry Andric __result = __t; 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric return __result; 4450b57cec5SDimitry Andric} 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andrictemplate <typename... _Types> 4480b57cec5SDimitry Andricstruct __traits { 4490b57cec5SDimitry Andric static constexpr _Trait __copy_constructible_trait = 450cb14a3feSDimitry Andric __variant_detail::__common_trait({__trait<_Types, is_trivially_copy_constructible, is_copy_constructible>...}); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric static constexpr _Trait __move_constructible_trait = 453cb14a3feSDimitry Andric __variant_detail::__common_trait({__trait<_Types, is_trivially_move_constructible, is_move_constructible>...}); 4540b57cec5SDimitry Andric 455bdd1243dSDimitry Andric static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait( 456cb14a3feSDimitry Andric {__copy_constructible_trait, __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...}); 4570b57cec5SDimitry Andric 458bdd1243dSDimitry Andric static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait( 459cb14a3feSDimitry Andric {__move_constructible_trait, __trait<_Types, is_trivially_move_assignable, is_move_assignable>...}); 4600b57cec5SDimitry Andric 461cb14a3feSDimitry Andric static constexpr _Trait __destructible_trait = 462cb14a3feSDimitry Andric __variant_detail::__common_trait({__trait<_Types, is_trivially_destructible, is_destructible>...}); 4630b57cec5SDimitry Andric}; 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andricnamespace __access { 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andricstruct __union { 4680b57cec5SDimitry Andric template <class _Vp> 469cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) { 4705f757f3fSDimitry Andric return std::forward<_Vp>(__v).__head; 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric template <class _Vp, size_t _Ip> 474cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) { 4755f757f3fSDimitry Andric return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>); 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric}; 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andricstruct __base { 4800b57cec5SDimitry Andric template <size_t _Ip, class _Vp> 481cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) { 482cb14a3feSDimitry Andric return __union::__get_alt(std::forward<_Vp>(__v).__data, in_place_index<_Ip>); 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric}; 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andricstruct __variant { 4870b57cec5SDimitry Andric template <size_t _Ip, class _Vp> 488cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) { 4895f757f3fSDimitry Andric return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_); 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric}; 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric} // namespace __access 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andricnamespace __visitation { 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andricstruct __base { 4980b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 499cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) 5000b57cec5SDimitry Andric __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 501cb14a3feSDimitry Andric constexpr auto __fdiagonal = __make_fdiagonal<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>(); 502cb14a3feSDimitry Andric return __fdiagonal[__index](std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...); 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 506cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) { 507cb14a3feSDimitry Andric constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>(); 508cb14a3feSDimitry Andric return __at(__fmatrix, __vs.index()...)(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andricprivate: 5120b57cec5SDimitry Andric template <class _Tp> 513cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr const _Tp& __at(const _Tp& __elem) { 514cb14a3feSDimitry Andric return __elem; 515cb14a3feSDimitry Andric } 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric template <class _Tp, size_t _Np, typename... _Indices> 518cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto&& 519cb14a3feSDimitry Andric __at(const __farray<_Tp, _Np>& __elems, size_t __index, _Indices... __indices) { 5200b57cec5SDimitry Andric return __at(__elems[__index], __indices...); 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric template <class _Fp, class... _Fs> 52406c3fb27SDimitry Andric static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() { 5250b57cec5SDimitry Andric static_assert( 526cb14a3feSDimitry Andric __all<is_same_v<_Fp, _Fs>...>::value, "`std::visit` requires the visitor to have a single return type."); 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric template <class... _Fs> 530cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_farray(_Fs&&... __fs) { 531bdd1243dSDimitry Andric __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>(); 532bdd1243dSDimitry Andric using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>; 5335f757f3fSDimitry Andric return __result{{std::forward<_Fs>(__fs)...}}; 5340b57cec5SDimitry Andric } 5350b57cec5SDimitry Andric 536e8d8bef9SDimitry Andric template <size_t... _Is> 5370b57cec5SDimitry Andric struct __dispatcher { 5380b57cec5SDimitry Andric template <class _Fp, class... _Vs> 539cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) { 540cb14a3feSDimitry Andric return std::__invoke(static_cast<_Fp>(__f), __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...); 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric }; 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is> 545cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_dispatch(index_sequence<_Is...>) { 5460b57cec5SDimitry Andric return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>; 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric template <size_t _Ip, class _Fp, class... _Vs> 550cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl() { 551cb14a3feSDimitry Andric return __make_dispatch<_Fp, _Vs...>(index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{}); 5520b57cec5SDimitry Andric } 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is> 555cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) { 5560b57cec5SDimitry Andric return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...); 5570b57cec5SDimitry Andric } 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric template <class _Fp, class _Vp, class... _Vs> 560cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal() { 56106c3fb27SDimitry Andric constexpr size_t __np = __remove_cvref_t<_Vp>::__size(); 56206c3fb27SDimitry Andric static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value); 56306c3fb27SDimitry Andric return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{}); 5640b57cec5SDimitry Andric } 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is> 567cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) { 5680b57cec5SDimitry Andric return __make_dispatch<_Fp, _Vs...>(__is); 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls> 572cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto 573cb14a3feSDimitry Andric __make_fmatrix_impl(index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) { 574cb14a3feSDimitry Andric return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(index_sequence<_Is..., _Js>{}, __ls...)...); 5750b57cec5SDimitry Andric } 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric template <class _Fp, class... _Vs> 578cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix() { 5790b57cec5SDimitry Andric return __make_fmatrix_impl<_Fp, _Vs...>( 580bdd1243dSDimitry Andric index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...); 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric}; 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andricstruct __variant { 5850b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 586cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) 5870b57cec5SDimitry Andric __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 588cb14a3feSDimitry Andric return __base::__visit_alt_at(__index, std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__impl_...); 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 592cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) { 593fe6060f1SDimitry Andric return __base::__visit_alt( 594cb14a3feSDimitry Andric std::forward<_Visitor>(__visitor), std::__as_variant(std::forward<_Vs>(__vs)).__impl_...); 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 598cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) 5990b57cec5SDimitry Andric __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 600cb14a3feSDimitry Andric return __visit_alt_at(__index, __make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...); 6010b57cec5SDimitry Andric } 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric template <class _Visitor, class... _Vs> 604cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, _Vs&&... __vs) { 605cb14a3feSDimitry Andric return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...); 6060b57cec5SDimitry Andric } 607fe6060f1SDimitry Andric 60806c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 20 609e8d8bef9SDimitry Andric template <class _Rp, class _Visitor, class... _Vs> 610cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr _Rp __visit_value(_Visitor&& __visitor, _Vs&&... __vs) { 611cb14a3feSDimitry Andric return __visit_alt(__make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...); 612e8d8bef9SDimitry Andric } 613e8d8bef9SDimitry Andric# endif 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andricprivate: 6160b57cec5SDimitry Andric template <class _Visitor, class... _Values> 61706c3fb27SDimitry Andric static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() { 618cb14a3feSDimitry Andric static_assert(is_invocable_v<_Visitor, _Values...>, "`std::visit` requires the visitor to be exhaustive."); 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric template <class _Visitor> 6220b57cec5SDimitry Andric struct __value_visitor { 6230b57cec5SDimitry Andric template <class... _Alts> 624cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Alts&&... __alts) const { 625cb14a3feSDimitry Andric __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>(); 626cb14a3feSDimitry Andric return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric _Visitor&& __visitor; 6290b57cec5SDimitry Andric }; 6300b57cec5SDimitry Andric 63106c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 20 632e8d8bef9SDimitry Andric template <class _Rp, class _Visitor> 633e8d8bef9SDimitry Andric struct __value_visitor_return_type { 634e8d8bef9SDimitry Andric template <class... _Alts> 635cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Rp operator()(_Alts&&... __alts) const { 636cb14a3feSDimitry Andric __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>(); 637e8d8bef9SDimitry Andric if constexpr (is_void_v<_Rp>) { 638cb14a3feSDimitry Andric std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...); 639cb14a3feSDimitry Andric } else { 640cb14a3feSDimitry Andric return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...); 641e8d8bef9SDimitry Andric } 642e8d8bef9SDimitry Andric } 643e8d8bef9SDimitry Andric 644e8d8bef9SDimitry Andric _Visitor&& __visitor; 645e8d8bef9SDimitry Andric }; 646e8d8bef9SDimitry Andric# endif 647e8d8bef9SDimitry Andric 6480b57cec5SDimitry Andric template <class _Visitor> 649cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) { 6505f757f3fSDimitry Andric return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)}; 6510b57cec5SDimitry Andric } 652e8d8bef9SDimitry Andric 65306c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 20 654e8d8bef9SDimitry Andric template <class _Rp, class _Visitor> 655cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) { 6565f757f3fSDimitry Andric return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)}; 657e8d8bef9SDimitry Andric } 658e8d8bef9SDimitry Andric# endif 6590b57cec5SDimitry Andric}; 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric} // namespace __visitation 6620b57cec5SDimitry Andric 663*0fca6ea1SDimitry Andric// Adding semi-colons in macro expansions helps clang-format to do a better job. 664*0fca6ea1SDimitry Andric// This macro is used to avoid compilation errors due to "stray" semi-colons. 665*0fca6ea1SDimitry Andric# define _LIBCPP_EAT_SEMICOLON static_assert(true, "") 666*0fca6ea1SDimitry Andric 6670b57cec5SDimitry Andrictemplate <size_t _Index, class _Tp> 6680b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __alt { 6690b57cec5SDimitry Andric using __value_type = _Tp; 670*0fca6ea1SDimitry Andric static constexpr size_t __index = _Index; 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric template <class... _Args> 673cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args) 6745f757f3fSDimitry Andric : __value(std::forward<_Args>(__args)...) {} 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric __value_type __value; 6770b57cec5SDimitry Andric}; 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index, class... _Types> 6800b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union; 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, size_t _Index> 6830b57cec5SDimitry Andricunion _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {}; 6840b57cec5SDimitry Andric 685*0fca6ea1SDimitry Andric# define _LIBCPP_VARIANT_UNION(destructible_trait, destructor_definition) \ 6860b57cec5SDimitry Andric template <size_t _Index, class _Tp, class... _Types> \ 687cb14a3feSDimitry Andric union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, _Index, _Tp, _Types...> { \ 6880b57cec5SDimitry Andric public: \ 689cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \ 6900b57cec5SDimitry Andric \ 6910b57cec5SDimitry Andric template <class... _Args> \ 692cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \ 6935f757f3fSDimitry Andric : __head(in_place, std::forward<_Args>(__args)...) {} \ 6940b57cec5SDimitry Andric \ 6950b57cec5SDimitry Andric template <size_t _Ip, class... _Args> \ 696cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \ 6975f757f3fSDimitry Andric : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {} \ 6980b57cec5SDimitry Andric \ 699*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __union(const __union&) = default; \ 700*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __union(__union&&) = default; \ 701*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __union& operator=(const __union&) = default; \ 702*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __union& operator=(__union&&) = default; \ 703*0fca6ea1SDimitry Andric destructor_definition; \ 7040b57cec5SDimitry Andric \ 7050b57cec5SDimitry Andric private: \ 7060b57cec5SDimitry Andric char __dummy; \ 7070b57cec5SDimitry Andric __alt<_Index, _Tp> __head; \ 7080b57cec5SDimitry Andric __union<destructible_trait, _Index + 1, _Types...> __tail; \ 7090b57cec5SDimitry Andric \ 7100b57cec5SDimitry Andric friend struct __access::__union; \ 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 713*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, 714*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = default); 715*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_UNION( 716*0fca6ea1SDimitry Andric _Trait::_Available, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() {} _LIBCPP_EAT_SEMICOLON); 717*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = delete); 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric# undef _LIBCPP_VARIANT_UNION 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andrictemplate <_Trait _DestructibleTrait, class... _Types> 7220b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __base { 7230b57cec5SDimitry Andricpublic: 7240b57cec5SDimitry Andric using __index_t = __variant_index_t<sizeof...(_Types)>; 7250b57cec5SDimitry Andric 726cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(__valueless_t __tag) noexcept 727753f127fSDimitry Andric : __data(__tag), __index(__variant_npos<__index_t>) {} 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric template <size_t _Ip, class... _Args> 730cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args) 731cb14a3feSDimitry Andric : __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), __index(_Ip) {} 7320b57cec5SDimitry Andric 733cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { return index() == variant_npos; } 7340b57cec5SDimitry Andric 735cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { 7360b57cec5SDimitry Andric return __index == __variant_npos<__index_t> ? variant_npos : __index; 7370b57cec5SDimitry Andric } 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andricprotected: 740cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() & { return *this; } 7410b57cec5SDimitry Andric 742cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() && { return std::move(*this); } 7430b57cec5SDimitry Andric 744cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const& { return *this; } 7450b57cec5SDimitry Andric 746cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const&& { return std::move(*this); } 7470b57cec5SDimitry Andric 748cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Types); } 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric __union<_DestructibleTrait, 0, _Types...> __data; 7510b57cec5SDimitry Andric __index_t __index; 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric friend struct __access::__base; 7540b57cec5SDimitry Andric friend struct __visitation::__base; 7550b57cec5SDimitry Andric}; 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__destructible_trait> 758e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __dtor; 7590b57cec5SDimitry Andric 760*0fca6ea1SDimitry Andric# define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor_definition, destroy) \ 7610b57cec5SDimitry Andric template <class... _Types> \ 762cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, destructible_trait> \ 7630b57cec5SDimitry Andric : public __base<destructible_trait, _Types...> { \ 7640b57cec5SDimitry Andric using __base_type = __base<destructible_trait, _Types...>; \ 7650b57cec5SDimitry Andric using __index_t = typename __base_type::__index_t; \ 7660b57cec5SDimitry Andric \ 7670b57cec5SDimitry Andric public: \ 7680b57cec5SDimitry Andric using __base_type::__base_type; \ 7690b57cec5SDimitry Andric using __base_type::operator=; \ 770*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __dtor(const __dtor&) = default; \ 771*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __dtor(__dtor&&) = default; \ 772*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __dtor& operator=(const __dtor&) = default; \ 773*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __dtor& operator=(__dtor&&) = default; \ 774*0fca6ea1SDimitry Andric destructor_definition; \ 7750b57cec5SDimitry Andric \ 7760b57cec5SDimitry Andric protected: \ 777*0fca6ea1SDimitry Andric destroy; \ 7780b57cec5SDimitry Andric } 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR( 781*0fca6ea1SDimitry Andric _Trait::_TriviallyAvailable, 782*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = default, 783*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept { 784*0fca6ea1SDimitry Andric this->__index = __variant_npos<__index_t>; 785*0fca6ea1SDimitry Andric } _LIBCPP_EAT_SEMICOLON); 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR( 7880b57cec5SDimitry Andric _Trait::_Available, 789*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() { __destroy(); } _LIBCPP_EAT_SEMICOLON, 790*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept { 7910b57cec5SDimitry Andric if (!this->valueless_by_exception()) { 7920b57cec5SDimitry Andric __visitation::__base::__visit_alt( 7930b57cec5SDimitry Andric [](auto& __alt) noexcept { 794bdd1243dSDimitry Andric using __alt_type = __remove_cvref_t<decltype(__alt)>; 7950b57cec5SDimitry Andric __alt.~__alt_type(); 7960b57cec5SDimitry Andric }, 7970b57cec5SDimitry Andric *this); 7980b57cec5SDimitry Andric } 7990b57cec5SDimitry Andric this->__index = __variant_npos<__index_t>; 800*0fca6ea1SDimitry Andric } _LIBCPP_EAT_SEMICOLON); 8010b57cec5SDimitry Andric 802*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable, 803*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = delete, 804*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept = delete); 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric# undef _LIBCPP_VARIANT_DESTRUCTOR 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andrictemplate <class _Traits> 809e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> { 810e8d8bef9SDimitry Andric using __base_type = __dtor<_Traits>; 8110b57cec5SDimitry Andric 8120b57cec5SDimitry Andricpublic: 8130b57cec5SDimitry Andric using __base_type::__base_type; 8140b57cec5SDimitry Andric using __base_type::operator=; 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andricprotected: 8170b57cec5SDimitry Andric template <class _Rhs> 818*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) { 8190b57cec5SDimitry Andric __lhs.__destroy(); 8200b57cec5SDimitry Andric if (!__rhs.valueless_by_exception()) { 8217a6dacacSDimitry Andric auto __rhs_index = __rhs.index(); 8220b57cec5SDimitry Andric __visitation::__base::__visit_alt_at( 8237a6dacacSDimitry Andric __rhs_index, 824*0fca6ea1SDimitry Andric [&__lhs](auto&& __rhs_alt) { 825*0fca6ea1SDimitry Andric std::__construct_at(std::addressof(__lhs.__data), 826*0fca6ea1SDimitry Andric in_place_index<__decay_t<decltype(__rhs_alt)>::__index>, 827*0fca6ea1SDimitry Andric std::forward<decltype(__rhs_alt)>(__rhs_alt).__value); 8280b57cec5SDimitry Andric }, 829cb14a3feSDimitry Andric std::forward<_Rhs>(__rhs)); 8307a6dacacSDimitry Andric __lhs.__index = __rhs_index; 8310b57cec5SDimitry Andric } 8320b57cec5SDimitry Andric } 8330b57cec5SDimitry Andric}; 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_constructible_trait> 8360b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_constructor; 8370b57cec5SDimitry Andric 838*0fca6ea1SDimitry Andric# define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, move_constructor_definition) \ 8390b57cec5SDimitry Andric template <class... _Types> \ 840cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, move_constructible_trait> \ 841e8d8bef9SDimitry Andric : public __ctor<__traits<_Types...>> { \ 842e8d8bef9SDimitry Andric using __base_type = __ctor<__traits<_Types...>>; \ 8430b57cec5SDimitry Andric \ 8440b57cec5SDimitry Andric public: \ 8450b57cec5SDimitry Andric using __base_type::__base_type; \ 8460b57cec5SDimitry Andric using __base_type::operator=; \ 8470b57cec5SDimitry Andric \ 848*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __move_constructor(const __move_constructor&) = default; \ 849*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~__move_constructor() = default; \ 850*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(const __move_constructor&) = default; \ 851*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(__move_constructor&&) = default; \ 852*0fca6ea1SDimitry Andric move_constructor_definition; \ 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 855*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 856*0fca6ea1SDimitry Andric _Trait::_TriviallyAvailable, 857*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) = default); 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 8600b57cec5SDimitry Andric _Trait::_Available, 861*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) noexcept( 862*0fca6ea1SDimitry Andric __all<is_nothrow_move_constructible_v<_Types>...>::value) 863*0fca6ea1SDimitry Andric : __move_constructor(__valueless_t{}) { 864*0fca6ea1SDimitry Andric this->__generic_construct(*this, std::move(__that)); 865*0fca6ea1SDimitry Andric } _LIBCPP_EAT_SEMICOLON); 8660b57cec5SDimitry Andric 867*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 868*0fca6ea1SDimitry Andric _Trait::_Unavailable, 869*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&&) = delete); 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric# undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_constructible_trait> 8740b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_constructor; 8750b57cec5SDimitry Andric 876*0fca6ea1SDimitry Andric# define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, copy_constructor_definition) \ 8770b57cec5SDimitry Andric template <class... _Types> \ 878cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, copy_constructible_trait> \ 8790b57cec5SDimitry Andric : public __move_constructor<__traits<_Types...>> { \ 8800b57cec5SDimitry Andric using __base_type = __move_constructor<__traits<_Types...>>; \ 8810b57cec5SDimitry Andric \ 8820b57cec5SDimitry Andric public: \ 8830b57cec5SDimitry Andric using __base_type::__base_type; \ 8840b57cec5SDimitry Andric using __base_type::operator=; \ 8850b57cec5SDimitry Andric \ 886*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __copy_constructor(__copy_constructor&&) = default; \ 887*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~__copy_constructor() = default; \ 888*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(const __copy_constructor&) = default; \ 889*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(__copy_constructor&&) = default; \ 890*0fca6ea1SDimitry Andric copy_constructor_definition; \ 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric 893*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 894*0fca6ea1SDimitry Andric _Trait::_TriviallyAvailable, 895*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) = default); 8960b57cec5SDimitry Andric 8970b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 898*0fca6ea1SDimitry Andric _Trait::_Available, 899*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) 900*0fca6ea1SDimitry Andric : __copy_constructor(__valueless_t{}) { this->__generic_construct(*this, __that); } _LIBCPP_EAT_SEMICOLON); 9010b57cec5SDimitry Andric 902*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 903*0fca6ea1SDimitry Andric _Trait::_Unavailable, 904*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor&) = delete); 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric# undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andrictemplate <class _Traits> 9090b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> { 9100b57cec5SDimitry Andric using __base_type = __copy_constructor<_Traits>; 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andricpublic: 9130b57cec5SDimitry Andric using __base_type::__base_type; 9140b57cec5SDimitry Andric using __base_type::operator=; 9150b57cec5SDimitry Andric 9160b57cec5SDimitry Andric template <size_t _Ip, class... _Args> 917*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto& __emplace(_Args&&... __args) { 9180b57cec5SDimitry Andric this->__destroy(); 919*0fca6ea1SDimitry Andric std::__construct_at(std::addressof(this->__data), in_place_index<_Ip>, std::forward<_Args>(__args)...); 9200b57cec5SDimitry Andric this->__index = _Ip; 921*0fca6ea1SDimitry Andric return __access::__base::__get_alt<_Ip>(*this).__value; 9220b57cec5SDimitry Andric } 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andricprotected: 9250b57cec5SDimitry Andric template <size_t _Ip, class _Tp, class _Arg> 926*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) { 9270b57cec5SDimitry Andric if (this->index() == _Ip) { 9285f757f3fSDimitry Andric __a.__value = std::forward<_Arg>(__arg); 9290b57cec5SDimitry Andric } else { 9300b57cec5SDimitry Andric struct { 931*0fca6ea1SDimitry Andric _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(true_type) const { 932*0fca6ea1SDimitry Andric __this->__emplace<_Ip>(std::forward<_Arg>(__arg)); 933*0fca6ea1SDimitry Andric } 934*0fca6ea1SDimitry Andric _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(false_type) const { 9355f757f3fSDimitry Andric __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg))); 9360b57cec5SDimitry Andric } 9370b57cec5SDimitry Andric __assignment* __this; 9380b57cec5SDimitry Andric _Arg&& __arg; 9395f757f3fSDimitry Andric } __impl{this, std::forward<_Arg>(__arg)}; 940cb14a3feSDimitry Andric __impl(bool_constant < is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v < _Tp >> {}); 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric } 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric template <class _That> 945*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_assign(_That&& __that) { 9460b57cec5SDimitry Andric if (this->valueless_by_exception() && __that.valueless_by_exception()) { 9470b57cec5SDimitry Andric // do nothing. 9480b57cec5SDimitry Andric } else if (__that.valueless_by_exception()) { 9490b57cec5SDimitry Andric this->__destroy(); 9500b57cec5SDimitry Andric } else { 9510b57cec5SDimitry Andric __visitation::__base::__visit_alt_at( 9520b57cec5SDimitry Andric __that.index(), 9530b57cec5SDimitry Andric [this](auto& __this_alt, auto&& __that_alt) { 954cb14a3feSDimitry Andric this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value); 9550b57cec5SDimitry Andric }, 956cb14a3feSDimitry Andric *this, 957cb14a3feSDimitry Andric std::forward<_That>(__that)); 9580b57cec5SDimitry Andric } 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric}; 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__move_assignable_trait> 9630b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __move_assignment; 9640b57cec5SDimitry Andric 965*0fca6ea1SDimitry Andric# define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment_definition) \ 9660b57cec5SDimitry Andric template <class... _Types> \ 967cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, move_assignable_trait> \ 9680b57cec5SDimitry Andric : public __assignment<__traits<_Types...>> { \ 9690b57cec5SDimitry Andric using __base_type = __assignment<__traits<_Types...>>; \ 9700b57cec5SDimitry Andric \ 9710b57cec5SDimitry Andric public: \ 9720b57cec5SDimitry Andric using __base_type::__base_type; \ 9730b57cec5SDimitry Andric using __base_type::operator=; \ 9740b57cec5SDimitry Andric \ 975*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __move_assignment(const __move_assignment&) = default; \ 976*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __move_assignment(__move_assignment&&) = default; \ 977*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~__move_assignment() = default; \ 978*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __move_assignment& operator=(const __move_assignment&) = default; \ 979*0fca6ea1SDimitry Andric move_assignment_definition; \ 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric 982cb14a3feSDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable, 983*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=( 984*0fca6ea1SDimitry Andric __move_assignment&& __that) = default); 9850b57cec5SDimitry Andric 9860b57cec5SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 9870b57cec5SDimitry Andric _Trait::_Available, 988*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& 989cb14a3feSDimitry Andric operator=(__move_assignment&& __that) noexcept( 990cb14a3feSDimitry Andric __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) { 9915f757f3fSDimitry Andric this->__generic_assign(std::move(__that)); 9920b57cec5SDimitry Andric return *this; 993*0fca6ea1SDimitry Andric } _LIBCPP_EAT_SEMICOLON); 9940b57cec5SDimitry Andric 995*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 996*0fca6ea1SDimitry Andric _Trait::_Unavailable, 997*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(__move_assignment&&) = delete); 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric# undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT 10000b57cec5SDimitry Andric 10010b57cec5SDimitry Andrictemplate <class _Traits, _Trait = _Traits::__copy_assignable_trait> 10020b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __copy_assignment; 10030b57cec5SDimitry Andric 1004*0fca6ea1SDimitry Andric# define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment_definition) \ 10050b57cec5SDimitry Andric template <class... _Types> \ 1006cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, copy_assignable_trait> \ 10070b57cec5SDimitry Andric : public __move_assignment<__traits<_Types...>> { \ 10080b57cec5SDimitry Andric using __base_type = __move_assignment<__traits<_Types...>>; \ 10090b57cec5SDimitry Andric \ 10100b57cec5SDimitry Andric public: \ 10110b57cec5SDimitry Andric using __base_type::__base_type; \ 10120b57cec5SDimitry Andric using __base_type::operator=; \ 10130b57cec5SDimitry Andric \ 1014*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __copy_assignment(const __copy_assignment&) = default; \ 1015*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __copy_assignment(__copy_assignment&&) = default; \ 1016*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~__copy_assignment() = default; \ 1017*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __copy_assignment& operator=(__copy_assignment&&) = default; \ 1018*0fca6ea1SDimitry Andric copy_assignment_definition; \ 10190b57cec5SDimitry Andric } 10200b57cec5SDimitry Andric 1021cb14a3feSDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable, 1022*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=( 1023*0fca6ea1SDimitry Andric const __copy_assignment& __that) = default); 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT( 1026*0fca6ea1SDimitry Andric _Trait::_Available, 1027*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& 1028*0fca6ea1SDimitry Andric operator=(const __copy_assignment& __that) { 10290b57cec5SDimitry Andric this->__generic_assign(__that); 10300b57cec5SDimitry Andric return *this; 1031*0fca6ea1SDimitry Andric } _LIBCPP_EAT_SEMICOLON); 10320b57cec5SDimitry Andric 1033*0fca6ea1SDimitry Andric_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable, 1034*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=( 1035*0fca6ea1SDimitry Andric const __copy_assignment&) = delete); 10360b57cec5SDimitry Andric 10370b57cec5SDimitry Andric# undef _LIBCPP_VARIANT_COPY_ASSIGNMENT 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andrictemplate <class... _Types> 1040cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __impl : public __copy_assignment<__traits<_Types...>> { 10410b57cec5SDimitry Andric using __base_type = __copy_assignment<__traits<_Types...>>; 10420b57cec5SDimitry Andric 10430b57cec5SDimitry Andricpublic: 104481ad6265SDimitry Andric using __base_type::__base_type; // get in_place_index_t constructor & friends 104506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default; 104606c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default; 104706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default; 104806c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default; 10490b57cec5SDimitry Andric 10500b57cec5SDimitry Andric template <size_t _Ip, class _Arg> 1051*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign(_Arg&& __arg) { 1052cb14a3feSDimitry Andric this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg)); 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric 1055*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__impl& __that) { 10560b57cec5SDimitry Andric if (this->valueless_by_exception() && __that.valueless_by_exception()) { 10570b57cec5SDimitry Andric // do nothing. 10580b57cec5SDimitry Andric } else if (this->index() == __that.index()) { 10590b57cec5SDimitry Andric __visitation::__base::__visit_alt_at( 10600b57cec5SDimitry Andric this->index(), 10610b57cec5SDimitry Andric [](auto& __this_alt, auto& __that_alt) { 10625f757f3fSDimitry Andric using std::swap; 10630b57cec5SDimitry Andric swap(__this_alt.__value, __that_alt.__value); 10640b57cec5SDimitry Andric }, 10650b57cec5SDimitry Andric *this, 10660b57cec5SDimitry Andric __that); 10670b57cec5SDimitry Andric } else { 10680b57cec5SDimitry Andric __impl* __lhs = this; 10695f757f3fSDimitry Andric __impl* __rhs = std::addressof(__that); 10700b57cec5SDimitry Andric if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) { 10715f757f3fSDimitry Andric std::swap(__lhs, __rhs); 10720b57cec5SDimitry Andric } 10735f757f3fSDimitry Andric __impl __tmp(std::move(*__rhs)); 107406c3fb27SDimitry Andric# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 10755ffd83dbSDimitry Andric if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) { 10765f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(*__lhs)); 10775ffd83dbSDimitry Andric } else { 10780b57cec5SDimitry Andric // EXTENSION: When the move construction of `__lhs` into `__rhs` throws 10790b57cec5SDimitry Andric // and `__tmp` is nothrow move constructible then we move `__tmp` back 10800b57cec5SDimitry Andric // into `__rhs` and provide the strong exception safety guarantee. 10810b57cec5SDimitry Andric try { 10825f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(*__lhs)); 10830b57cec5SDimitry Andric } catch (...) { 10840b57cec5SDimitry Andric if (__tmp.__move_nothrow()) { 10855f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(__tmp)); 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric throw; 10880b57cec5SDimitry Andric } 10895ffd83dbSDimitry Andric } 10900b57cec5SDimitry Andric# else 10915ffd83dbSDimitry Andric // this isn't consolidated with the `if constexpr` branch above due to 10925ffd83dbSDimitry Andric // `throw` being ill-formed with exceptions disabled even when discarded. 10935f757f3fSDimitry Andric this->__generic_construct(*__rhs, std::move(*__lhs)); 10940b57cec5SDimitry Andric# endif 10955f757f3fSDimitry Andric this->__generic_construct(*__lhs, std::move(__tmp)); 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andricprivate: 1100*0fca6ea1SDimitry Andric constexpr inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const { 11010b57cec5SDimitry Andric constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...}; 11020b57cec5SDimitry Andric return this->valueless_by_exception() || __results[this->index()]; 11030b57cec5SDimitry Andric } 11040b57cec5SDimitry Andric}; 11050b57cec5SDimitry Andric 11060b57cec5SDimitry Andricstruct __no_narrowing_check { 11070b57cec5SDimitry Andric template <class _Dest, class _Source> 110881ad6265SDimitry Andric using _Apply = __type_identity<_Dest>; 11090b57cec5SDimitry Andric}; 11100b57cec5SDimitry Andric 11110b57cec5SDimitry Andricstruct __narrowing_check { 11120b57cec5SDimitry Andric template <class _Dest> 111381ad6265SDimitry Andric static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>; 11140b57cec5SDimitry Andric template <class _Dest, class _Source> 1115bdd1243dSDimitry Andric using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()})); 11160b57cec5SDimitry Andric}; 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andrictemplate <class _Dest, class _Source> 1119*0fca6ea1SDimitry Andricusing __check_for_narrowing _LIBCPP_NODEBUG = 1120*0fca6ea1SDimitry Andric typename _If< is_arithmetic<_Dest>::value, __narrowing_check, __no_narrowing_check >::template _Apply<_Dest, 1121*0fca6ea1SDimitry Andric _Source>; 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andrictemplate <class _Tp, size_t _Idx> 11240b57cec5SDimitry Andricstruct __overload { 11250b57cec5SDimitry Andric template <class _Up> 11260b57cec5SDimitry Andric auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>; 11270b57cec5SDimitry Andric}; 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andrictemplate <class... _Bases> 11300b57cec5SDimitry Andricstruct __all_overloads : _Bases... { 11310b57cec5SDimitry Andric void operator()() const; 11320b57cec5SDimitry Andric using _Bases::operator()...; 11330b57cec5SDimitry Andric}; 11340b57cec5SDimitry Andric 113506c3fb27SDimitry Andrictemplate <class _IdxSeq> 11360b57cec5SDimitry Andricstruct __make_overloads_imp; 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andrictemplate <size_t... _Idx> 11390b57cec5SDimitry Andricstruct __make_overloads_imp<__tuple_indices<_Idx...> > { 11400b57cec5SDimitry Andric template <class... _Types> 1141349cc55cSDimitry Andric using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>; 11420b57cec5SDimitry Andric}; 11430b57cec5SDimitry Andric 11440b57cec5SDimitry Andrictemplate <class... _Types> 1145cb14a3feSDimitry Andricusing _MakeOverloads _LIBCPP_NODEBUG = 1146cb14a3feSDimitry Andric typename __make_overloads_imp< __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>; 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1149cb14a3feSDimitry Andricusing __best_match_t = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type; 11500b57cec5SDimitry Andric 11511fd87a68SDimitry Andric} // namespace __variant_detail 11520b57cec5SDimitry Andric 11537a6dacacSDimitry Andrictemplate <class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>> 11547a6dacacSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto) 11557a6dacacSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs); 11567a6dacacSDimitry Andric 11577a6dacacSDimitry Andric# if _LIBCPP_STD_VER >= 20 11587a6dacacSDimitry Andrictemplate <class _Rp, 11597a6dacacSDimitry Andric class _Visitor, 11607a6dacacSDimitry Andric class... _Vs, 11617a6dacacSDimitry Andric typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>> 11627a6dacacSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp 11637a6dacacSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs); 11647a6dacacSDimitry Andric# endif 11657a6dacacSDimitry Andric 11660b57cec5SDimitry Andrictemplate <class... _Types> 116706c3fb27SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant 1168cb14a3feSDimitry Andric : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value, 11690b57cec5SDimitry Andric __all<is_move_constructible_v<_Types>...>::value>, 11700b57cec5SDimitry Andric private __sfinae_assign_base< 1171cb14a3feSDimitry Andric __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value, 1172cb14a3feSDimitry Andric __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> { 1173cb14a3feSDimitry Andric static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative."); 11740b57cec5SDimitry Andric 1175cb14a3feSDimitry Andric static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative."); 11760b57cec5SDimitry Andric 1177cb14a3feSDimitry Andric static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative."); 11780b57cec5SDimitry Andric 1179cb14a3feSDimitry Andric static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative."); 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric using __first_type = variant_alternative_t<0, variant>; 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andricpublic: 1184*0fca6ea1SDimitry Andric using __trivially_relocatable = 1185*0fca6ea1SDimitry Andric conditional_t<_And<__libcpp_is_trivially_relocatable<_Types>...>::value, variant, void>; 1186*0fca6ea1SDimitry Andric 11870b57cec5SDimitry Andric template <bool _Dummy = true, 1188cb14a3feSDimitry Andric enable_if_t<__dependent_type<is_default_constructible<__first_type>, _Dummy>::value, int> = 0> 1189cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>) 1190bdd1243dSDimitry Andric : __impl_(in_place_index<0>) {} 11910b57cec5SDimitry Andric 119206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default; 119306c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default; 11940b57cec5SDimitry Andric 1195cb14a3feSDimitry Andric template < class _Arg, 1196bdd1243dSDimitry Andric enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0, 1197bdd1243dSDimitry Andric enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0, 1198bdd1243dSDimitry Andric enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0, 11990b57cec5SDimitry Andric class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, 1200cb14a3feSDimitry Andric size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 12010b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0> 1202cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>) 12035f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {} 12040b57cec5SDimitry Andric 1205cb14a3feSDimitry Andric template <size_t _Ip, 1206cb14a3feSDimitry Andric class... _Args, 12070b57cec5SDimitry Andric class = enable_if_t<(_Ip < sizeof...(_Types)), int>, 12080b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 12090b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1210cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept( 1211cb14a3feSDimitry Andric is_nothrow_constructible_v<_Tp, _Args...>) 12125f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {} 12130b57cec5SDimitry Andric 1214cb14a3feSDimitry Andric template < size_t _Ip, 12150b57cec5SDimitry Andric class _Up, 12160b57cec5SDimitry Andric class... _Args, 12170b57cec5SDimitry Andric enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 12180b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 1219cb14a3feSDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0> 1220cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr variant( 12210b57cec5SDimitry Andric in_place_index_t<_Ip>, 12220b57cec5SDimitry Andric initializer_list<_Up> __il, 1223cb14a3feSDimitry Andric _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) 12245f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {} 12250b57cec5SDimitry Andric 1226cb14a3feSDimitry Andric template < class _Tp, 12270b57cec5SDimitry Andric class... _Args, 1228cb14a3feSDimitry Andric size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 12290b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1230cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept( 12310b57cec5SDimitry Andric is_nothrow_constructible_v<_Tp, _Args...>) 12325f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {} 12330b57cec5SDimitry Andric 1234cb14a3feSDimitry Andric template < class _Tp, 12350b57cec5SDimitry Andric class _Up, 12360b57cec5SDimitry Andric class... _Args, 1237cb14a3feSDimitry Andric size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1238cb14a3feSDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0> 1239cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr variant( 12400b57cec5SDimitry Andric in_place_type_t<_Tp>, 12410b57cec5SDimitry Andric initializer_list<_Up> __il, 1242cb14a3feSDimitry Andric _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>) 12435f757f3fSDimitry Andric : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {} 12440b57cec5SDimitry Andric 1245*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~variant() = default; 12460b57cec5SDimitry Andric 124706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default; 124806c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default; 12490b57cec5SDimitry Andric 1250cb14a3feSDimitry Andric template < class _Arg, 1251bdd1243dSDimitry Andric enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0, 12520b57cec5SDimitry Andric class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, 1253cb14a3feSDimitry Andric size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1254cb14a3feSDimitry Andric enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0> 1255*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 variant& 1256cb14a3feSDimitry Andric operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) { 12575f757f3fSDimitry Andric __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg)); 12580b57cec5SDimitry Andric return *this; 12590b57cec5SDimitry Andric } 12600b57cec5SDimitry Andric 1261cb14a3feSDimitry Andric template < size_t _Ip, 12620b57cec5SDimitry Andric class... _Args, 12630b57cec5SDimitry Andric enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 12640b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 12650b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1266*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) { 12675f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...); 12680b57cec5SDimitry Andric } 12690b57cec5SDimitry Andric 1270cb14a3feSDimitry Andric template < size_t _Ip, 12710b57cec5SDimitry Andric class _Up, 12720b57cec5SDimitry Andric class... _Args, 12730b57cec5SDimitry Andric enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 12740b57cec5SDimitry Andric class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 1275cb14a3feSDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0> 1276*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { 12775f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...); 12780b57cec5SDimitry Andric } 12790b57cec5SDimitry Andric 1280cb14a3feSDimitry Andric template < class _Tp, 12810b57cec5SDimitry Andric class... _Args, 1282cb14a3feSDimitry Andric size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 12830b57cec5SDimitry Andric enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1284*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) { 12855f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...); 12860b57cec5SDimitry Andric } 12870b57cec5SDimitry Andric 1288cb14a3feSDimitry Andric template < class _Tp, 12890b57cec5SDimitry Andric class _Up, 12900b57cec5SDimitry Andric class... _Args, 1291cb14a3feSDimitry Andric size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1292cb14a3feSDimitry Andric enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0> 1293*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { 12945f757f3fSDimitry Andric return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...); 12950b57cec5SDimitry Andric } 12960b57cec5SDimitry Andric 1297cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { 1298bdd1243dSDimitry Andric return __impl_.valueless_by_exception(); 12990b57cec5SDimitry Andric } 13000b57cec5SDimitry Andric 1301cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); } 13020b57cec5SDimitry Andric 1303cb14a3feSDimitry Andric template < bool _Dummy = true, 1304cb14a3feSDimitry Andric enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value && 13050b57cec5SDimitry Andric __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value, 13060b57cec5SDimitry Andric int> = 0> 1307*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(variant& __that) noexcept( 1308cb14a3feSDimitry Andric __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) { 1309bdd1243dSDimitry Andric __impl_.__swap(__that.__impl_); 13100b57cec5SDimitry Andric } 13110b57cec5SDimitry Andric 13127a6dacacSDimitry Andric# if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER) 13137a6dacacSDimitry Andric // Helper class to implement [variant.visit]/10 13147a6dacacSDimitry Andric // Constraints: The call to visit does not use an explicit template-argument-list 13157a6dacacSDimitry Andric // that begins with a type template-argument. 13167a6dacacSDimitry Andric struct __variant_visit_barrier_tag { 13177a6dacacSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __variant_visit_barrier_tag() = default; 13187a6dacacSDimitry Andric }; 13197a6dacacSDimitry Andric 13207a6dacacSDimitry Andric template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor> 13217a6dacacSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) { 13227a6dacacSDimitry Andric using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>; 13237a6dacacSDimitry Andric return std::visit(std::forward<_Visitor>(__visitor), (_VariantT)__self); 13247a6dacacSDimitry Andric } 13257a6dacacSDimitry Andric 13267a6dacacSDimitry Andric template <class _Rp, class _Self, class _Visitor> 13277a6dacacSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) { 13287a6dacacSDimitry Andric using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>; 13297a6dacacSDimitry Andric return std::visit<_Rp>(std::forward<_Visitor>(__visitor), (_VariantT)__self); 13307a6dacacSDimitry Andric } 13317a6dacacSDimitry Andric# endif 13327a6dacacSDimitry Andric 13330b57cec5SDimitry Andricprivate: 1334bdd1243dSDimitry Andric __variant_detail::__impl<_Types...> __impl_; 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andric friend struct __variant_detail::__access::__variant; 13370b57cec5SDimitry Andric friend struct __variant_detail::__visitation::__variant; 13380b57cec5SDimitry Andric}; 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1341cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept { 13420b57cec5SDimitry Andric return __v.index() == _Ip; 13430b57cec5SDimitry Andric} 13440b57cec5SDimitry Andric 13450b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1346cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept { 1347bdd1243dSDimitry Andric return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 13480b57cec5SDimitry Andric} 13490b57cec5SDimitry Andric 13500b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp> 1351cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr auto&& __generic_get(_Vp&& __v) { 13520b57cec5SDimitry Andric using __variant_detail::__access::__variant; 1353bdd1243dSDimitry Andric if (!std::__holds_alternative<_Ip>(__v)) { 13540b57cec5SDimitry Andric __throw_bad_variant_access(); 13550b57cec5SDimitry Andric } 13565f757f3fSDimitry Andric return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value; 13570b57cec5SDimitry Andric} 13580b57cec5SDimitry Andric 13590b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1360bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1361cb14a3feSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>& 1362cb14a3feSDimitry Andricget(variant<_Types...>& __v) { 13630b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 13640b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1365bdd1243dSDimitry Andric return std::__generic_get<_Ip>(__v); 13660b57cec5SDimitry Andric} 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1369bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1370cb14a3feSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&& 1371cb14a3feSDimitry Andricget(variant<_Types...>&& __v) { 13720b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 13730b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 13745f757f3fSDimitry Andric return std::__generic_get<_Ip>(std::move(__v)); 13750b57cec5SDimitry Andric} 13760b57cec5SDimitry Andric 13770b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1378bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1379cb14a3feSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>& 1380cb14a3feSDimitry Andricget(const variant<_Types...>& __v) { 13810b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 13820b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1383bdd1243dSDimitry Andric return std::__generic_get<_Ip>(__v); 13840b57cec5SDimitry Andric} 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1387bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI 1388cb14a3feSDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& 1389cb14a3feSDimitry Andricget(const variant<_Types...>&& __v) { 13900b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 13910b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 13925f757f3fSDimitry Andric return std::__generic_get<_Ip>(std::move(__v)); 13930b57cec5SDimitry Andric} 13940b57cec5SDimitry Andric 13950b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1396cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp& get(variant<_Types...>& __v) { 13970b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 13985f757f3fSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 13990b57cec5SDimitry Andric} 14000b57cec5SDimitry Andric 14010b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1402cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp&& get(variant<_Types...>&& __v) { 14030b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1404cb14a3feSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v)); 14050b57cec5SDimitry Andric} 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1408cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp& 1409cb14a3feSDimitry Andricget(const variant<_Types...>& __v) { 14100b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 14115f757f3fSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 14120b57cec5SDimitry Andric} 14130b57cec5SDimitry Andric 14140b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1415cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&& 1416cb14a3feSDimitry Andricget(const variant<_Types...>&& __v) { 14170b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 1418cb14a3feSDimitry Andric return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v)); 14190b57cec5SDimitry Andric} 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andrictemplate <size_t _Ip, class _Vp> 1422cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept { 14230b57cec5SDimitry Andric using __variant_detail::__access::__variant; 1424cb14a3feSDimitry Andric return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr; 14250b57cec5SDimitry Andric} 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1428cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>> 14290b57cec5SDimitry Andricget_if(variant<_Types...>* __v) noexcept { 14300b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 14310b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1432bdd1243dSDimitry Andric return std::__generic_get_if<_Ip>(__v); 14330b57cec5SDimitry Andric} 14340b57cec5SDimitry Andric 14350b57cec5SDimitry Andrictemplate <size_t _Ip, class... _Types> 1436cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>> 14370b57cec5SDimitry Andricget_if(const variant<_Types...>* __v) noexcept { 14380b57cec5SDimitry Andric static_assert(_Ip < sizeof...(_Types)); 14390b57cec5SDimitry Andric static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1440bdd1243dSDimitry Andric return std::__generic_get_if<_Ip>(__v); 14410b57cec5SDimitry Andric} 14420b57cec5SDimitry Andric 14430b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1444cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept { 14450b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 14465f757f3fSDimitry Andric return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 14470b57cec5SDimitry Andric} 14480b57cec5SDimitry Andric 14490b57cec5SDimitry Andrictemplate <class _Tp, class... _Types> 1450cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept { 14510b57cec5SDimitry Andric static_assert(!is_void_v<_Tp>); 14525f757f3fSDimitry Andric return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 14530b57cec5SDimitry Andric} 14540b57cec5SDimitry Andric 14550b57cec5SDimitry Andrictemplate <class _Operator> 14560b57cec5SDimitry Andricstruct __convert_to_bool { 14570b57cec5SDimitry Andric template <class _T1, class _T2> 1458cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const { 14595f757f3fSDimitry Andric static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value, 14600b57cec5SDimitry Andric "the relational operator does not return a type which is implicitly convertible to bool"); 14615f757f3fSDimitry Andric return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2)); 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric}; 14640b57cec5SDimitry Andric 14650b57cec5SDimitry Andrictemplate <class... _Types> 1466cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 14670b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 1468cb14a3feSDimitry Andric if (__lhs.index() != __rhs.index()) 1469cb14a3feSDimitry Andric return false; 1470cb14a3feSDimitry Andric if (__lhs.valueless_by_exception()) 1471cb14a3feSDimitry Andric return true; 14720b57cec5SDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs); 14730b57cec5SDimitry Andric} 14740b57cec5SDimitry Andric 147506c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 20 1476bdd1243dSDimitry Andric 1477cb14a3feSDimitry Andrictemplate <class... _Types> 1478cb14a3feSDimitry Andric requires(three_way_comparable<_Types> && ...) 1479bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...> 1480bdd1243dSDimitry Andricoperator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 1481bdd1243dSDimitry Andric using __variant_detail::__visitation::__variant; 1482bdd1243dSDimitry Andric using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>; 1483bdd1243dSDimitry Andric if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception()) 1484bdd1243dSDimitry Andric return strong_ordering::equal; 1485bdd1243dSDimitry Andric if (__lhs.valueless_by_exception()) 1486bdd1243dSDimitry Andric return strong_ordering::less; 1487bdd1243dSDimitry Andric if (__rhs.valueless_by_exception()) 1488bdd1243dSDimitry Andric return strong_ordering::greater; 1489bdd1243dSDimitry Andric if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0) 1490bdd1243dSDimitry Andric return __c; 1491bdd1243dSDimitry Andric auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; }; 1492bdd1243dSDimitry Andric return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs); 1493bdd1243dSDimitry Andric} 1494bdd1243dSDimitry Andric 149506c3fb27SDimitry Andric# endif // _LIBCPP_STD_VER >= 20 1496bdd1243dSDimitry Andric 14970b57cec5SDimitry Andrictemplate <class... _Types> 1498cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 14990b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 1500cb14a3feSDimitry Andric if (__lhs.index() != __rhs.index()) 1501cb14a3feSDimitry Andric return true; 1502cb14a3feSDimitry Andric if (__lhs.valueless_by_exception()) 1503cb14a3feSDimitry Andric return false; 1504cb14a3feSDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs); 15050b57cec5SDimitry Andric} 15060b57cec5SDimitry Andric 15070b57cec5SDimitry Andrictemplate <class... _Types> 1508cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 15090b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 1510cb14a3feSDimitry Andric if (__rhs.valueless_by_exception()) 1511cb14a3feSDimitry Andric return false; 1512cb14a3feSDimitry Andric if (__lhs.valueless_by_exception()) 1513cb14a3feSDimitry Andric return true; 1514cb14a3feSDimitry Andric if (__lhs.index() < __rhs.index()) 1515cb14a3feSDimitry Andric return true; 1516cb14a3feSDimitry Andric if (__lhs.index() > __rhs.index()) 1517cb14a3feSDimitry Andric return false; 15180b57cec5SDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs); 15190b57cec5SDimitry Andric} 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andrictemplate <class... _Types> 1522cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 15230b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 1524cb14a3feSDimitry Andric if (__lhs.valueless_by_exception()) 1525cb14a3feSDimitry Andric return false; 1526cb14a3feSDimitry Andric if (__rhs.valueless_by_exception()) 1527cb14a3feSDimitry Andric return true; 1528cb14a3feSDimitry Andric if (__lhs.index() > __rhs.index()) 1529cb14a3feSDimitry Andric return true; 1530cb14a3feSDimitry Andric if (__lhs.index() < __rhs.index()) 1531cb14a3feSDimitry Andric return false; 15320b57cec5SDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs); 15330b57cec5SDimitry Andric} 15340b57cec5SDimitry Andric 15350b57cec5SDimitry Andrictemplate <class... _Types> 1536cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 15370b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 1538cb14a3feSDimitry Andric if (__lhs.valueless_by_exception()) 1539cb14a3feSDimitry Andric return true; 1540cb14a3feSDimitry Andric if (__rhs.valueless_by_exception()) 1541cb14a3feSDimitry Andric return false; 1542cb14a3feSDimitry Andric if (__lhs.index() < __rhs.index()) 1543cb14a3feSDimitry Andric return true; 1544cb14a3feSDimitry Andric if (__lhs.index() > __rhs.index()) 1545cb14a3feSDimitry Andric return false; 1546cb14a3feSDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs); 15470b57cec5SDimitry Andric} 15480b57cec5SDimitry Andric 15490b57cec5SDimitry Andrictemplate <class... _Types> 1550cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { 15510b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 1552cb14a3feSDimitry Andric if (__rhs.valueless_by_exception()) 1553cb14a3feSDimitry Andric return true; 1554cb14a3feSDimitry Andric if (__lhs.valueless_by_exception()) 1555cb14a3feSDimitry Andric return false; 1556cb14a3feSDimitry Andric if (__lhs.index() > __rhs.index()) 1557cb14a3feSDimitry Andric return true; 1558cb14a3feSDimitry Andric if (__lhs.index() < __rhs.index()) 1559cb14a3feSDimitry Andric return false; 1560cb14a3feSDimitry Andric return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs); 15610b57cec5SDimitry Andric} 15620b57cec5SDimitry Andric 1563e8d8bef9SDimitry Andrictemplate <class... _Vs> 1564cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void __throw_if_valueless(_Vs&&... __vs) { 1565cb14a3feSDimitry Andric const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception()); 1566e8d8bef9SDimitry Andric if (__valueless) { 1567e8d8bef9SDimitry Andric __throw_bad_variant_access(); 1568e8d8bef9SDimitry Andric } 1569e8d8bef9SDimitry Andric} 1570e8d8bef9SDimitry Andric 15717a6dacacSDimitry Andrictemplate < class _Visitor, class... _Vs, typename> 1572cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto) 1573cb14a3feSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs) { 15740b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 15755f757f3fSDimitry Andric std::__throw_if_valueless(std::forward<_Vs>(__vs)...); 1576cb14a3feSDimitry Andric return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...); 15770b57cec5SDimitry Andric} 15780b57cec5SDimitry Andric 157906c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 20 15807a6dacacSDimitry Andrictemplate < class _Rp, class _Visitor, class... _Vs, typename> 1581cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp 1582cb14a3feSDimitry Andricvisit(_Visitor&& __visitor, _Vs&&... __vs) { 1583e8d8bef9SDimitry Andric using __variant_detail::__visitation::__variant; 15845f757f3fSDimitry Andric std::__throw_if_valueless(std::forward<_Vs>(__vs)...); 1585cb14a3feSDimitry Andric return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...); 1586e8d8bef9SDimitry Andric} 1587e8d8bef9SDimitry Andric# endif 1588e8d8bef9SDimitry Andric 15890b57cec5SDimitry Andrictemplate <class... _Types> 1590*0fca6ea1SDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto 1591*0fca6ea1SDimitry Andricswap(variant<_Types...>& __lhs, 1592*0fca6ea1SDimitry Andric variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs))) -> decltype(__lhs.swap(__rhs)) { 1593cb14a3feSDimitry Andric return __lhs.swap(__rhs); 1594cb14a3feSDimitry Andric} 15950b57cec5SDimitry Andric 15960b57cec5SDimitry Andrictemplate <class... _Types> 1597cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> { 15980b57cec5SDimitry Andric using argument_type = variant<_Types...>; 15990b57cec5SDimitry Andric using result_type = size_t; 16000b57cec5SDimitry Andric 1601cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const { 16020b57cec5SDimitry Andric using __variant_detail::__visitation::__variant; 16030b57cec5SDimitry Andric size_t __res = 16040b57cec5SDimitry Andric __v.valueless_by_exception() 16050b57cec5SDimitry Andric ? 299792458 // Random value chosen by the universe upon creation 16060b57cec5SDimitry Andric : __variant::__visit_alt( 16070b57cec5SDimitry Andric [](const auto& __alt) { 1608bdd1243dSDimitry Andric using __alt_type = __remove_cvref_t<decltype(__alt)>; 1609cb14a3feSDimitry Andric using __value_type = remove_const_t< typename __alt_type::__value_type>; 16100b57cec5SDimitry Andric return hash<__value_type>{}(__alt.__value); 16110b57cec5SDimitry Andric }, 16120b57cec5SDimitry Andric __v); 1613bdd1243dSDimitry Andric return std::__hash_combine(__res, hash<size_t>{}(__v.index())); 16140b57cec5SDimitry Andric } 16150b57cec5SDimitry Andric}; 16160b57cec5SDimitry Andric 1617fe6060f1SDimitry Andric// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong 1618fe6060f1SDimitry Andric// type whereas std::get will throw or returning nullptr. This makes it faster than 1619fe6060f1SDimitry Andric// std::get. 1620fe6060f1SDimitry Andrictemplate <size_t _Ip, class _Vp> 1621cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept { 1622fe6060f1SDimitry Andric using __variant_detail::__access::__variant; 16235f757f3fSDimitry Andric return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value; 16240b57cec5SDimitry Andric} 1625fe6060f1SDimitry Andric 1626fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types> 1627cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept { 1628bdd1243dSDimitry Andric return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1629fe6060f1SDimitry Andric} 1630fe6060f1SDimitry Andric 1631fe6060f1SDimitry Andrictemplate <class _Tp, class... _Types> 1632cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept { 1633bdd1243dSDimitry Andric return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1634fe6060f1SDimitry Andric} 16350b57cec5SDimitry Andric 163606c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 17 16370b57cec5SDimitry Andric 16380b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 16390b57cec5SDimitry Andric 16400b57cec5SDimitry Andric_LIBCPP_POP_MACROS 16410b57cec5SDimitry Andric 1642bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 164306c3fb27SDimitry Andric# include <exception> 1644*0fca6ea1SDimitry Andric# include <tuple> 1645bdd1243dSDimitry Andric# include <type_traits> 1646bdd1243dSDimitry Andric# include <typeinfo> 1647bdd1243dSDimitry Andric# include <utility> 1648bdd1243dSDimitry Andric#endif 1649bdd1243dSDimitry Andric 16500b57cec5SDimitry Andric#endif // _LIBCPP_VARIANT 1651