#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED #define BOOST_VARIANT2_VARIANT_HPP_INCLUDED // Copyright 2017 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 #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"; } }; // 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 /*inline*/ constexpr std::size_t variant_size_v = variant_size::value; template struct variant_size>: mp_size> { }; // variant_alternative template struct variant_alternative; template using variant_alternative_t = typename variant_alternative::type; 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> { }; template struct variant_alternative: mp_defer< variant2::detail::var_alt_impl, mp_size_t, T, mp_quote> { }; template struct variant_alternative: mp_defer< variant2::detail::var_alt_impl, mp_size_t, T, mp_quote> { }; template struct variant_alternative>: mp_defer, mp_size_t> { }; // 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" ); #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( mp_size_t() ); #else if( v.index() != I ) throw bad_variant_access(); return v._get_impl( mp_size_t() ); #endif } template constexpr variant_alternative_t>&& get(variant&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) 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" ); #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( mp_size_t() ); #else if( v.index() != I ) throw bad_variant_access(); return 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" ); #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) 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" ); constexpr auto I = mp_find, U>::value; #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( mp_size_t() ); #else if( v.index() != I ) throw bad_variant_access(); return v._get_impl( mp_size_t() ); #endif } 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" ); constexpr auto I = mp_find, U>::value; #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) 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 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" ); constexpr auto I = mp_find, U>::value; #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( mp_size_t() ); #else if( v.index() != I ) throw bad_variant_access(); return v._get_impl( mp_size_t() ); #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" ); constexpr auto I = mp_find, U>::value; #if BOOST_WORKAROUND( BOOST_GCC, < 60000 ) 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_if template constexpr std::add_pointer_t>> 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 std::add_pointer_t>> 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 std::add_pointer_t get_if(variant* v) noexcept { static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); constexpr auto I = mp_find, U>::value; return v && v->index() == I? &v->_get_impl( mp_size_t() ): 0; } template constexpr std::add_pointer_t 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" ); constexpr auto I = mp_find, U>::value; return v && v->index() == I? &v->_get_impl( mp_size_t() ): 0; } // namespace detail { // 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 ) noexcept { ::new( &first_ ) T1( std::forward(a)... ); } template void emplace( mp_size_t, A&&... a ) noexcept { rest_.emplace( mp_size_t(), std::forward(a)... ); } constexpr T1& get( mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp_size_t<0> ) const noexcept { return first_; } template 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( mp_size_t<0>, A&&... a ) noexcept { ::new( &first_ ) T1( std::forward(a)... ); } template void emplace( mp_size_t, A&&... a ) noexcept { rest_.emplace( mp_size_t(), std::forward(a)... ); } constexpr T1& get( mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp_size_t<0> ) const noexcept { return first_; } template 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, <= 1910 ) 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 struct variant_base_impl; // trivially destructible, single buffered template using variant_base = variant_base_impl...>::value, mp_any...>, std::is_nothrow_default_constructible...>::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 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 ) { std::size_t const J = I+1; std::size_t const K = mp_find_if, std::is_nothrow_constructible>::value; using U = mp_at_c, I>; if( std::is_nothrow_constructible::value ) { st1_.emplace( mp_size_t(), std::forward(a)... ); ix_ = J; } else if( K < sizeof...(T) ) // have nothrow destructible { try { st1_.emplace( mp_size_t(), std::forward(a)... ); ix_ = J; } catch( ... ) { st1_.emplace( mp_size_t() ); ix_ = K; throw; } } else { assert( std::is_nothrow_move_constructible::value ); U tmp( std::forward(a)... ); st1_.emplace( mp_size_t(), std::move(tmp) ); ix_ = J; } } }; // 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 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( j ): st2_.get( j ); } template 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)... ) { } void _destroy() noexcept { if( ix_ > 0 ) { mp_with_index<1 + sizeof...(T)>( ix_, [&]( auto I ){ using U = mp_at_c, I>; st1_.get( I ).~U(); }); } } ~variant_base_impl() noexcept { _destroy(); } constexpr std::size_t index() const noexcept { return ix_ - 1; } template 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; std::size_t const K = mp_find_if, std::is_nothrow_constructible>::value; 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( K < sizeof...(T) ) // have nothrow destructible { _destroy(); try { st1_.emplace( mp_size_t(), std::forward(a)... ); ix_ = J; } catch( ... ) { st1_.emplace( mp_size_t() ); ix_ = K; 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>() ) { } void _destroy() noexcept { if( ix_ > 0 ) { mp_with_index<1 + sizeof...(T)>( ix_, [&]( auto I ){ using U = mp_at_c, I>; st1_.get( I ).~U(); }); } else if( ix_ < 0 ) { mp_with_index<1 + sizeof...(T)>( -ix_, [&]( auto I ){ using U = mp_at_c, I>; st2_.get( I ).~U(); }); } } ~variant_base_impl() noexcept { _destroy(); } constexpr std::size_t index() const noexcept { return ix_ >= 0? ix_ - 1: -ix_ - 1; } template 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( j ): st2_.get( j ); } 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 { }; template constexpr in_place_type_t in_place_type{}; 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 { }; template constexpr in_place_index_t in_place_index{}; 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, <= 1910 ) 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 = std::enable_if_t(), std::declval()))>; #endif } // namespace det2 template struct is_swappable: mp_valid { }; #if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) 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>> variant( variant const& r ) noexcept( mp_all...>::value ) { mp_with_index( r.index(), [&]( auto I ){ ::new( static_cast(this) ) variant_base( I, r._get_impl( I ) ); }); } template...>, E1>> variant( variant && r ) noexcept( mp_all...>::value ) { mp_with_index( r.index(), [&]( auto I ){ ::new( static_cast(this) ) variant_base( I, std::move( r._get_impl( I ) ) ); }); } template, class E1 = std::enable_if_t< !std::is_same::value && !variant2::detail::is_in_place_index::value && !variant2::detail::is_in_place_type::value >, class V = variant2::detail::resolve_overload_type, class E2 = std::enable_if_t::value> > constexpr variant( U&& u ) noexcept( std::is_nothrow_constructible::value ) : variant_base( variant2::detail::resolve_overload_index(), std::forward(u) ) { } template, U>, class E = std::enable_if_t::value>> constexpr explicit variant( in_place_type_t, A&&... a ): variant_base( I(), std::forward(a)... ) { } template, U>, class E = std::enable_if_t&, A...>::value>> constexpr explicit variant( in_place_type_t, std::initializer_list il, A&&... a ): variant_base( I(), il, std::forward(a)... ) { } template, I>, A...>::value>> constexpr explicit variant( in_place_index_t, A&&... a ): variant_base( mp_size_t(), std::forward(a)... ) { } template, I>, std::initializer_list&, A...>::value>> constexpr explicit variant( in_place_index_t, std::initializer_list il, A&&... a ): variant_base( mp_size_t(), il, std::forward(a)... ) { } // assignment template..., std::is_copy_assignable...>, E1>> variant& operator=( variant const & r ) noexcept( mp_all..., std::is_nothrow_copy_assignable...>::value ) { mp_with_index( r.index(), [&]( auto I ){ if( this->index() == I ) { this->_get_impl( I ) = r._get_impl( I ); } else { this->variant_base::template emplace( r._get_impl( I ) ); } }); return *this; } template..., std::is_move_assignable...>, E1>> variant& operator=( variant && r ) noexcept( mp_all..., std::is_nothrow_move_assignable...>::value ) { mp_with_index( r.index(), [&]( auto I ){ 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 ) ) ); } }); return *this; } template, variant>::value>, class V = variant2::detail::resolve_overload_type, class E2 = std::enable_if_t::value && std::is_constructible::value> > 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 = std::enable_if_t::value>> U& emplace( A&&... a ) { variant_base::template emplace( std::forward(a)... ); return _get_impl( I() ); } template, U>, class E = std::enable_if_t&, A...>::value>> U& emplace( std::initializer_list il, A&&... a ) { variant_base::template emplace( il, std::forward(a)... ); return _get_impl( I() ); } template, I>, A...>::value>> 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>> 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 void swap( variant& r ) noexcept( mp_all..., variant2::detail::is_nothrow_swappable...>::value ) { if( index() == r.index() ) { mp_with_index( index(), [&]( auto I ){ using std::swap; swap( this->_get_impl( I ), r._get_impl( I ) ); }); } 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) template..., mp_contains, U>...>, void> > variant( variant const& r ) noexcept( mp_all...>::value ) { mp_with_index( r.index(), [&]( auto I ){ using J = mp_find, mp_at_c, I>>; ::new( static_cast(this) ) variant_base( J{}, r._get_impl( I ) ); }); } template..., mp_contains, U>...>, void> > variant( variant && r ) noexcept( mp_all...>::value ) { mp_with_index( r.index(), [&]( auto I ){ using J = mp_find, mp_at_c, I>>; ::new( static_cast(this) ) variant_base( J{}, std::move( r._get_impl( I ) ) ); }); } // subset (extension) private: template> static variant _subset_impl( mp_size_t, V && v ) { return variant( in_place_index, std::forward(v) ); } template static variant _subset_impl( mp_size_t, V && /*v*/ ) { throw bad_variant_access(); } public: template..., mp_contains, U>...>, void> > constexpr variant subset() & { return mp_with_index( index(), [&]( auto I ){ using J = mp_find, mp_at_c, I>>; return this->_subset_impl( J{}, this->_get_impl( I ) ); }); } template..., mp_contains, U>...>, void> > constexpr variant subset() const& { return mp_with_index( index(), [&]( auto I ){ using J = mp_find, mp_at_c, I>>; return this->_subset_impl( J{}, this->_get_impl( I ) ); }); } template..., mp_contains, U>...>, void> > constexpr variant subset() && { return mp_with_index( index(), [&]( auto I ){ using J = mp_find, mp_at_c, I>>; return this->_subset_impl( J{}, std::move( this->_get_impl( I ) ) ); }); } template..., mp_contains, U>...>, void> > constexpr variant subset() const&& { return mp_with_index( index(), [&]( auto I ){ using J = mp_find, mp_at_c, I>>; return this->_subset_impl( J{}, std::move( this->_get_impl( I ) ) ); }); } }; // relational operators template constexpr bool operator==( variant const & v, variant const & w ) { if( v.index() != w.index() ) return false; return mp_with_index( v.index(), [&]( auto I ){ return v._get_impl( I ) == w._get_impl( I ); }); } template constexpr bool operator!=( variant const & v, variant const & w ) { if( v.index() != w.index() ) return true; return mp_with_index( v.index(), [&]( auto I ){ return v._get_impl( I ) != w._get_impl( I ); }); } template constexpr bool operator<( variant const & v, variant const & w ) { if( v.index() < w.index() ) return true; if( v.index() > w.index() ) return false; return mp_with_index( v.index(), [&]( auto I ){ return v._get_impl( I ) < w._get_impl( I ); }); } template constexpr bool operator>( variant const & v, variant const & w ) { if( v.index() > w.index() ) return true; if( v.index() < w.index() ) return false; return mp_with_index( v.index(), [&]( auto I ){ return v._get_impl( I ) > w._get_impl( I ); }); } template constexpr bool operator<=( variant const & v, variant const & w ) { if( v.index() < w.index() ) return true; if( v.index() > w.index() ) return false; return mp_with_index( v.index(), [&]( auto I ){ return v._get_impl( I ) <= w._get_impl( I ); }); } template constexpr bool operator>=( variant const & v, variant const & w ) { if( v.index() > w.index() ) return true; if( v.index() < w.index() ) return false; return mp_with_index( v.index(), [&]( auto I ){ return v._get_impl( I ) >= w._get_impl( I ); }); } // 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>; template>> using apply_cv_ref_ = mp_if, T&, T>; #if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) 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)(); } template constexpr auto visit( F&& f, V1&& v1 ) -> variant2::detail::Vret { return mp_with_index>( v1.index(), [&]( auto I ){ return std::forward(f)( get( std::forward(v1) ) ); }); } #if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) template constexpr auto visit( F&& f, V1&& v1, V2&& v2 ) -> 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) ); }); } template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3 ) -> 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(v3) ); }); } template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3, V4&& v4 ) -> 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(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>> void swap( variant & v, variant & w ) noexcept( noexcept(v.swap(w)) ) { v.swap( w ); } } // namespace variant2 } // namespace boost #endif // #ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED