#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED #define BOOST_VARIANT2_VARIANT_HPP_INCLUDED // Copyright 2017, 2018 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 { namespace variant2 { using namespace boost::mp11; // bad_variant_access class bad_variant_access: public std::exception { public: bad_variant_access() noexcept { } char const * what() const noexcept { return "bad_variant_access"; } }; // 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 { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template /*inline*/ constexpr std::size_t variant_size_v = variant_size::value; #endif template struct variant_size>: 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 = mp_at_c, I>; }; template struct variant_alternative_impl const, true>: std::add_const< mp_at_c, I> > { }; template struct variant_alternative_impl volatile, true>: std::add_volatile< mp_at_c, I> > { }; template struct variant_alternative_impl const volatile, true>: std::add_cv< mp_at_c, I> > { }; } // 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))> { }; #else namespace detail { template using var_alt_impl = mp_invoke>; } // namespace detail template struct variant_alternative { }; template struct variant_alternative: mp_defer< variant2::detail::var_alt_impl, mp_size_t, T, mp_quote_trait> { }; template struct variant_alternative: mp_defer< variant2::detail::var_alt_impl, mp_size_t, T, mp_quote_trait> { }; template struct variant_alternative: mp_defer< variant2::detail::var_alt_impl, mp_size_t, T, mp_quote_trait> { }; template struct variant_alternative>: mp_defer, mp_size_t> { }; #endif // holds_alternative template constexpr bool holds_alternative( variant const& v ) noexcept { static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); return v.index() == mp_find, U>::value; } // get (index) template constexpr variant_alternative_t>& get(variant& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( 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, < 1920) return (void)( v.index() != I? throw bad_variant_access(): 0 ), std::move( v._get_impl( mp_size_t() ) ); #else if( v.index() != I ) throw bad_variant_access(); return std::move( v._get_impl( mp_size_t() ) ); #endif } template constexpr variant_alternative_t> const& get(variant const& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( 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, < 1920) return (void)( v.index() != I? throw bad_variant_access(): 0 ), std::move( v._get_impl( mp_size_t() ) ); #else if( v.index() != I ) throw bad_variant_access(); return std::move( v._get_impl( mp_size_t() ) ); #endif } // get (type) template constexpr U& get(variant& v) { static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp_find, U>; return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), v._get_impl( I() ); } template constexpr U&& get(variant&& v) { static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp_find, U>; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1920) return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), std::move( v._get_impl( I() ) ); #else if( v.index() != I::value ) throw bad_variant_access(); return std::move( v._get_impl( I() ) ); #endif } template constexpr U const& get(variant const& v) { static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp_find, U>; return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), v._get_impl( I() ); } template constexpr U const&& get(variant const&& v) { static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp_find, U>; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1920) return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), std::move( v._get_impl( I() ) ); #else if( v.index() != I::value ) 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( 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( mp_size_t() ): 0; } template constexpr typename std::add_pointer::type get_if(variant* v) noexcept { static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = 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( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = 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: mp_bool::value && std::has_trivial_copy_constructor::value> { }; template struct is_trivially_copy_assignable: mp_bool::value && std::has_trivial_copy_assign::value> { }; template struct is_trivially_move_constructible: mp_bool::value && std::is_trivial::value> { }; template struct is_trivially_move_assignable: 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( mp_size_t<0>, A&&... a ): first_( std::forward(a)... ) { } template constexpr explicit variant_storage_impl( mp_size_t, A&&... a ): rest_( mp_size_t(), std::forward(a)... ) { } ~variant_storage_impl() { } template void emplace( mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } template void emplace( mp_size_t, A&&... a ) { rest_.emplace( mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T1& get( mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp_size_t<0> ) const noexcept { return first_; } template BOOST_CXX14_CONSTEXPR mp_at_c, I-1>& get( mp_size_t ) noexcept { return rest_.get( mp_size_t() ); } template constexpr mp_at_c, I-1> const& get( mp_size_t ) const noexcept { return rest_.get( mp_size_t() ); } }; // all trivially destructible template union variant_storage_impl { T1 first_; variant_storage rest_; template constexpr explicit variant_storage_impl( mp_size_t<0>, A&&... a ): first_( std::forward(a)... ) { } template constexpr explicit variant_storage_impl( mp_size_t, A&&... a ): rest_( mp_size_t(), std::forward(a)... ) { } template void emplace_impl( mp_false, mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp_false, mp_size_t, A&&... a ) { rest_.emplace( mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp_true, mp_size_t, A&&... a ) { *this = variant_storage_impl( mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace( mp_size_t, A&&... a ) { this->emplace_impl( mp_all, variant2::detail::is_trivially_move_assignable...>(), mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T1& get( mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp_size_t<0> ) const noexcept { return first_; } template BOOST_CXX14_CONSTEXPR mp_at_c, I-1>& get( mp_size_t ) noexcept { return rest_.get( mp_size_t() ); } template constexpr mp_at_c, I-1> const& get( mp_size_t ) const noexcept { return rest_.get( mp_size_t() ); } }; // resolve_overload_* template struct overload; template<> struct overload<> { void operator()() const; }; template struct overload: overload { using overload::operator(); mp_identity operator()(T1) const; }; #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template using resolve_overload_type_ = decltype( overload()(std::declval()) ); template struct resolve_overload_type_impl: 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 = mp_find, resolve_overload_type>; // variant_base template using can_be_valueless = mp_any..., std::is_nothrow_default_constructible...>; template using valueless_index = mp_if, monostate>, mp_find, monostate>, mp_find_if, std::is_nothrow_default_constructible>>; template struct variant_base_impl; // trivially destructible, single buffered template using variant_base = variant_base_impl...>::value, mp_any...>, can_be_valueless>::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_( mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp_size_t(), std::forward(a)... ) { } constexpr std::size_t index() const noexcept { return ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp_at_c, I>& _get_impl( mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J ); return st1_.get( mp_size_t() ); } template constexpr mp_at_c, I> const& _get_impl( mp_size_t ) const noexcept { // size_t const J = I+1; // assert( ix_ == I+1 ); return st1_.get( mp_size_t() ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp_true, mp_bool, A&&... a ) { st1_.emplace( mp_size_t(), std::forward(a)... ); ix_ = J; } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp_false, mp_true, A&&... a ) { U tmp( std::forward(a)... ); st1_.emplace( mp_size_t(), std::move(tmp) ); ix_ = J; } template void emplace_impl( mp_false, mp_false, A&&... a ) { if( can_be_valueless::value ) { std::size_t const K = valueless_index::value; assert( K < sizeof...(T) ); try { st1_.emplace( mp_size_t(), std::forward(a)... ); ix_ = J; } catch( ... ) { st1_.emplace( mp_size_t() ); ix_ = K+1; throw; } } else { assert( std::is_nothrow_move_constructible::value ); U tmp( std::forward(a)... ); st1_.emplace( 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 = mp_at_c, I>; this->emplace_impl( std::is_nothrow_constructible(), mp_all, variant2::detail::is_trivially_move_assignable...>(), 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_( mp_size_t<0>() ), st2_( mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp_size_t(), std::forward(a)... ), st2_( mp_size_t<0>() ) { } constexpr std::size_t index() const noexcept { return ix_ >= 0? ix_ - 1: -ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp_at_c, I>& _get_impl( mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J || -ix_ == J ); constexpr mp_size_t j{}; return ix_ >= 0? st1_.get( j ): st2_.get( j ); } template constexpr mp_at_c, I> const& _get_impl( 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( mp_size_t() ): st2_.get( mp_size_t() ); } template BOOST_CXX14_CONSTEXPR void emplace( A&&... a ) { size_t const J = I+1; if( ix_ >= 0 ) { st2_.emplace( mp_size_t(), std::forward(a)... ); ix_ = -static_cast( J ); } else { st1_.emplace( 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_( mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp_size_t(), std::forward(a)... ) { } //[&]( 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 = mp_at, I>; this_->st1_.get( I() ).~U(); } }; void _destroy() noexcept { if( ix_ > 0 ) { 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 mp_at_c, I>& _get_impl( mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J ); return st1_.get( mp_size_t() ); } template constexpr mp_at_c, I> const& _get_impl( mp_size_t ) const noexcept { // size_t const J = I+1; // assert( ix_ == J ); return st1_.get( mp_size_t() ); } template void emplace( A&&... a ) { size_t const J = I+1; using U = mp_at_c, I>; if( std::is_nothrow_constructible::value ) { _destroy(); st1_.emplace( mp_size_t(), std::forward(a)... ); ix_ = J; } else if( can_be_valueless::value ) { std::size_t const K = valueless_index::value; assert( K < sizeof...(T) ); _destroy(); try { st1_.emplace( mp_size_t(), std::forward(a)... ); ix_ = J; } catch( ... ) { st1_.emplace( mp_size_t() ); ix_ = K+1; throw; } } else { assert( std::is_nothrow_move_constructible::value ); U tmp( std::forward(a)... ); _destroy(); st1_.emplace( 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_( mp_size_t<0>() ), st2_( mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp_size_t(), std::forward(a)... ), st2_( mp_size_t<0>() ) { } //[&]( 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 = mp_at, I>; this_->st1_.get( I() ).~U(); } }; struct _destroy_L2 { variant_base_impl * this_; template void operator()( I ) const noexcept { using U = mp_at, I>; this_->st2_.get( I() ).~U(); } }; void _destroy() noexcept { if( ix_ > 0 ) { mp_with_index<1 + sizeof...(T)>( ix_, _destroy_L1{ this } ); } else if( ix_ < 0 ) { 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 mp_at_c, I>& _get_impl( mp_size_t ) noexcept { size_t const J = I+1; assert( ix_ == J || -ix_ == J ); constexpr mp_size_t j{}; return ix_ >= 0? st1_.get( j ): st2_.get( j ); } template constexpr mp_at_c, I> const& _get_impl( 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( mp_size_t() ): st2_.get( mp_size_t() ); } template void emplace( A&&... a ) { size_t const J = I+1; if( ix_ >= 0 ) { st2_.emplace( mp_size_t(), std::forward(a)... ); _destroy(); ix_ = -static_cast( J ); } else { st1_.emplace( 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 = 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: mp_valid { }; #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template struct is_nothrow_swappable: mp_eval_if>, mp_false, det2::is_nothrow_swappable_impl, T> { }; #else template struct is_nothrow_swappable: mp_valid { }; #endif } // namespace detail // variant template class variant: private variant2::detail::variant_base { private: using variant_base = variant2::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< mp_first> >::value ) : variant_base( 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 { ::new( static_cast( this_ ) ) variant_base( i, r._get_impl( i ) ); } }; public: template...>>, E1>, class E3 = mp_if...>, E1> > variant( variant const& r ) noexcept( mp_all...>::value ) { 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 { ::new( static_cast( this_ ) ) variant_base( i, std::move( r._get_impl( i ) ) ); } }; public: template...>>, E1>, class E3 = mp_if...>, E1> > variant( variant && r ) noexcept( mp_all...>::value ) { mp_with_index( r.index(), L2{ this, r } ); } template::type, class E1 = typename std::enable_if< !std::is_same::value && !variant2::detail::is_in_place_index::value && !variant2::detail::is_in_place_type::value >::type, class V = variant2::detail::resolve_overload_type, class E2 = typename std::enable_if::value>::type > constexpr variant( U&& u ) noexcept( std::is_nothrow_constructible::value ) : variant_base( variant2::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( 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( mp_size_t(), il, std::forward(a)... ) { } // assignment template..., variant2::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 { if( this_->index() == i ) { this_->_get_impl( i ) = r._get_impl( i ); } else { this_->variant_base::template emplace( r._get_impl( i ) ); } } }; public: template..., variant2::detail::is_trivially_copy_assignable...>>, E1>, class E3 = mp_if..., std::is_copy_assignable...>, E1> > BOOST_CXX14_CONSTEXPR variant& operator=( variant const & r ) noexcept( mp_all..., std::is_nothrow_copy_assignable...>::value ) { mp_with_index( r.index(), L3{ this, r } ); return *this; } template..., variant2::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 { if( this_->index() == i ) { this_->_get_impl( i ) = std::move( r._get_impl( i ) ); } else { this_->variant_base::template emplace( std::move( r._get_impl( i ) ) ); } } }; public: template..., variant2::detail::is_trivially_move_assignable...>>, E1>, class E3 = mp_if..., std::is_move_assignable...>, E1> > variant& operator=( variant && r ) noexcept( mp_all..., std::is_nothrow_move_assignable...>::value ) { mp_with_index( r.index(), L4{ this, r } ); return *this; } template::type, variant>::value>::type, class V = variant2::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_assignable::value && std::is_nothrow_constructible::value ) { std::size_t const I = variant2::detail::resolve_overload_index::value; if( index() == I ) { _get_impl( mp_size_t() ) = std::forward(u); } else { this->template emplace( std::forward(u) ); } return *this; } // modifiers template, U>, class E = typename std::enable_if::value>::type> BOOST_CXX14_CONSTEXPR U& emplace( A&&... a ) { variant_base::template emplace( std::forward(a)... ); return _get_impl( I() ); } template, U>, class E = typename std::enable_if&, A...>::value>::type> BOOST_CXX14_CONSTEXPR U& emplace( std::initializer_list il, A&&... a ) { 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( 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( mp_size_t() ); } // value status 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( mp_all..., variant2::detail::is_nothrow_swappable...>::value ) { if( index() == r.index() ) { mp_with_index( index(), L5{ this, r } ); } else { variant tmp( std::move(*this) ); *this = std::move( r ); r = std::move( tmp ); } } // private accessors constexpr int _real_index() const noexcept { return this->ix_; } 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 = mp_find, mp_at, I>>; ::new( static_cast(this_) ) variant_base( J{}, r._get_impl( i ) ); } }; public: template..., mp_contains, U>...>, void> > variant( variant const& r ) noexcept( mp_all...>::value ) { 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 = mp_find, mp_at, I>>; ::new( static_cast(this_) ) variant_base( J{}, std::move( r._get_impl( i ) ) ); } }; public: template..., mp_contains, U>...>, void> > variant( variant && r ) noexcept( mp_all...>::value ) { mp_with_index( r.index(), L7{ this, r } ); } // subset (extension) private: template::type> static constexpr variant _subset_impl( mp_size_t, V && v ) { return variant( in_place_index_t(), std::forward(v) ); } template static variant _subset_impl( mp_size_t, V && /*v*/ ) { throw bad_variant_access(); } private: template struct L8 { variant * this_; template variant operator()( I i ) const { using J = mp_find, mp_at, I>>; return this_->_subset_impl( J{}, this_->_get_impl( i ) ); } }; public: template..., mp_contains, U>...>, void> > BOOST_CXX14_CONSTEXPR variant subset() & { return mp_with_index( index(), L8{ this } ); } private: template struct L9 { variant const * this_; template variant operator()( I i ) const { using J = mp_find, mp_at, I>>; return this_->_subset_impl( J{}, this_->_get_impl( i ) ); } }; public: template..., mp_contains, U>...>, void> > constexpr variant subset() const& { return mp_with_index( index(), L9{ this } ); } private: template struct L10 { variant * this_; template variant operator()( I i ) const { using J = mp_find, mp_at, I>>; return this_->_subset_impl( J{}, std::move( this_->_get_impl( i ) ) ); } }; public: template..., mp_contains, U>...>, void> > BOOST_CXX14_CONSTEXPR variant subset() && { return 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 = mp_find, mp_at, I>>; return this_->_subset_impl( J{}, std::move( this_->_get_impl( i ) ) ); } }; public: template..., mp_contains, U>...>, void> > constexpr variant subset() const&& { return mp_with_index( index(), L11{ this } ); } #endif }; // relational operators namespace detail { template struct eq_L { variant const & v; variant const & w; template 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() && mp_with_index( v.index(), detail::eq_L{ v, w } ); } namespace detail { template struct ne_L { variant const & v; variant const & w; template 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() || mp_with_index( v.index(), detail::ne_L{ v, w } ); } namespace detail { template struct lt_L { variant const & v; variant const & w; template 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() && 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 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() && 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 struct Qret { template using fn = decltype( std::declval()( std::declval()... ) ); }; template using front_if_same = mp_if, mp_front>; template using var_size = variant_size::type>; template::type>> using apply_cv_ref_ = mp_if, T&, T>; #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template struct apply_cv_ref_impl { template using _f = apply_cv_ref_; using L = mp_iota>; using type = mp_transform<_f, L>; }; template using apply_cv_ref = typename apply_cv_ref_impl::type; #else template using apply_cv_ref = mp_transform_q, mp_iota>>; #endif 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)( get( std::forward(v1) ) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1 ) -> variant2::detail::Vret { return 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), get( std::forward(v1) ) ); return visit( f2, std::forward(v2) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1, V2&& v2 ) -> variant2::detail::Vret { return 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), 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 ) -> variant2::detail::Vret { return 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), 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 ) -> variant2::detail::Vret { return 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 ) -> variant2::detail::Vret { return mp_with_index>( v1.index(), [&]( auto I ){ auto f2 = [&]( auto&&... a ){ return std::forward(f)( get( std::forward(v1) ), std::forward(a)... ); }; return visit( f2, std::forward(v2), std::forward(v)... ); }); } #endif // specialized algorithms template..., variant2::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