Index: include/variant =================================================================== --- /dev/null +++ include/variant @@ -0,0 +1,1235 @@ +// -*- C++ -*- +//===------------------------------ variant -------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_VARIANT +#define _LIBCPP_VARIANT + +/* + variant synopsis + +namespace std { + + // 20.7.2, class template variant + template + class variant { + public: + + // 20.7.2.1, constructors + constexpr variant() noexcept(see below); + variant(const variant&); + variant(variant&&) noexcept(see below); + + template constexpr variant(T&&) noexcept(see below); + + template + constexpr explicit variant(in_place_type_t, Args&&...); + + template + constexpr explicit variant( + in_place_type_t, initializer_list, Args&&...); + + template + constexpr explicit variant(in_place_index_t, Args&&...); + + template + constexpr explicit variant( + in_place_index_t, initializer_list, Args&&...); + + // 20.7.2.2, destructor + ~variant(); + + // 20.7.2.3, assignment + variant& operator=(const variant&); + variant& operator=(variant&&) noexcept(see below); + + template variant& operator=(T&&) noexcept(see below); + + // 20.7.2.4, modifiers + template + void emplace(Args&&...); + + template + void emplace(initializer_list, Args&&...); + + template + void emplace(Args&&...); + + template + void emplace(initializer_list, Args&&...); + + // 20.7.2.5, value status + constexpr bool valueless_by_exception() const noexcept; + constexpr size_t index() const noexcept; + + // 20.7.2.6, swap + void swap(variant&) noexcept(see below); + }; + + // 20.7.3, variant helper classes + template struct variant_size; // undefined + + template + constexpr size_t variant_size_v = variant_size::value; + + template struct variant_size; + template struct variant_size; + template struct variant_size; + + template + struct variant_size>; + + template struct variant_alternative; // undefined + + template + using variant_alternative_t = typename variant_alternative::type; + + template struct variant_alternative; + template struct variant_alternative; + template struct variant_alternative; + + template + struct variant_alternative>; + + constexpr size_t variant_npos = -1; + + // 20.7.4, value access + template + constexpr bool holds_alternative(const variant&) noexcept; + + template + constexpr variant_alternative_t>& + get(variant&); + + template + constexpr variant_alternative_t>&& + get(variant&&); + + template + constexpr variant_alternative_t> const& + get(const variant&); + + template + constexpr variant_alternative_t> const&& + get(const variant&&); + + template + constexpr T& get(variant&); + + template + constexpr T&& get(variant&&); + + template + constexpr const T& get(const variant&); + + template + constexpr const T&& get(const variant&&); + + template + constexpr add_pointer_t>> + get_if(variant*) noexcept; + + template + constexpr add_pointer_t>> + get_if(const variant*) noexcept; + + template + constexpr add_pointer_t + get_if(variant*) noexcept; + + template + constexpr add_pointer_t + get_if(const variant*) noexcept; + + // 20.7.5, relational operators + template + constexpr bool operator==(const variant&, const variant&); + + template + constexpr bool operator!=(const variant&, const variant&); + + template + constexpr bool operator<(const variant&, const variant&); + + template + constexpr bool operator>(const variant&, const variant&); + + template + constexpr bool operator<=(const variant&, const variant&); + + template + constexpr bool operator>=(const variant&, const variant&); + + // 20.7.6, visitation + template + constexpr see below visit(Visitor&&, Variants&&...); + + // 20.7.7, class monostate + struct monostate; + + // 20.7.8, monostate relational operators + constexpr bool operator<(monostate, monostate) noexcept; + constexpr bool operator>(monostate, monostate) noexcept; + constexpr bool operator<=(monostate, monostate) noexcept; + constexpr bool operator>=(monostate, monostate) noexcept; + constexpr bool operator==(monostate, monostate) noexcept; + constexpr bool operator!=(monostate, monostate) noexcept; + + // 20.7.9, specialized algorithms + template + void swap(variant&, variant&) noexcept(see below); + + // 20.7.10, class bad_variant_access + class bad_variant_access; + + // 20.7.11, hash support + template struct hash; + template struct hash>; + template <> struct hash; + +} // namespace std + +*/ + +#include <__config> +#include <__tuple> +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +namespace std { // explicitly not using versioning namespace + +class _LIBCPP_EXCEPTION_ABI bad_variant_access : public exception { +public: + virtual const char* what() const noexcept; +}; + +} // namespace std + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 14 + +template +class _LIBCPP_TYPE_VIS variant; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_size; + +template +constexpr size_t variant_size_v = variant_size<_Tp>::value; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_size + : variant_size<_Tp> {}; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_size + : variant_size<_Tp> {}; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_size + : variant_size<_Tp> {}; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_size> + : integral_constant {}; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_alternative; + +template +using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_alternative<_Ip, const _Tp> + : add_const> {}; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_alternative<_Ip, volatile _Tp> + : add_volatile> {}; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_alternative<_Ip, const volatile _Tp> + : add_cv> {}; + +template +struct _LIBCPP_TYPE_VIS_ONLY variant_alternative<_Ip, variant<_Types...>> { + static_assert(_Ip < sizeof...(_Types)); + using type = __type_pack_element<_Ip, _Types...>; +}; + +constexpr size_t variant_npos = static_cast(-1); + +struct __variant_valueless_t {}; +constexpr __variant_valueless_t __variant_valueless = __variant_valueless_t{}; + +namespace __find_detail { + + template + constexpr size_t __find_index() { + constexpr bool __matches[] = {is_same_v<_Tp, _Types>...}; + size_t __result = __not_found; + for (size_t __i = 0; __i < sizeof...(_Types); ++__i) { + if (__matches[__i]) { + if (__result != __not_found) { + return __ambiguous; + } + __result = __i; + } + } + return __result; + } + + template + struct __find_unambiguous_index_sfinae_impl + : integral_constant {}; + + template <> + struct __find_unambiguous_index_sfinae_impl<__not_found> {}; + + template <> + struct __find_unambiguous_index_sfinae_impl<__ambiguous> {}; + + template + struct __find_unambiguous_index_sfinae + : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {}; + +} // namespace __find_detail + +using __find_detail::__find_unambiguous_index_sfinae; + +template +class __variant_overload; + +template <> +class __variant_overload<> { +public: + void operator()() const; +}; + +template +class __variant_overload<_Tp, _Types...> + : public __variant_overload<_Types...> { +public: + using __variant_overload<_Types...>::operator(); + __identity<_Tp> operator()(_Tp) const; +}; + +template +using __variant_best_match_t = + typename result_of_t<__variant_overload<_Types...>(_Tp&&)>::type; + +struct __variant_union_access { + template + static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) { + return _VSTD::forward<_Vp>(__v).__head; + } + + template + static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) { + return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>); + } +}; + +struct __variant_impl_access { + template + static constexpr auto&& __get_alt(_Vp&& __v) { + return __variant_union_access::__get_alt(_VSTD::forward<_Vp>(__v).__data, + in_place_index<_Ip>); + } +}; + +struct __variant_access { + template + static constexpr auto&& __get_alt(_Vp&& __v) { + return __variant_impl_access::__get_alt<_Ip>( + _VSTD::forward<_Vp>(__v).__impl); + } +}; + +namespace __variant_impl_visitation_detail { + + template + constexpr const _Tp& __at_impl(const _Tp& __elem, const size_t*) { + return __elem; + } + + template + constexpr auto&& __at_impl(const array<_Tp, _Np>& __elems, + const size_t* __index) { + return __at_impl(__elems[*__index], __index + 1); + } + + template + constexpr auto&& __at(const array<_Tp, _Np>& __elems, + const size_t (&__indices)[_Ip]) { + return __at_impl(__elems, begin(__indices)); + } + + template + struct __all_same : __all...> {}; + + template + constexpr void __variant_visit_has_same_return_types(true_type) {} + + template + constexpr void __variant_visit_has_same_return_types(false_type) = delete; + + template + constexpr array...>, sizeof...(_Args)> + __make_array(_Args&&... __args) { + using __all_same = __all_same...>; + __variant_visit_has_same_return_types...>(__all_same{}); + return {{_VSTD::forward<_Args>(__args)...}}; + } + + template + constexpr auto __make_dispatch(index_sequence<_Is...>) { + struct __dispatcher { + static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) { + return __invoke_constexpr( + static_cast<_Fp>(__f), + __variant_impl_access::__get_alt<_Is>(static_cast<_Vs>(__vs))...); + } + }; + return _VSTD::addressof(__dispatcher::__dispatch); + } + + template + constexpr auto __make_fdiagonal_impl() { + return __make_dispatch<_Fp, _Vs...>( + index_sequence<(__identity<_Vs>{}, _Ip)...>{}); + } + + template + constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) { + return __make_array(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...); + } + + template + constexpr auto __make_fdiagonal() { + constexpr size_t _Np = decay_t<_Vp>::__size(); + static_assert(__all<(_Np == decay_t<_Vs>::__size())...>::value); + return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{}); + } + + template + constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) { + return __make_dispatch<_Fp, _Vs...>(__is); + } + + template + constexpr auto __make_fmatrix_impl(index_sequence<_Is...>, + index_sequence<_Js...>, + _Ls... __ls) { + return __make_array(__make_fmatrix_impl<_Fp, _Vs...>( + index_sequence<_Is..., _Js>{}, __ls...)...); + } + + template + constexpr auto __make_fmatrix() { + return __make_fmatrix_impl<_Fp, _Vs...>( + index_sequence<>{}, make_index_sequence::__size()>{}...); + } + +} // namespace __variant_impl_visitation_detail + +struct __variant_impl_visitation { + template + static constexpr decltype(auto) __visit_alt_at(size_t __index, + _Visitor&& __visitor, + _Vs&&... __vs) { + using namespace __variant_impl_visitation_detail; + constexpr auto __fdiagonal = __make_fdiagonal< + _Visitor&&, + decltype(_VSTD::forward<_Vs>(__vs).__as_variant_base())...>(); + return __fdiagonal[__index]( + _VSTD::forward<_Visitor>(__visitor), + _VSTD::forward<_Vs>(__vs).__as_variant_base()...); + } + + template + static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, + _Vs&&... __vs) { + using namespace __variant_impl_visitation_detail; + constexpr auto __fmatrix = __make_fmatrix< + _Visitor&&, + decltype(_VSTD::forward<_Vs>(__vs).__as_variant_base())...>(); + const size_t __indices[] = {__vs.index()...}; + return __at(__fmatrix, __indices)( + _VSTD::forward<_Visitor>(__visitor), + _VSTD::forward<_Vs>(__vs).__as_variant_base()...); + } +}; + +struct __variant_visitation { + template + static constexpr decltype(auto) __visit_alt_at(size_t __index, + _Visitor&& __visitor, + _Vs&&... __vs) { + return __variant_impl_visitation::__visit_alt_at( + __index, + _VSTD::forward<_Visitor>(__visitor), + _VSTD::forward<_Vs>(__vs).__impl...); + } + + template + static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, + _Vs&&... __vs) { + return __variant_impl_visitation::__visit_alt( + _VSTD::forward<_Visitor>(__visitor), + _VSTD::forward<_Vs>(__vs).__impl...); + } + + template + struct __value_visitor { + template + constexpr decltype(auto) operator()(_Alts&&... __alts) const { + return __invoke_constexpr(static_cast<_Visitor>(__visitor), + _VSTD::forward<_Alts>(__alts).__value...); + } + + _Visitor __visitor; + }; + + template + static constexpr auto __make_value_visitor(_Visitor&& __visitor) { + return __value_visitor<_Visitor&&>{_VSTD::forward<_Visitor>(__visitor)}; + } + + template + static constexpr decltype(auto) __visit_value_at(size_t __index, + _Visitor&& __visitor, + _Vs&&... __vs) { + return __visit_alt_at( + __index, + __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)), + _VSTD::forward<_Vs>(__vs)...); + } + + template + static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, + _Vs&&... __vs) { + return __visit_alt( + __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)), + _VSTD::forward<_Vs>(__vs)...); + } +}; + +template +struct _LIBCPP_TYPE_VIS_ONLY __variant_alt { + using __value_type = _Tp; + + __variant_alt() = default; + __variant_alt(const __variant_alt&) = delete; + __variant_alt(__variant_alt&&) = delete; + __variant_alt& operator=(const __variant_alt&) = delete; + __variant_alt& operator=(__variant_alt&&) = delete; + + template + explicit constexpr __variant_alt(_Args&&... __args) + : __value(_VSTD::forward<_Args>(__args)...) {} + + __value_type __value; +}; + +template +union __variant_union; + +template +union __variant_union<_TriviallyDestructible, _Index> {}; + +#define _LIBCPP_VARIANT_UNION(__trivially_destructible, dtor) \ + template \ + union __variant_union<__trivially_destructible, _Index, _Tp, _Types...> { \ + public: \ + explicit constexpr __variant_union(__variant_valueless_t) noexcept \ + : __dummy{} {} \ + \ + template \ + explicit constexpr __variant_union(in_place_index_t<0>, \ + _Args&&... __args) \ + : __head(_VSTD::forward<_Args>(__args)...) {} \ + \ + template \ + explicit constexpr __variant_union(in_place_index_t<_Ip>, \ + _Args&&... __args) \ + : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \ + \ + dtor \ + \ + private: \ + char __dummy; \ + __variant_alt<_Index, _Tp> __head; \ + __variant_union<__trivially_destructible, _Index + 1, _Types...> __tail; \ + \ + friend struct __variant_union_access; \ + } + +_LIBCPP_VARIANT_UNION(true , ~__variant_union() = default;); +_LIBCPP_VARIANT_UNION(false, ~__variant_union() {}); + +#undef _LIBCPP_VARIANT_UNION + +template +class __variant_base { +public: + static constexpr size_t __size() { return sizeof...(_Types); } + + explicit constexpr __variant_base(__variant_valueless_t) noexcept + : __index(variant_npos), __data(__variant_valueless) {} + + template + explicit constexpr __variant_base(in_place_index_t<_Ip>, _Args&&... __args) + : __index(_Ip), __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {} + + constexpr bool valueless_by_exception() const noexcept { + return index() == variant_npos; + } + + constexpr size_t index() const noexcept { return __index; } + +protected: + constexpr auto&& __as_variant_base() & { return *this; } + constexpr auto&& __as_variant_base() && { return _VSTD::move(*this); } + constexpr auto&& __as_variant_base() const & { return *this; } + constexpr auto&& __as_variant_base() const && { return _VSTD::move(*this); } + + size_t __index; + __variant_union<__trivially_destructible, 0, _Types...> __data; + + friend struct __variant_impl_access; + friend struct __variant_impl_visitation; +}; + +template +class __variant_dtor; + +template +class __variant_dtor + : public __variant_base { + using __base_type = __variant_base; + +public: + using __base_type::__base_type; + ~__variant_dtor() = default; + +protected: + void __destroy() noexcept { this->__index = variant_npos; } +}; + +template +class __variant_dtor + : public __variant_base { + using __base_type = __variant_base; + +public: + using __base_type::__base_type; + ~__variant_dtor() { __destroy(); } + +protected: + void __destroy() noexcept { + if (!this->valueless_by_exception()) { + __variant_impl_visitation::__visit_alt( + [](auto& __alt) noexcept { + using __alt_type = decay_t; + __alt.~__alt_type(); + }, + *this); + } + this->__index = variant_npos; + } +}; + +template +struct __variant_impl + : public __variant_dtor< + __all...>::value, _Types...> { + using __base_type = + __variant_dtor<__all...>::value, + _Types...>; + +public: + using __base_type::__base_type; + + __variant_impl( + const conditional_t<__all...>::value, + __variant_impl, + __nat>& __that) + : __variant_impl(__variant_valueless) { + static_assert(!is_same_v, __nat>); + __generic_construct(*this, __that); + } + + __variant_impl( + conditional_t<__all...>::value, + __variant_impl, + __nat>&& + __that) + noexcept(__all...>::value) + : __variant_impl(__variant_valueless) { + static_assert(!is_same_v, __nat>); + __generic_construct(*this, _VSTD::move(__that)); + } + + __variant_impl& operator=( + const conditional_t<__all<(is_copy_constructible_v<_Types> && + is_move_constructible_v<_Types> && + is_copy_assignable_v<_Types>)...>::value, + __variant_impl, + __nat>& __that) { + static_assert(!is_same_v, __nat>); + return __generic_assign(__that); + } + + __variant_impl& operator=( + conditional_t<__all<(is_move_constructible_v<_Types> && + is_move_assignable_v<_Types>)...>::value, + __variant_impl, + __nat>&& + __that) noexcept(__all<(is_nothrow_move_constructible_v<_Types> && + is_nothrow_move_assignable_v<_Types>)...>::value) { + static_assert(!is_same_v, __nat>); + return __generic_assign(_VSTD::move(__that)); + } + + template + void __assign(_Arg&& __arg) { + __assign_alt(__variant_impl_access::__get_alt<_Ip>(*this), + _VSTD::forward<_Arg>(__arg), + false_type{}); + } + + template + void __emplace(_Args&&... __args) { + this->__destroy(); + __construct_alt(__variant_impl_access::__get_alt<_Ip>(*this), + _VSTD::forward<_Args>(__args)...); + this->__index = _Ip; + } + + void __swap(__variant_impl& __that) { + if (this->valueless_by_exception() && __that.valueless_by_exception()) { + // do nothing. + } else if (this->index() == __that.index()) { + __variant_impl_visitation::__visit_alt_at( + this->index(), + [](auto& __this_alt, auto& __that_alt) { + using _VSTD::swap; + swap(__this_alt.__value, __that_alt.__value); + }, + *this, __that); + } else { + __variant_impl* __lhs = this; + __variant_impl* __rhs = _VSTD::addressof(__that); + if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) { + _VSTD::swap(__lhs, __rhs); + } + __variant_impl __tmp(_VSTD::move(*__rhs)); +#ifndef _LIBCPP_NO_EXCEPTIONS + // EXTENSION: When the move construction of `__lhs` into `__rhs` throws + // and `__tmp` is nothrow move constructible then we move `__tmp` back + // into `__rhs` and provide the strong exception safety guarentee. + try { + __generic_construct(*__rhs, _VSTD::move(*__lhs)); + } catch (...) { + if (__tmp.__move_nothrow()) { + __generic_construct(*__rhs, _VSTD::move(__tmp)); + } + throw; + } +#else + __generic_construct(*__rhs, _VSTD::move(*__lhs)); +#endif + __generic_construct(*__lhs, _VSTD::move(__tmp)); + } + } + +private: + bool __move_nothrow() const { + constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...}; + return this->valueless_by_exception() || __results[this->index()]; + } + + template + void __assign_alt(__variant_alt<_Ip, _Tp>& __alt, + _Arg&& __arg, + bool_constant<_CopyAssign> __tag) { + if (this->index() == _Ip) { + __alt.__value = _VSTD::forward<_Arg>(__arg); + } else { + struct { + void operator()(true_type) const { + __this->__emplace<_Ip>(_Tp(static_cast<_Arg&&>(__arg))); + } + void operator()(false_type) const { + __this->__emplace<_Ip>(static_cast<_Arg&&>(__arg)); + } + __variant_impl* __this; + _Arg&& __arg; + } __impl{this, _VSTD::forward<_Arg>(__arg)}; + __impl(__tag); + } + } + + template + __variant_impl& __generic_assign(_That&& __that) { + static_assert(is_same_v, __variant_impl>); + if (this->valueless_by_exception() && __that.valueless_by_exception()) { + // do nothing. + } else if (__that.valueless_by_exception()) { + this->__destroy(); + } else { + __variant_impl_visitation::__visit_alt_at( + __that.index(), + [this](auto& __this_alt, auto&& __that_alt) { + __assign_alt( + __this_alt, + _VSTD::forward(__that_alt).__value, + is_lvalue_reference<_That&&>{}); + }, + *this, _VSTD::forward<_That>(__that)); + } + return *this; + } + + template + static void __construct_alt(__variant_alt<_Ip, _Tp>& __alt, + _Args&&... __args) { + ::new (_VSTD::addressof(__alt)) + __variant_alt<_Ip, _Tp>(_VSTD::forward<_Args>(__args)...); + } + + template + static void __generic_construct(__variant_impl& __lhs, _Rhs&& __rhs) { + static_assert(is_same_v, __variant_impl>); + __lhs.__destroy(); + if (!__rhs.valueless_by_exception()) { + __variant_impl_visitation::__visit_alt_at( + __rhs.index(), + [](auto& __lhs_alt, auto&& __rhs_alt) { + __construct_alt( + __lhs_alt, + _VSTD::forward(__rhs_alt).__value); + }, + __lhs, _VSTD::forward<_Rhs>(__rhs)); + __lhs.__index = __rhs.index(); + } + } +}; + +template +class variant { + static_assert(sizeof...(_Types) > 0, + "variant must consist of at least one alternative."); + + static_assert(__all...>::value, + "variant can not have an array type as an alternative."); + + static_assert(__all...>::value, + "variant can not have a reference type as an alternative."); + + static_assert(__all...>::value, + "variant can not have a void type as an alternative."); + +public: + template = 0, + class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, + enable_if_t, int> = 0> + constexpr variant() noexcept(is_nothrow_default_constructible_v<_Tp>) + : __impl(in_place_index<0>) {} + + variant(const variant&) = default; + variant(variant&&) = default; + + template , variant>, int> = 0, + class _Tp = __variant_best_match_t<_Arg&&, _Types...>, + size_t _Ip = __find_unambiguous_index_sfinae<_Tp, _Types...>::value, + enable_if_t, int> = 0> + constexpr variant(_Arg&&__arg) noexcept( + is_nothrow_constructible_v<_Tp, _Arg&&>) + : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {} + + template = 0, + class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, + enable_if_t, int> = 0> + explicit constexpr variant( + in_place_index_t<_Ip>, + _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args&&...>) + : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {} + + template < + size_t _Ip, + class _Up, + class... _Args, + enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, + class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, + enable_if_t&, _Args&&...>, + int> = 0> + explicit constexpr variant( + in_place_index_t<_Ip>, + initializer_list<_Up> __il, + _Args&&... __args) noexcept( + is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args&&...>) + : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {} + + template ::value, + enable_if_t, int> = 0> + explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept( + is_nothrow_constructible_v<_Tp, _Args&&...>) + : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {} + + template < + class _Tp, + class _Up, + class... _Args, + size_t _Ip = __find_unambiguous_index_sfinae<_Tp, _Types...>::value, + enable_if_t&, _Args&&...>, + int> = 0> + explicit constexpr variant( + in_place_type_t<_Tp>, + initializer_list<_Up> __il, + _Args&&... __args) noexcept( + is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args&&...>) + : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {} + + ~variant() = default; + + variant& operator=(const variant&) = default; + variant& operator=(variant&&) = default; + + template , variant>, int> = 0, + class _Tp = __variant_best_match_t<_Arg&&, _Types...>, + size_t _Ip = __find_unambiguous_index_sfinae<_Tp, _Types...>::value, + enable_if_t<(is_assignable_v<_Tp&, _Arg&&> && + is_constructible_v<_Tp, _Arg&&>), int> = 0> + variant& operator=(_Arg&& __arg) noexcept( + (is_nothrow_assignable_v<_Tp&, _Arg&&> && + is_nothrow_constructible_v<_Tp, _Arg&&>)) { + __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg)); + return *this; + } + + template < + size_t _Ip, + class... _Args, + enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, + class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, + enable_if_t, int> = 0> + void emplace(_Args&&... __args) { + __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...); + } + + template < + size_t _Ip, + class _Up, + class... _Args, + enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, + class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, + enable_if_t&, _Args&&...>, + int> = 0> + void emplace(initializer_list<_Up> __il, _Args&&... __args) { + __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...); + } + + template < + class _Tp, + class... _Args, + size_t _Ip = __find_unambiguous_index_sfinae<_Tp, _Types...>::value, + enable_if_t, int> = 0> + void emplace(_Args&&... __args) { + __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...); + } + + template < + class _Tp, + class _Up, + class... _Args, + size_t _Ip = __find_unambiguous_index_sfinae<_Tp, _Types...>::value, + enable_if_t&, _Args&&...>, + int> = 0> + void emplace(initializer_list<_Up> __il, _Args&&... __args) { + __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...); + } + + constexpr bool valueless_by_exception() const noexcept { + return __impl.valueless_by_exception(); + } + + constexpr size_t index() const noexcept { return __impl.index(); } + + template < + bool _Dummy = false, + enable_if_t< + __all<( + __dependent_type, _Dummy>::value && + __dependent_type, _Dummy>::value)...>::value, + int> = 0> + void swap(variant& __that) noexcept( + __all<(is_nothrow_move_constructible_v<_Types> && + is_nothrow_swappable_v<_Types>)...>::value) { + __impl.__swap(__that.__impl); + } + +private: + __variant_impl<_Types...> __impl; + + friend struct __variant_access; + friend struct __variant_visitation; +}; + +template +constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept { + return __v.index() == _Ip; +} + +template +constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept { + return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v); +} + +template +static constexpr auto&& __generic_get(_Vp&& __v) { + return __holds_alternative<_Ip>(__v) + ? __variant_access::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)) + .__value + : throw bad_variant_access{}; +} + +template +constexpr variant_alternative_t<_Ip, variant<_Types...>>& get( + variant<_Types...>& __v) { + static_assert(_Ip < sizeof...(_Types)); + static_assert(!is_void_v>>); + return __generic_get<_Ip>(__v); +} + +template +constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get( + variant<_Types...>&& __v) { + static_assert(_Ip < sizeof...(_Types)); + static_assert(!is_void_v>>); + return __generic_get<_Ip>(_VSTD::move(__v)); +} + +template +constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get( + const variant<_Types...>& __v) { + static_assert(_Ip < sizeof...(_Types)); + static_assert(!is_void_v>>); + return __generic_get<_Ip>(__v); +} + +template +constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get( + const variant<_Types...>&& __v) { + static_assert(_Ip < sizeof...(_Types)); + static_assert(!is_void_v>>); + return __generic_get<_Ip>(_VSTD::move(__v)); +} + +template +constexpr _Tp& get(variant<_Types...>& __v) { + static_assert(!is_void_v<_Tp>); + return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); +} + +template +constexpr _Tp&& get(variant<_Types...>&& __v) { + static_assert(!is_void_v<_Tp>); + return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>( + _VSTD::move(__v)); +} + +template +constexpr const _Tp& get(const variant<_Types...>& __v) { + static_assert(!is_void_v<_Tp>); + return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); +} + +template +constexpr const _Tp&& get(const variant<_Types...>&& __v) { + static_assert(!is_void_v<_Tp>); + return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>( + _VSTD::move(__v)); +} + +template +constexpr auto* __generic_get_if(_Vp* __v) noexcept { + return __v && __holds_alternative<_Ip>(*__v) + ? _VSTD::addressof(__variant_access::__get_alt<_Ip>(*__v).__value) + : nullptr; +} + +template +constexpr add_pointer_t>> +get_if(variant<_Types...>* __v) noexcept { + static_assert(_Ip < sizeof...(_Types)); + static_assert(!is_void_v>>); + return __generic_get_if<_Ip>(__v); +} + +template +constexpr add_pointer_t>> +get_if(const variant<_Types...>* __v) noexcept { + static_assert(_Ip < sizeof...(_Types)); + static_assert(!is_void_v>>); + return __generic_get_if<_Ip>(__v); +} + +template +constexpr add_pointer_t<_Tp> +get_if(variant<_Types...>* __v) noexcept { + static_assert(!is_void_v<_Tp>); + return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); +} + +template +constexpr add_pointer_t +get_if(const variant<_Types...>* __v) noexcept { + static_assert(!is_void_v<_Tp>); + return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); +} + +template +constexpr bool operator==(const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) { + if (__lhs.index() != __rhs.index()) return false; + if (__lhs.valueless_by_exception()) return true; + return __variant_visitation::__visit_value_at( + __lhs.index(), equal_to<>{}, __lhs, __rhs); +} + +template +constexpr bool operator!=(const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) { + if (__lhs.index() != __rhs.index()) return true; + if (__lhs.valueless_by_exception()) return false; + return __variant_visitation::__visit_value_at( + __lhs.index(), not_equal_to<>{}, __lhs, __rhs); +} + +template +constexpr bool operator<(const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) { + if (__rhs.valueless_by_exception()) return false; + if (__lhs.valueless_by_exception()) return true; + if (__lhs.index() < __rhs.index()) return true; + if (__lhs.index() > __rhs.index()) return false; + return __variant_visitation::__visit_value_at( + __lhs.index(), less<>{}, __lhs, __rhs); +} + +template +constexpr bool operator>(const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) { + if (__lhs.valueless_by_exception()) return false; + if (__rhs.valueless_by_exception()) return true; + if (__lhs.index() > __rhs.index()) return true; + if (__lhs.index() < __rhs.index()) return false; + return __variant_visitation::__visit_value_at( + __lhs.index(), greater<>{}, __lhs, __rhs); +} + +template +constexpr bool operator<=(const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) { + if (__lhs.valueless_by_exception()) return true; + if (__rhs.valueless_by_exception()) return false; + if (__lhs.index() < __rhs.index()) return true; + if (__lhs.index() > __rhs.index()) return false; + return __variant_visitation::__visit_value_at( + __lhs.index(), less_equal<>{}, __lhs, __rhs); +} + +template +constexpr bool operator>=(const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) { + if (__rhs.valueless_by_exception()) return true; + if (__lhs.valueless_by_exception()) return false; + if (__lhs.index() > __rhs.index()) return true; + if (__lhs.index() < __rhs.index()) return false; + return __variant_visitation::__visit_value_at( + __lhs.index(), greater_equal<>{}, __lhs, __rhs); +} + +template +constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) { + bool __results[] = {__vs.valueless_by_exception()...}; + for (bool __result : __results) { + if (__result) { + throw bad_variant_access{}; + } + } + return __variant_visitation::__visit_value( + _VSTD::forward<_Visitor>(__visitor), _VSTD::forward<_Vs>(__vs)...); +} + +struct _LIBCPP_TYPE_VIS_ONLY monostate {}; + +constexpr bool operator<(monostate, monostate) noexcept { return false; } +constexpr bool operator>(monostate, monostate) noexcept { return false; } +constexpr bool operator<=(monostate, monostate) noexcept { return true; } +constexpr bool operator>=(monostate, monostate) noexcept { return true; } +constexpr bool operator==(monostate, monostate) noexcept { return true; } +constexpr bool operator!=(monostate, monostate) noexcept { return false; } + +template +void swap(variant<_Types...> &__lhs, + variant<_Types...> &__rhs) noexcept(noexcept(__lhs.swap(__rhs))) { + __lhs.swap(__rhs); +} + +template +struct _LIBCPP_TYPE_VIS_ONLY hash> { + using argument_type = variant<_Types...>; + using result_type = size_t; + + result_type operator()(const argument_type& __v) const { + return __v.valueless_by_exception() + ? variant_npos + : __variant_visitation::__visit_alt( + [](const auto& __alt) { + using __alt_type = decay_t; + using __value_type = typename __alt_type::__value_type; + return hash<__value_type>{}(__alt.__value); + }, + __v); + } +}; + +template <> +struct _LIBCPP_TYPE_VIS_ONLY hash { + using argument_type = monostate; + using result_type = size_t; + + result_type operator()(const argument_type&) const { return 0; } +}; + +#endif // _LIBCPP_STD_VER > 14 + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_VARIANT Index: src/variant.cpp =================================================================== --- /dev/null +++ src/variant.cpp @@ -0,0 +1,18 @@ +//===------------------------ variant.cpp ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "variant" + +namespace std { + +const char* bad_variant_access::what() const noexcept { + return "bad_variant_access"; +} + +} // namespace std