Skip to content

Commit

Permalink
Support result<void> & fv, where fv returns void. Refs #119.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Apr 8, 2024
1 parent 75ab18c commit 96fef94
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
20 changes: 19 additions & 1 deletion include/boost/system/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,8 @@ result<U, E> operator&( result<T, E>&& r, F&& f )

template<class E, class F,
class U = decltype( std::declval<F>()() ),
class En = typename std::enable_if<!detail::is_result<U>::value>::type
class En1 = typename std::enable_if<!detail::is_result<U>::value>::type,
class En2 = typename std::enable_if<!std::is_void<U>::value>::type
>
result<U, E> operator&( result<void, E> const& r, F&& f )
{
Expand All @@ -1206,6 +1207,23 @@ result<U, E> operator&( result<void, E> const& r, F&& f )
}
}

template<class E, class F,
class U = decltype( std::declval<F>()() ),
class En = typename std::enable_if<std::is_void<U>::value>::type
>
result<U, E> operator&( result<void, E> const& r, F&& f )
{
if( r.has_error() )
{
return r.error();
}
else
{
std::forward<F>( f )();
return {};
}
}

// result & unary-returning-result

template<class T, class E, class F,
Expand Down
44 changes: 44 additions & 0 deletions test/result_and_fn1v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ void fv1( int /*x*/ )
{
}

void fv2()
{
}

int main()
{
{
Expand Down Expand Up @@ -243,5 +247,45 @@ int main()
BOOST_TEST( r2.has_error() );
}

{
result<void> r;
result<void> r2 = r & fv2;

BOOST_TEST( r2.has_value() );
}

{
result<void> const r;
result<void> r2 = r & fv2;

BOOST_TEST( r2.has_value() );
}

{
result<void> r2 = result<void>() & fv2;

BOOST_TEST( r2.has_value() );
}

{
result<void, E> r( in_place_error );
result<void, E> r2 = r & fv2;

BOOST_TEST( r2.has_error() );
}

{
result<void, E> const r( in_place_error );
result<void, E> r2 = r & fv2;

BOOST_TEST( r2.has_error() );
}

{
result<void, E> r2 = result<void, E>( in_place_error ) & fv2;

BOOST_TEST( r2.has_error() );
}

return boost::report_errors();
}

0 comments on commit 96fef94

Please sign in to comment.