forked from boostorg/variant2
Add .then
This commit is contained in:
@@ -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 )
|
||||
|
Reference in New Issue
Block a user