Add .then

This commit is contained in:
Peter Dimov
2017-06-02 18:11:43 +03:00
parent 3d2a3cb3eb
commit 0b387f5116

View File

@@ -95,7 +95,7 @@ public:
}
};
// expected
// throw_on_unexpected
template<class E> void throw_on_unexpected( E const& e )
{
@@ -119,6 +119,13 @@ void throw_on_unexpected( std::exception_ptr const & e )
}
}
// expected
template<class T, class... E> class expected;
template<class T> struct is_expected: std::false_type {};
template<class T, class... E> struct is_expected<expected<T, E...>>: std::true_type {};
template<class T, class... E> class expected
{
private:
@@ -387,6 +394,28 @@ public:
});
}
// then
private:
template<class F, class U> using then_result_ = decltype( std::declval<F>()( std::declval<U>() ) );
template<class F, class U, class R = then_result_<F, U>> using then_result = mp_if<is_expected<R>, R, expected<R, E...>>;
public:
template<class F> then_result<F, T const&> then( F && f ) const
{
if( has_value() )
{
return std::forward<F>(f)( **this );
}
else
{
return unexpected();
}
}
};
template<class T, class... E> inline constexpr bool operator==( expected<T, E...> const & x1, expected<T, E...> const & x2 )