Compare commits

...

2 Commits

Author SHA1 Message Date
Peter Dimov bc7c00fa67 Update revision history 2026-05-31 15:42:56 +03:00
Peter Dimov 8c935dc996 Fix unwrap_and_invoke for functions returning void. Fixes #145. 2026-05-31 14:34:01 +03:00
4 changed files with 70 additions and 1 deletions
+4
View File
@@ -8,6 +8,10 @@ https://www.boost.org/LICENSE_1_0.txt
# Revision History
:idprefix:
## Changes in Boost 1.92
* Fix `unwrap_and_invoke` for functions returning `void`.
## Changes in Boost 1.91
* The return type of `operator|(result<T&>, U)` has been changed to non-reference.
+24 -1
View File
@@ -66,7 +66,8 @@ template<class R, class T, class E> int invoke_test( R& r, result<T, E> const& r
template<class F, class... A,
class R = decltype( compat::invoke( std::declval<F>(), detail::invoke_unwrap( std::declval<A>() )... ) ),
class E = detail::get_error_type<compat::remove_cvref_t<A>...>
class E = detail::get_error_type<compat::remove_cvref_t<A>...>,
class En = typename std::enable_if< !std::is_void<R>::value >::type
>
auto unwrap_and_invoke( F&& f, A&&... a ) -> result<R, E>
{
@@ -82,6 +83,28 @@ auto unwrap_and_invoke( F&& f, A&&... a ) -> result<R, E>
return compat::invoke( std::forward<F>(f), detail::invoke_unwrap( std::forward<A>(a) )... );
}
template<class F, class... A,
class R = decltype( compat::invoke( std::declval<F>(), detail::invoke_unwrap( std::declval<A>() )... ) ),
class E = detail::get_error_type<compat::remove_cvref_t<A>...>,
class En1 = void,
class En2 = typename std::enable_if< std::is_void<R>::value >::type
>
auto unwrap_and_invoke( F&& f, A&&... a ) -> result<R, E>
{
{
result<void, E> r;
using Q = int[];
(void)Q{ detail::invoke_test( r, a )... };
if( !r ) return r.error();
}
compat::invoke( std::forward<F>(f), detail::invoke_unwrap( std::forward<A>(a) )... );
return {};
}
// unwrap_and_construct
namespace detail
+2
View File
@@ -228,3 +228,5 @@ run unwrap_and_construct.cpp ;
run unwrap_and_construct2.cpp ;
run detail_is_aggregate_test.cpp ;
run unwrap_and_invoke3.cpp ;
+40
View File
@@ -0,0 +1,40 @@
// Copyright 2026 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/system/unwrap_and_invoke.hpp>
#include <boost/system/result.hpp>
#include <boost/core/lightweight_test.hpp>
using namespace boost::system;
int g_x;
void f( int x )
{
g_x += x;
}
int main()
{
{
g_x = 0;
result<int> a1( 1 );
result<void> r = unwrap_and_invoke( f, a1 );
BOOST_TEST( r );
BOOST_TEST_EQ( g_x, 1 );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int> a1( ec );
result<void> r = unwrap_and_invoke( f, a1 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
return boost::report_errors();
}