diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index b79f1fe..76ab756 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -536,17 +536,15 @@ template struct variant_base_impl void _destroy() noexcept { - mp_for_each>([&]( auto I ){ + if( ix_ > 0 ) + { + mp_for_index_c<1 + sizeof...(T)>( ix_, [&]( auto I ){ - using U = mp_at_c, I>; - constexpr auto J = decltype(I)::value + 1; + using U = mp_at_c, I>; + st1_.get( I ).~U(); - if( J == ix_ ) - { - st1_.get( mp_size_t() ).~U(); - } - - }); + }); + } } ~variant_base_impl() noexcept @@ -635,22 +633,24 @@ template struct variant_base_impl void _destroy() noexcept { - mp_for_each>([&]( auto I ){ + if( ix_ > 0 ) + { + mp_for_index_c<1 + sizeof...(T)>( ix_, [&]( auto I ){ - using U = mp_at_c, I>; - constexpr auto J = decltype(I)::value + 1; + using U = mp_at_c, I>; + st1_.get( I ).~U(); - if( ix_ > 0 && J == ix_ ) - { - st1_.get( mp_size_t() ).~U(); - } + }); + } + else if( ix_ < 0 ) + { + mp_for_index_c<1 + sizeof...(T)>( -ix_, [&]( auto I ){ - if( ix_ < 0 && J == -ix_ ) - { - st2_.get( mp_size_t() ).~U(); - } + using U = mp_at_c, I>; + st2_.get( I ).~U(); - }); + }); + } } ~variant_base_impl() noexcept @@ -810,12 +810,9 @@ public: variant( variant const& r ) noexcept( mp_all...>::value ) { - mp_for_each>([&]( auto I ){ + mp_for_index_c( r.index(), [&]( auto I ){ - if( I == r.index() ) - { - ::new( static_cast(this) ) variant_base( I, r._get_impl( I ) ); - } + ::new( static_cast(this) ) variant_base( I, r._get_impl( I ) ); }); } @@ -824,12 +821,9 @@ public: variant( variant && r ) noexcept( mp_all...>::value ) { - mp_for_each>([&]( auto I ){ + mp_for_index_c( r.index(), [&]( auto I ){ - if( I == r.index() ) - { - ::new( static_cast(this) ) variant_base( I, std::move( r._get_impl( I ) ) ); - } + ::new( static_cast(this) ) variant_base( I, std::move( r._get_impl( I ) ) ); }); } @@ -871,20 +865,15 @@ public: variant& operator=( variant const & r ) noexcept( mp_all..., std::is_nothrow_copy_assignable...>::value ) { - mp_for_each>([&]( auto I ){ + mp_for_index_c( r.index(), [&]( auto I ){ - constexpr auto J = I.value; - - if( J == r.index() ) + if( this->index() == I ) { - if( this->index() == J ) - { - get(*this) = get(r); - } - else - { - this->variant_base::template emplace( get(r) ); - } + get(*this) = get(r); + } + else + { + this->variant_base::template emplace( get(r) ); } }); @@ -896,20 +885,15 @@ public: variant& operator=( variant && r ) noexcept( mp_all..., std::is_nothrow_move_assignable...>::value ) { - mp_for_each>([&]( auto I ){ + mp_for_index_c( r.index(), [&]( auto I ){ - constexpr auto J = I.value; - - if( J == r.index() ) + if( this->index() == I ) { - if( this->index() == J ) - { - get(*this) = get(std::move(r)); - } - else - { - this->variant_base::template emplace( get(std::move(r)) ); - } + get(*this) = get(std::move(r)); + } + else + { + this->variant_base::template emplace( get(std::move(r)) ); } }); @@ -982,15 +966,10 @@ public: { if( index() == r.index() ) { - mp_for_each>([&]( auto I ){ + mp_for_index_c( index(), [&]( auto I ){ - constexpr auto J = I.value; - - if( J == this->index() ) - { - using std::swap; - swap( get(*this), get(r) ); - } + using std::swap; + swap( get(*this), get(r) ); }); } @@ -1017,30 +996,22 @@ template constexpr bool operator==( variant const & v, variant { if( v.index() != w.index() ) return false; - bool r = false; + return mp_for_index_c( v.index(), [&]( auto I ){ - mp_for_each>([&]( auto I ){ - - if( I == v.index() ) r = get(v) == get(w); + return get(v) == get(w); }); - - return r; } template constexpr bool operator!=( variant const & v, variant const & w ) { if( v.index() != w.index() ) return true; - bool r = true; + return mp_for_index_c( v.index(), [&]( auto I ){ - mp_for_each>([&]( auto I ){ - - if( I == v.index() ) r = get(v) != get(w); + return get(v) != get(w); }); - - return r; } template constexpr bool operator<( variant const & v, variant const & w ) @@ -1048,15 +1019,11 @@ template constexpr bool operator<( variant const & v, variant< if( v.index() < w.index() ) return true; if( v.index() > w.index() ) return false; - bool r = false; + return mp_for_index_c( v.index(), [&]( auto I ){ - mp_for_each>([&]( auto I ){ - - if( I == v.index() ) r = get(v) < get(w); + return get(v) < get(w); }); - - return r; } template constexpr bool operator>( variant const & v, variant const & w ) @@ -1064,15 +1031,11 @@ template constexpr bool operator>( variant const & v, variant if( v.index() > w.index() ) return true; if( v.index() < w.index() ) return false; - bool r = false; + return mp_for_index_c( v.index(), [&]( auto I ){ - mp_for_each>([&]( auto I ){ - - if( I == v.index() ) r = get(v) > get(w); + return get(v) > get(w); }); - - return r; } template constexpr bool operator<=( variant const & v, variant const & w ) @@ -1080,15 +1043,11 @@ template constexpr bool operator<=( variant const & v, variant if( v.index() < w.index() ) return true; if( v.index() > w.index() ) return false; - bool r = false; + return mp_for_index_c( v.index(), [&]( auto I ){ - mp_for_each>([&]( auto I ){ - - if( I == v.index() ) r = get(v) <= get(w); + return get(v) <= get(w); }); - - return r; } template constexpr bool operator>=( variant const & v, variant const & w ) @@ -1096,15 +1055,11 @@ template constexpr bool operator>=( variant const & v, variant if( v.index() > w.index() ) return true; if( v.index() < w.index() ) return false; - bool r = false; + return mp_for_index_c( v.index(), [&]( auto I ){ - mp_for_each>([&]( auto I ){ - - if( I == v.index() ) r = get(v) >= get(w); + return get(v) >= get(w); }); - - return r; } // visitation