Support result<void> &= fv. Refs #119.

This commit is contained in:
Peter Dimov
2024-04-09 03:01:38 +03:00
parent 4aeebd2173
commit 93852d1a7e
2 changed files with 41 additions and 0 deletions

View File

@ -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,

View File

@ -59,6 +59,13 @@ int& h( int& )
return x;
}
static int fv_called;
void fv()
{
++fv_called;
}
int main()
{
{
@ -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();
}