forked from boostorg/variant2
Add visit_by_index
This commit is contained in:
@ -2208,6 +2208,34 @@ void swap( variant<T...> & v, variant<T...> & w )
|
|||||||
v.swap( w );
|
v.swap( w );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// visit_by_index
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
template<class V, class... F> struct visit_by_index_L
|
||||||
|
{
|
||||||
|
V&& v;
|
||||||
|
std::tuple<F&&...> tp;
|
||||||
|
|
||||||
|
template<class I> void operator()( I ) const
|
||||||
|
{
|
||||||
|
std::get<I::value>( std::move(tp) )( unsafe_get<I::value>( std::forward<V>(v) ) );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template<class V, class... F> void visit_by_index( V&& v, F&&... f )
|
||||||
|
{
|
||||||
|
constexpr auto N = variant_size<V>::value;
|
||||||
|
|
||||||
|
static_assert( N == sizeof...(F), "Incorrect number of function objects" );
|
||||||
|
|
||||||
|
mp11::mp_with_index<N>( v.index(),
|
||||||
|
detail::visit_by_index_L<V, F...>{ std::forward<V>(v), std::tuple<F&&...>( std::forward<F>(f)... ) } );
|
||||||
|
}
|
||||||
|
|
||||||
// hashing support
|
// hashing support
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
|
@ -121,3 +121,5 @@ run variant_visit_r.cpp : : :
|
|||||||
;
|
;
|
||||||
|
|
||||||
compile variant_derived_construct.cpp ;
|
compile variant_derived_construct.cpp ;
|
||||||
|
|
||||||
|
run variant_visit_by_index.cpp ;
|
||||||
|
50
test/variant_visit_by_index.cpp
Normal file
50
test/variant_visit_by_index.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// Copyright 2017, 2021 Peter Dimov.
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// https://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
#include <boost/variant2/variant.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
|
||||||
|
using namespace boost::variant2;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
variant<int, int, float> v;
|
||||||
|
|
||||||
|
visit_by_index( v,
|
||||||
|
[]( int& x ){ BOOST_TEST_EQ( x, 0 ); },
|
||||||
|
[]( int& ){ BOOST_ERROR( "incorrect alternative" ); },
|
||||||
|
[]( float& ){ BOOST_ERROR( "incorrect alternative" ); } );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int const, int, float const> v( 1 );
|
||||||
|
|
||||||
|
visit_by_index( v,
|
||||||
|
[]( int const& x ){ BOOST_TEST_EQ( x, 1 ); },
|
||||||
|
[]( int& ){ BOOST_ERROR( "incorrect alternative" ); },
|
||||||
|
[]( float const& ){ BOOST_ERROR( "incorrect alternative" ); } );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float> const v( in_place_index_t<1>(), 2 );
|
||||||
|
|
||||||
|
visit_by_index( v,
|
||||||
|
[]( int const& ){ BOOST_ERROR( "incorrect alternative" ); },
|
||||||
|
[]( int const& x ){ BOOST_TEST_EQ( x, 2 ); },
|
||||||
|
[]( float const& ){ BOOST_ERROR( "incorrect alternative" ); } );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int const, int, float const> const v( 3.14f );
|
||||||
|
|
||||||
|
visit_by_index( v,
|
||||||
|
[]( int const& ){ BOOST_ERROR( "incorrect alternative" ); },
|
||||||
|
[]( int const& ){ BOOST_ERROR( "incorrect alternative" ); },
|
||||||
|
[]( float const& x ){ BOOST_TEST_EQ( x, 3.14f ); } );
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Reference in New Issue
Block a user