#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED #define BOOST_VARIANT2_VARIANT_HPP_INCLUDED // Copyright 2017-2019 Peter Dimov. // // Distributed under the Boost Software License, Version 1.0. // // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt #if defined(_MSC_VER) && _MSC_VER < 1910 # pragma warning( push ) # pragma warning( disable: 4521 4522 ) // multiple copy operators #endif #ifndef BOOST_MP11_HPP_INCLUDED #include #endif #include #include #include #include #include #include #include #include // namespace boost { #ifdef BOOST_NO_EXCEPTIONS BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined #endif namespace variant2 { // bad_variant_access class bad_variant_access: public std::exception { public: bad_variant_access() noexcept { } char const * what() const noexcept { return "bad_variant_access"; } }; namespace detail { BOOST_NORETURN inline void throw_bad_variant_access() { #ifdef BOOST_NO_EXCEPTIONS boost::throw_exception( bad_variant_access() ); #else throw bad_variant_access(); #endif } } // namespace detail // monostate struct 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; } // variant forward declaration template class variant; // variant_size template struct variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template /*inline*/ constexpr std::size_t variant_size_v = variant_size::value; #endif template struct variant_size>: mp11::mp_size> { }; // variant_alternative template struct variant_alternative; template using variant_alternative_t = typename variant_alternative::type; #if BOOST_WORKAROUND(BOOST_GCC, < 40900) namespace detail { template struct variant_alternative_impl { }; template struct variant_alternative_impl, true> { using type = mp11::mp_at_c, I>; }; template struct variant_alternative_impl const, true>: std::add_const< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl volatile, true>: std::add_volatile< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl const volatile, true>: std::add_cv< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl const&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> const > { }; template struct variant_alternative_impl volatile&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> volatile > { }; template struct variant_alternative_impl const volatile&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> const volatile > { }; template struct variant_alternative_impl&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl const&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> const > { }; template struct variant_alternative_impl volatile&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> volatile > { }; template struct variant_alternative_impl const volatile&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> const volatile > { }; } // namespace detail template struct variant_alternative { }; template struct variant_alternative>: public detail::variant_alternative_impl, (I < sizeof...(T))> { }; template struct variant_alternative const>: public detail::variant_alternative_impl const, (I < sizeof...(T))> { }; template struct variant_alternative volatile>: public detail::variant_alternative_impl volatile, (I < sizeof...(T))> { }; template struct variant_alternative const volatile>: public detail::variant_alternative_impl const volatile, (I < sizeof...(T))> { }; template struct variant_alternative&>: public detail::variant_alternative_impl&, (I < sizeof...(T))> { }; template struct variant_alternative const&>: public detail::variant_alternative_impl const&, (I < sizeof...(T))> { }; template struct variant_alternative volatile&>: public detail::variant_alternative_impl volatile&, (I < sizeof...(T))> { }; template struct variant_alternative const volatile&>: public detail::variant_alternative_impl const volatile&, (I < sizeof...(T))> { }; template struct variant_alternative&&>: public detail::variant_alternative_impl&&, (I < sizeof...(T))> { }; template struct variant_alternative const&&>: public detail::variant_alternative_impl const&&, (I < sizeof...(T))> { }; template struct variant_alternative volatile&&>: public detail::variant_alternative_impl volatile&&, (I < sizeof...(T))> { }; template struct variant_alternative const volatile&&>: public detail::variant_alternative_impl const volatile&&, (I < sizeof...(T))> { }; #else namespace detail { #if defined( BOOST_MP11_VERSION ) && BOOST_MP11_VERSION >= 107000 template using var_alt_impl = mp11::mp_invoke_q>; #else template using var_alt_impl = mp11::mp_invoke>; #endif } // namespace detail template struct variant_alternative { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative>: mp11::mp_defer, mp11::mp_size_t> { }; #endif // variant_npos constexpr std::size_t variant_npos = ~static_cast( 0 ); // holds_alternative template constexpr bool holds_alternative( variant const& v ) noexcept { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); return v.index() == mp11::mp_find, U>::value; } // get (index) template constexpr variant_alternative_t>& get(variant& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t>&& get(variant&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t() ) ); #else if( v.index() != I ) detail::throw_bad_variant_access(); return std::move( v._get_impl( mp11::mp_size_t() ) ); #endif } template constexpr variant_alternative_t> const& get(variant const& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t> const&& get(variant const&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t() ) ); #else if( v.index() != I ) detail::throw_bad_variant_access(); return std::move( v._get_impl( mp11::mp_size_t() ) ); #endif } // detail::unsafe_get (for visit) namespace detail { template constexpr variant_alternative_t>& unsafe_get(variant& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t>&& unsafe_get(variant&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return std::move( v._get_impl( mp11::mp_size_t() ) ); } template constexpr variant_alternative_t> const& unsafe_get(variant const& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t> const&& unsafe_get(variant const&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return std::move( v._get_impl( mp11::mp_size_t() ) ); } } // namespace detail // get (type) template constexpr U& get(variant& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() ); } template constexpr U&& get(variant&& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) ); #else if( v.index() != I::value ) detail::throw_bad_variant_access(); return std::move( v._get_impl( I() ) ); #endif } template constexpr U const& get(variant const& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() ); } template constexpr U const&& get(variant const&& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) ); #else if( v.index() != I::value ) detail::throw_bad_variant_access(); return std::move( v._get_impl( I() ) ); #endif } // get_if template constexpr typename std::add_pointer>>::type get_if(variant* v) noexcept { static_assert( I < sizeof...(T), "Index out of bounds" ); return v && v->index() == I? &v->_get_impl( mp11::mp_size_t() ): 0; } template constexpr typename std::add_pointer>>::type get_if(variant const * v) noexcept { static_assert( I < sizeof...(T), "Index out of bounds" ); return v && v->index() == I? &v->_get_impl( mp11::mp_size_t() ): 0; } template constexpr typename std::add_pointer::type get_if(variant* v) noexcept { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return v && v->index() == I::value? &v->_get_impl( I() ): 0; } template constexpr typename std::add_pointer::type get_if(variant const * v) noexcept { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return v && v->index() == I::value? &v->_get_impl( I() ): 0; } // namespace detail { // trivially_* #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 template struct is_trivially_copy_constructible: mp11::mp_bool::value && std::has_trivial_copy_constructor::value> { }; template struct is_trivially_copy_assignable: mp11::mp_bool::value && std::has_trivial_copy_assign::value> { }; template struct is_trivially_move_constructible: mp11::mp_bool::value && std::is_trivial::value> { }; template struct is_trivially_move_assignable: mp11::mp_bool::value && std::is_trivial::value> { }; #else using std::is_trivially_copy_constructible; using std::is_trivially_copy_assignable; using std::is_trivially_move_constructible; using std::is_trivially_move_assignable; #endif // variant_storage template union variant_storage_impl; template using variant_storage = variant_storage_impl...>, T...>; template union variant_storage_impl { }; // not all trivially destructible template union variant_storage_impl { T1 first_; variant_storage rest_; template constexpr explicit variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward(a)... ) { } template constexpr explicit variant_storage_impl( mp11::mp_size_t, A&&... a ): rest_( mp11::mp_size_t(), std::forward(a)... ) { } ~variant_storage_impl() { } template void emplace( mp11::mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } template void emplace( mp11::mp_size_t, A&&... a ) { rest_.emplace( mp11::mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I-1>& get( mp11::mp_size_t ) noexcept { return rest_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I-1> const& get( mp11::mp_size_t ) const noexcept { return rest_.get( mp11::mp_size_t() ); } }; // all trivially destructible template union variant_storage_impl { T1 first_; variant_storage rest_; template constexpr explicit variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward(a)... ) { } template constexpr explicit variant_storage_impl( mp11::mp_size_t, A&&... a ): rest_( mp11::mp_size_t(), std::forward(a)... ) { } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, mp11::mp_size_t, A&&... a ) { rest_.emplace( mp11::mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, mp11::mp_size_t, A&&... a ) { *this = variant_storage_impl( mp11::mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace( mp11::mp_size_t, A&&... a ) { this->emplace_impl( mp11::mp_all, detail::is_trivially_move_assignable...>(), mp11::mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I-1>& get( mp11::mp_size_t ) noexcept { return rest_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I-1> const& get( mp11::mp_size_t ) const noexcept { return rest_.get( mp11::mp_size_t() ); } }; // resolve_overload_* template struct overload; template<> struct overload<> { void operator()() const; }; template struct overload: overload { using overload::operator(); mp11::mp_identity operator()(T1) const; }; #if BOOST_WORKAROUND( BOOST_MSVC, < 1930 ) template using resolve_overload_type_ = decltype( overload()(std::declval()) ); template struct resolve_overload_type_impl: mp11::mp_defer< resolve_overload_type_, U, T... > { }; template using resolve_overload_type = typename resolve_overload_type_impl::type::type; #else template using resolve_overload_type = typename decltype( overload()(std::declval()) )::type; #endif template using resolve_overload_index = mp11::mp_find, resolve_overload_type>; // variant_base template struct variant_base_impl; template using variant_base = variant_base_impl...>::value, mp11::mp_all...>::value, T...>; struct none {}; // trivially destructible, single buffered template struct variant_base_impl { int ix_; variant_storage st1_; constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t(), std::forward(a)... ) { } // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &st1_ ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = I::value + 1; } constexpr std::size_t index() const noexcept { return ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J ); return st1_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { // size_t const J = I+1; // assert( ix_ == I+1 ); return st1_.get( mp11::mp_size_t() ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, A&&... a ) { static_assert( std::is_nothrow_constructible::value, "Logic error: U must be nothrow constructible from A&&..." ); st1_.emplace( mp11::mp_size_t(), std::forward(a)... ); ix_ = J; } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, A&&... a ) { static_assert( std::is_nothrow_move_constructible::value, "Logic error: U must be nothrow move constructible" ); U tmp( std::forward(a)... ); st1_.emplace( mp11::mp_size_t(), std::move(tmp) ); ix_ = J; } template BOOST_CXX14_CONSTEXPR void emplace( A&&... a ) { std::size_t const J = I+1; using U = mp11::mp_at_c, I>; this->emplace_impl( std::is_nothrow_constructible(), std::forward(a)... ); } }; // trivially destructible, double buffered template struct variant_base_impl { int ix_; variant_storage st1_; variant_storage st2_; constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t(), std::forward(a)... ), st2_( mp11::mp_size_t<0>() ) { } // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &st1_ ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = I::value + 1; } constexpr std::size_t index() const noexcept { return ix_ >= 0? ix_ - 1: -ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J || -ix_ == J ); constexpr mp11::mp_size_t j{}; return ix_ >= 0? st1_.get( j ): st2_.get( j ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { // size_t const J = I+1; // assert( ix_ == J || -ix_ == J ); // constexpr mp_size_t j{}; return ix_ >= 0? st1_.get( mp11::mp_size_t() ): st2_.get( mp11::mp_size_t() ); } template BOOST_CXX14_CONSTEXPR void emplace( A&&... a ) { size_t const J = I+1; if( ix_ >= 0 ) { st2_.emplace( mp11::mp_size_t(), std::forward(a)... ); ix_ = -static_cast( J ); } else { st1_.emplace( mp11::mp_size_t(), std::forward(a)... ); ix_ = J; } } }; // not trivially destructible, single buffered template struct variant_base_impl { int ix_; variant_storage st1_; constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t(), std::forward(a)... ) { } // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &st1_ ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = I::value + 1; } //[&]( auto I ){ // using U = mp_at_c, I>; // st1_.get( I ).~U(); //} struct _destroy_L1 { variant_base_impl * this_; template void operator()( I ) const noexcept { using U = mp11::mp_at, I>; this_->st1_.get( I() ).~U(); } }; void _destroy() noexcept { if( ix_ > 0 ) { mp11::mp_with_index<1 + sizeof...(T)>( ix_, _destroy_L1{ this } ); } } ~variant_base_impl() noexcept { _destroy(); } constexpr std::size_t index() const noexcept { return ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J ); return st1_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { // size_t const J = I+1; // assert( ix_ == J ); return st1_.get( mp11::mp_size_t() ); } template void emplace( A&&... a ) { size_t const J = I+1; using U = mp11::mp_at_c, I>; static_assert( std::is_nothrow_move_constructible::value, "Logic error: U must be nothrow move constructible" ); U tmp( std::forward(a)... ); _destroy(); st1_.emplace( mp11::mp_size_t(), std::move(tmp) ); ix_ = J; } }; // not trivially destructible, double buffered template struct variant_base_impl { int ix_; variant_storage st1_; variant_storage st2_; constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t(), std::forward(a)... ), st2_( mp11::mp_size_t<0>() ) { } // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &st1_ ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = I::value + 1; } //[&]( auto I ){ // using U = mp_at_c, I>; // st1_.get( I ).~U(); //} struct _destroy_L1 { variant_base_impl * this_; template void operator()( I ) const noexcept { using U = mp11::mp_at, I>; this_->st1_.get( I() ).~U(); } }; struct _destroy_L2 { variant_base_impl * this_; template void operator()( I ) const noexcept { using U = mp11::mp_at, I>; this_->st2_.get( I() ).~U(); } }; void _destroy() noexcept { if( ix_ > 0 ) { mp11::mp_with_index<1 + sizeof...(T)>( ix_, _destroy_L1{ this } ); } else if( ix_ < 0 ) { mp11::mp_with_index<1 + sizeof...(T)>( -ix_, _destroy_L2{ this } ); } } ~variant_base_impl() noexcept { _destroy(); } constexpr std::size_t index() const noexcept { return ix_ >= 0? ix_ - 1: -ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J || -ix_ == J ); constexpr mp11::mp_size_t j{}; return ix_ >= 0? st1_.get( j ): st2_.get( j ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { // size_t const J = I+1; // assert( ix_ == J || -ix_ == J ); // constexpr mp_size_t j{}; return ix_ >= 0? st1_.get( mp11::mp_size_t() ): st2_.get( mp11::mp_size_t() ); } template void emplace( A&&... a ) { size_t const J = I+1; if( ix_ >= 0 ) { st2_.emplace( mp11::mp_size_t(), std::forward(a)... ); _destroy(); ix_ = -static_cast( J ); } else { st1_.emplace( mp11::mp_size_t(), std::forward(a)... ); _destroy(); ix_ = J; } } }; } // namespace detail // in_place_type_t template struct in_place_type_t { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template constexpr in_place_type_t in_place_type{}; #endif namespace detail { template struct is_in_place_type: std::false_type {}; template struct is_in_place_type>: std::true_type {}; } // namespace detail // in_place_index_t template struct in_place_index_t { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template constexpr in_place_index_t in_place_index{}; #endif namespace detail { template struct is_in_place_index: std::false_type {}; template struct is_in_place_index>: std::true_type {}; } // namespace detail // is_nothrow_swappable namespace detail { namespace det2 { using std::swap; template using is_swappable_impl = decltype(swap(std::declval(), std::declval())); #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template struct is_nothrow_swappable_impl_ { static constexpr bool value = noexcept(swap(std::declval(), std::declval())); }; template using is_nothrow_swappable_impl = mp11::mp_bool< is_nothrow_swappable_impl_::value >; #else template using is_nothrow_swappable_impl = typename std::enable_if(), std::declval()))>::type; #endif } // namespace det2 template struct is_swappable: mp11::mp_valid { }; #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template struct is_nothrow_swappable: mp11::mp_eval_if>, mp11::mp_false, det2::is_nothrow_swappable_impl, T> { }; #else template struct is_nothrow_swappable: mp11::mp_valid { }; #endif } // namespace detail // variant template class variant: private detail::variant_base { private: using variant_base = detail::variant_base; private: variant( variant const volatile& r ) = delete; variant& operator=( variant const volatile& r ) = delete; public: // constructors template> >, E1>> constexpr variant() noexcept( std::is_nothrow_default_constructible< mp11::mp_first> >::value ) : variant_base( mp11::mp_size_t<0>() ) { } template...>, E1> > constexpr variant( variant const& r ) noexcept : variant_base( static_cast(r) ) { } private: struct L1 { variant_base * this_; variant const & r; template void operator()( I i ) const { this_->_replace( i, r._get_impl( i ) ); } }; public: template...>>, E1>, class E3 = mp11::mp_if...>, E1> > variant( variant const& r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L1{ this, r } ); } template...>, E1> > constexpr variant( variant && r ) noexcept : variant_base( static_cast(r) ) { } private: struct L2 { variant_base * this_; variant & r; template void operator()( I i ) const { this_->_replace( i, std::move( r._get_impl( i ) ) ); } }; public: template...>>, E1>, class E3 = mp11::mp_if...>, E1> > variant( variant && r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L2{ this, r } ); } template::type, class E1 = typename std::enable_if< !std::is_same::value && !detail::is_in_place_index::value && !detail::is_in_place_type::value >::type, class V = detail::resolve_overload_type, class E2 = typename std::enable_if::value>::type > constexpr variant( U&& u ) noexcept( std::is_nothrow_constructible::value ) : variant_base( detail::resolve_overload_index(), std::forward(u) ) { } template, U>, class E = typename std::enable_if::value>::type> constexpr explicit variant( in_place_type_t, A&&... a ): variant_base( I(), std::forward(a)... ) { } template, U>, class E = typename std::enable_if&, A&&...>::value>::type> constexpr explicit variant( in_place_type_t, std::initializer_list il, A&&... a ): variant_base( I(), il, std::forward(a)... ) { } template, I>, A&&...>::value>::type> constexpr explicit variant( in_place_index_t, A&&... a ): variant_base( mp11::mp_size_t(), std::forward(a)... ) { } template, I>, std::initializer_list&, A&&...>::value>::type> constexpr explicit variant( in_place_index_t, std::initializer_list il, A&&... a ): variant_base( mp11::mp_size_t(), il, std::forward(a)... ) { } // assignment template..., detail::is_trivially_copy_constructible..., detail::is_trivially_copy_assignable...>, E1> > BOOST_CXX14_CONSTEXPR variant& operator=( variant const & r ) noexcept { static_cast( *this ) = static_cast( r ); return *this; } private: struct L3 { variant * this_; variant const & r; template void operator()( I i ) const { this_->variant_base::template emplace( r._get_impl( i ) ); } }; public: template..., detail::is_trivially_copy_constructible..., detail::is_trivially_copy_assignable...>>, E1>, class E3 = mp11::mp_if..., std::is_copy_assignable...>, E1> > BOOST_CXX14_CONSTEXPR variant& operator=( variant const & r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L3{ this, r } ); return *this; } template..., detail::is_trivially_move_constructible..., detail::is_trivially_move_assignable...>, E1> > BOOST_CXX14_CONSTEXPR variant& operator=( variant && r ) noexcept { static_cast( *this ) = static_cast( r ); return *this; } private: struct L4 { variant * this_; variant & r; template void operator()( I i ) const { this_->variant_base::template emplace( std::move( r._get_impl( i ) ) ); } }; public: template..., detail::is_trivially_move_constructible..., detail::is_trivially_move_assignable...>>, E1>, class E3 = mp11::mp_if..., std::is_move_assignable...>, E1> > variant& operator=( variant && r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L4{ this, r } ); return *this; } template::type, variant>::value>::type, class V = detail::resolve_overload_type, class E2 = typename std::enable_if::value && std::is_constructible::value>::type > BOOST_CXX14_CONSTEXPR variant& operator=( U&& u ) noexcept( std::is_nothrow_constructible::value ) { std::size_t const I = detail::resolve_overload_index::value; this->template emplace( std::forward(u) ); return *this; } // modifiers template, U>::value == 1 && std::is_constructible::value >::type> BOOST_CXX14_CONSTEXPR U& emplace( A&&... a ) { using I = mp11::mp_find, U>; variant_base::template emplace( std::forward(a)... ); return _get_impl( I() ); } template, U>::value == 1 && std::is_constructible&, A&&...>::value >::type> BOOST_CXX14_CONSTEXPR U& emplace( std::initializer_list il, A&&... a ) { using I = mp11::mp_find, U>; variant_base::template emplace( il, std::forward(a)... ); return _get_impl( I() ); } template, I>, A&&...>::value>::type> BOOST_CXX14_CONSTEXPR variant_alternative_t>& emplace( A&&... a ) { variant_base::template emplace( std::forward(a)... ); return _get_impl( mp11::mp_size_t() ); } template, I>, std::initializer_list&, A&&...>::value>::type> BOOST_CXX14_CONSTEXPR variant_alternative_t>& emplace( std::initializer_list il, A&&... a ) { variant_base::template emplace( il, std::forward(a)... ); return _get_impl( mp11::mp_size_t() ); } // value status constexpr bool valueless_by_exception() const noexcept { return false; } using variant_base::index; // swap private: struct L5 { variant * this_; variant & r; template void operator()( I i ) const { using std::swap; swap( this_->_get_impl( i ), r._get_impl( i ) ); } }; public: void swap( variant& r ) noexcept( mp11::mp_all..., detail::is_nothrow_swappable...>::value ) { if( index() == r.index() ) { mp11::mp_with_index( index(), L5{ this, r } ); } else { variant tmp( std::move(*this) ); *this = std::move( r ); r = std::move( tmp ); } } // private accessors using variant_base::_get_impl; // converting constructors (extension) private: template struct L6 { variant_base * this_; variant const & r; template void operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; this_->_replace( J{}, r._get_impl( i ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > variant( variant const& r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L6{ this, r } ); } private: template struct L7 { variant_base * this_; variant & r; template void operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; this_->_replace( J{}, std::move( r._get_impl( i ) ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > variant( variant && r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L7{ this, r } ); } // subset (extension) private: template::type> static constexpr variant _subset_impl( mp11::mp_size_t, V && v ) { return variant( in_place_index_t(), std::forward(v) ); } template static variant _subset_impl( mp11::mp_size_t, V && /*v*/ ) { detail::throw_bad_variant_access(); } private: template struct L8 { variant * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, this_->_get_impl( i ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > BOOST_CXX14_CONSTEXPR variant subset() & { return mp11::mp_with_index( index(), L8{ this } ); } private: template struct L9 { variant const * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, this_->_get_impl( i ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > constexpr variant subset() const& { return mp11::mp_with_index( index(), L9{ this } ); } private: template struct L10 { variant * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, std::move( this_->_get_impl( i ) ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > BOOST_CXX14_CONSTEXPR variant subset() && { return mp11::mp_with_index( index(), L10{ this } ); } #if !BOOST_WORKAROUND(BOOST_GCC, < 40900) // g++ 4.8 doesn't handle const&& particularly well private: template struct L11 { variant const * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, std::move( this_->_get_impl( i ) ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > constexpr variant subset() const&& { return mp11::mp_with_index( index(), L11{ this } ); } #endif }; // relational operators namespace detail { template struct eq_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) == w._get_impl( i ); } }; } // namespace detail template constexpr bool operator==( variant const & v, variant const & w ) { return v.index() == w.index() && mp11::mp_with_index( v.index(), detail::eq_L{ v, w } ); } namespace detail { template struct ne_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) != w._get_impl( i ); } }; } // namespace detail template constexpr bool operator!=( variant const & v, variant const & w ) { return v.index() != w.index() || mp11::mp_with_index( v.index(), detail::ne_L{ v, w } ); } namespace detail { template struct lt_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) < w._get_impl( i ); } }; } // namespace detail template constexpr bool operator<( variant const & v, variant const & w ) { return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index( v.index(), detail::lt_L{ v, w } ) ); } template constexpr bool operator>( variant const & v, variant const & w ) { return w < v; } namespace detail { template struct le_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) <= w._get_impl( i ); } }; } // namespace detail template constexpr bool operator<=( variant const & v, variant const & w ) { return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index( v.index(), detail::le_L{ v, w } ) ); } template constexpr bool operator>=( variant const & v, variant const & w ) { return w <= v; } // visitation namespace detail { template using remove_cv_ref_t = typename std::remove_cv::type>::type; template struct copy_cv_ref { using type = T; }; template struct copy_cv_ref { using type = T const; }; template struct copy_cv_ref { using type = T volatile; }; template struct copy_cv_ref { using type = T const volatile; }; template struct copy_cv_ref { using type = typename copy_cv_ref::type&; }; template struct copy_cv_ref { using type = typename copy_cv_ref::type&&; }; template using copy_cv_ref_t = typename copy_cv_ref::type; template struct Qret { template using fn = decltype( std::declval()( std::declval()... ) ); }; template using front_if_same = mp11::mp_if, mp11::mp_front>; template using apply_cv_ref = mp11::mp_product, mp11::mp_list>; template using Vret = front_if_same, apply_cv_ref...>>; } // namespace detail template constexpr auto visit( F&& f ) -> decltype(std::forward(f)()) { return std::forward(f)(); } namespace detail { template struct visit_L1 { F&& f; V1&& v1; template auto operator()( I ) const -> Vret { return std::forward(f)( unsafe_get( std::forward(v1) ) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L1{ std::forward(f), std::forward(v1) } ); } #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) || BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) namespace detail { template struct bind_front_ { F&& f; A&& a; template auto operator()( T&&... t ) -> decltype( std::forward(f)( std::forward(a), std::forward(t)... ) ) { return std::forward(f)( std::forward(a), std::forward(t)... ); } }; template bind_front_ bind_front( F&& f, A&& a ) { return bind_front_{ std::forward(f), std::forward(a) }; } template struct visit_L2 { F&& f; V1&& v1; V2&& v2; template auto operator()( I ) const -> Vret { auto f2 = bind_front( std::forward(f), unsafe_get( std::forward(v1) ) ); return visit( f2, std::forward(v2) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1, V2&& v2 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L2{ std::forward(f), std::forward(v1), std::forward(v2) } ); } namespace detail { template struct visit_L3 { F&& f; V1&& v1; V2&& v2; V3&& v3; template auto operator()( I ) const -> Vret { auto f2 = bind_front( std::forward(f), unsafe_get( std::forward(v1) ) ); return visit( f2, std::forward(v2), std::forward(v3) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L3{ std::forward(f), std::forward(v1), std::forward(v2), std::forward(v3) } ); } namespace detail { template struct visit_L4 { F&& f; V1&& v1; V2&& v2; V3&& v3; V4&& v4; template auto operator()( I ) const -> Vret { auto f2 = bind_front( std::forward(f), unsafe_get( std::forward(v1) ) ); return visit( f2, std::forward(v2), std::forward(v3), std::forward(v4) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3, V4&& v4 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L4{ std::forward(f), std::forward(v1), std::forward(v2), std::forward(v3), std::forward(v4) } ); } #else template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V&&... v ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), [&]( auto I ){ auto f2 = [&]( auto&&... a ){ return std::forward(f)( detail::unsafe_get( std::forward(v1) ), std::forward(a)... ); }; return visit( f2, std::forward(v2), std::forward(v)... ); }); } #endif // specialized algorithms template..., detail::is_swappable...>::value>::type> void swap( variant & v, variant & w ) noexcept( noexcept(v.swap(w)) ) { v.swap( w ); } } // namespace variant2 } // namespace boost #if defined(_MSC_VER) && _MSC_VER < 1910 # pragma warning( pop ) #endif #endif // #ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED