Add operator|( result, nullary-returning-result )

This commit is contained in:
Peter Dimov
2023-10-29 03:20:08 +03:00
parent c1fa3619b6
commit dc73ca428b
4 changed files with 401 additions and 0 deletions
+60
View File
@@ -890,6 +890,8 @@ public:
namespace detail
{
// is_value_convertible_to
template<class T, class U> struct is_value_convertible_to: std::is_convertible<T, U>
{
};
@@ -901,6 +903,11 @@ template<class T, class U> struct is_value_convertible_to<T, U&>:
{
};
// is_result
template<class T> struct is_result: std::false_type {};
template<class T, class E> struct is_result< result<T, E> >: std::true_type {};
} // namespace detail
// result | value
@@ -969,6 +976,59 @@ template<class T, class E, class F,
}
}
// result | nullary-returning-result
template<class T, class E, class F,
class U = decltype( std::declval<F>()() ),
class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
class En2 = typename std::enable_if<detail::is_value_convertible_to<T, typename U::value_type>::value>::type
>
U operator|( result<T, E> const& r, F&& f )
{
if( r )
{
return *r;
}
else
{
return std::forward<F>( f )();
}
}
template<class T, class E, class F,
class U = decltype( std::declval<F>()() ),
class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
class En2 = typename std::enable_if<detail::is_value_convertible_to<T, typename U::value_type>::value>::type
>
U operator|( result<T, E>&& r, F&& f )
{
if( r )
{
return *std::move( r );
}
else
{
return std::forward<F>( f )();
}
}
template<class E, class F,
class U = decltype( std::declval<F>()() ),
class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
class En2 = typename std::enable_if<std::is_void<typename U::value_type>::value>::type
>
U operator|( result<void, E> const& r, F&& f )
{
if( r )
{
return {};
}
else
{
return std::forward<F>( f )();
}
}
} // namespace system
} // namespace boost