Skip to content

Commit

Permalink
Support result<X> & 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 f9a4655 commit 75ab18c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
40 changes: 38 additions & 2 deletions include/boost/system/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,8 @@ result<T, E>& operator|=( result<T, E>& r, F&& f )

template<class T, class E, class F,
class U = decltype( std::declval<F>()( std::declval<T const&>() ) ),
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<T, E> const& r, F&& f )
{
Expand All @@ -1140,7 +1141,8 @@ result<U, E> operator&( result<T, E> const& r, F&& f )

template<class T, class E, class F,
class U = decltype( std::declval<F>()( std::declval<T>() ) ),
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<T, E>&& r, F&& f )
{
Expand All @@ -1154,6 +1156,40 @@ result<U, E> operator&( result<T, E>&& r, F&& f )
}
}

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

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

template<class E, class F,
class U = decltype( std::declval<F>()() ),
class En = typename std::enable_if<!detail::is_result<U>::value>::type
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 @@ -55,6 +55,10 @@ int k()
return 7;
}

void fv1( int /*x*/ )
{
}

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

{
result<int> r( 1 );
result<void> r2 = r & fv1;

BOOST_TEST( r2.has_value() );
}

{
result<int> const r( 1 );
result<void> r2 = r & fv1;

BOOST_TEST( r2.has_value() );
}

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

BOOST_TEST( r2.has_value() );
}

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

BOOST_TEST( r2.has_error() );
}

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

BOOST_TEST( r2.has_error() );
}

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

BOOST_TEST( r2.has_error() );
}

return boost::report_errors();
}

0 comments on commit 75ab18c

Please sign in to comment.