From 1f8c4a19003dabeb3beaaa4a84740dd26412e8f9 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 18 Oct 2022 06:29:57 +0300 Subject: [PATCH] Replace polymorphic lambdas with function objects for C++11 --- include/boost/variant2/variant.hpp | 61 ++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index b474643..2c7b7bb 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -2449,16 +2449,52 @@ template<> struct is_null_like< variant2::monostate, void >: std::true_type namespace variant2 { +namespace detail +{ + +struct tag_invoke_L1 +{ + boost::json::value& v; + + template void operator()( T const& t ) const + { + boost::json::value_from( t, v ); + } +}; + +} // namespace detail + template void tag_invoke( boost::json::value_from_tag const&, boost::json::value& v, variant const & w ) { - visit( [&](auto const& t){ - - boost::json::value_from( t, v ); - - }, w ); + visit( detail::tag_invoke_L1{ v }, w ); } +namespace detail +{ + +template struct tag_invoke_L2 +{ + boost::json::value const& v; + typename boost::json::result_for::type& r; + + template void operator()( I i ) const + { + if( !r ) + { + using Ti = mp11::mp_at_c; + auto r2 = boost::json::try_value_to( v ); + + if( r2 ) + { + r.emplace( in_place_index_t{}, *r2 ); + } + } + } +}; + +} // namespace detail + template typename boost::json::result_for, boost::json::value>::type tag_invoke( boost::json::try_value_to_tag> const&, boost::json::value const& v ) @@ -2466,20 +2502,7 @@ template static constexpr boost::source_location loc = BOOST_CURRENT_LOCATION; auto r = boost::json::result_from_errno< variant >( EINVAL, &loc ); - mp11::mp_for_each>( [&](auto I){ - - if( !r ) - { - using Ti = mp11::mp_at_c, I>; - auto r2 = boost::json::try_value_to( v ); - - if( r2 ) - { - r.emplace( in_place_index, *r2 ); - } - } - - }); + mp11::mp_for_each>( detail::tag_invoke_L2< variant >{ v, r } ); return r; }