From 0b387f5116ed3e78302c99364741045696fb6bef Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 2 Jun 2017 18:11:43 +0300 Subject: [PATCH] Add .then --- include/boost/variant2/expected.hpp | 31 ++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/include/boost/variant2/expected.hpp b/include/boost/variant2/expected.hpp index fb90ea1..452ae35 100644 --- a/include/boost/variant2/expected.hpp +++ b/include/boost/variant2/expected.hpp @@ -95,7 +95,7 @@ public: } }; -// expected +// throw_on_unexpected template void throw_on_unexpected( E const& e ) { @@ -119,6 +119,13 @@ void throw_on_unexpected( std::exception_ptr const & e ) } } +// expected + +template class expected; + +template struct is_expected: std::false_type {}; +template struct is_expected>: std::true_type {}; + template class expected { private: @@ -387,6 +394,28 @@ public: }); } + + // then + +private: + + template using then_result_ = decltype( std::declval()( std::declval() ) ); + + template> using then_result = mp_if, R, expected>; + +public: + + template then_result then( F && f ) const + { + if( has_value() ) + { + return std::forward(f)( **this ); + } + else + { + return unexpected(); + } + } }; template inline constexpr bool operator==( expected const & x1, expected const & x2 )