#ifndef BOOST_LEAF_TO_VARIANT_HPP_INCLUDED #define BOOST_LEAF_TO_VARIANT_HPP_INCLUDED // Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc. // 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 __cplusplus >= 201703L #include #include #include #include #include #include namespace boost { namespace leaf { template std::variant()().value())>::type,std::tuple...>> to_variant( TryBlock && try_block ) { static_assert(is_result_type()())>::value, "The return type of the try_block passed to a to_variant function must be registered with leaf::is_result_type"); using T = typename std::decay()().value())>::type; using error_tuple_type = std::tuple...>; using variant_type = std::variant; return try_handle_all( [&]() -> result { if( auto r = std::forward(try_block)() ) return *std::move(r); else return r.error(); }, []( E const * ... e ) -> variant_type { return error_tuple_type { e ? std::optional(*e) : std::optional{}... }; }, []() -> variant_type { return error_tuple_type { }; } ); } } } #endif #endif