Support result<void> & fv, where fv returns void. Refs #119.

This commit is contained in:
Peter Dimov
2024-04-08 18:01:41 +03:00
parent 75ab18c9fd
commit 96fef94580
2 changed files with 63 additions and 1 deletions

View File

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

View File

@ -59,6 +59,10 @@ void fv1( int /*x*/ )
{
}
void fv2()
{
}
int main()
{
{
@ -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();
}