Skip to content

Commit

Permalink
Enable result<void> & fv. Refs #117.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Jan 31, 2024
1 parent 3f2f87f commit 5c1d0df
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/boost/system/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,22 @@ 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
>
result<U, E> operator&( result<void, E> const& r, F&& f )
{
if( r.has_error() )
{
return r.error();
}
else
{
return std::forward<F>( f )();
}
}

// result & unary-returning-result

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

int k()
{
return 7;
}

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

{
result<void> r;
result<int> r2 = r & k;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( *r2, 7 );
}

{
result<void> const r;
result<int> r2 = r & k;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( *r2, 7 );
}

{
result<int> r2 = result<void>() & k;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( *r2, 7 );
}

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

BOOST_TEST( r2.has_error() );
}

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

BOOST_TEST( r2.has_error() );
}

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

BOOST_TEST( r2.has_error() );
}

return boost::report_errors();
}

0 comments on commit 5c1d0df

Please sign in to comment.