Skip to content

Commit

Permalink
Support result<void> &= fv. Refs #119.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Apr 9, 2024
1 parent 4aeebd2 commit 93852d1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/boost/system/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,20 @@ result<T, E>& operator&=( result<T, E>& r, F&& f )
return r;
}

template<class E, class F,
class U = decltype( std::declval<F>()() ),
class En = typename std::enable_if<!detail::is_result<U>::value>::type
>
result<void, E>& operator&=( result<void, E>& r, F&& f )
{
if( r )
{
std::forward<F>( f )();
}

return r;
}

// result &= unary-returning-result

template<class T, class E, class F,
Expand Down
27 changes: 27 additions & 0 deletions test/result_and_eq_fn1v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ int& h( int& )
return x;
}

static int fv_called;

void fv()
{
++fv_called;
}

int main()
{
{
Expand Down Expand Up @@ -110,5 +117,25 @@ int main()
BOOST_TEST( r.has_error() );
}

{
result<void> r;
fv_called = 0;

r &= fv;

BOOST_TEST( r.has_value() );
BOOST_TEST_EQ( fv_called, 1 );
}

{
result<void, E> r( in_place_error );
fv_called = 0;

r &= fv;

BOOST_TEST( r.has_error() );
BOOST_TEST_EQ( fv_called, 0 );
}

return boost::report_errors();
}

0 comments on commit 93852d1

Please sign in to comment.