From edfdf3e6894a960351421ef978c9e1b0654006b2 Mon Sep 17 00:00:00 2001 From: Simon Brand Date: Mon, 25 Feb 2019 11:51:54 +0000 Subject: [PATCH] Move error inside and_then Fixes #49 --- tests/issues.cpp | 21 ++++++++++++++++++++- tl/expected.hpp | 8 ++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/issues.cpp b/tests/issues.cpp index b271140..9aa84ad 100644 --- a/tests/issues.cpp +++ b/tests/issues.cpp @@ -105,4 +105,23 @@ TEST_CASE("Issue 42", "[issues.42]") { TEST_CASE("Issue 43", "[issues.43]") { auto result = tl::expected{}; result = tl::make_unexpected(std::string{ "foo" }); -} \ No newline at end of file +} + +#include + + using MaybeDataPtr = tl::expected>; + + MaybeDataPtr test(int i) noexcept + { + return std::move(i); + } + + MaybeDataPtr test2(int i) noexcept + { + return std::move(i); + } + +TEST_CASE("Issue 49", "[issues.49]") { + auto m = test(10) + .and_then(test2); +} diff --git a/tl/expected.hpp b/tl/expected.hpp index e16058d..2042c51 100644 --- a/tl/expected.hpp +++ b/tl/expected.hpp @@ -1910,7 +1910,7 @@ constexpr auto and_then_impl(Exp &&exp, F &&f) { return exp.has_value() ? detail::invoke(std::forward(f), *std::forward(exp)) - : Ret(unexpect, exp.error()); + : Ret(unexpect, std::forward(exp).error()); } template ::value, "F must return an expected"); return exp.has_value() ? detail::invoke(std::forward(f)) - : Ret(unexpect, exp.error()); + : Ret(unexpect, std::forward(exp).error()); } #else template struct TC; @@ -1933,7 +1933,7 @@ auto and_then_impl(Exp &&exp, F &&f) -> Ret { return exp.has_value() ? detail::invoke(std::forward(f), *std::forward(exp)) - : Ret(unexpect, exp.error()); + : Ret(unexpect, std::forward(exp).error()); } template Ret { static_assert(detail::is_expected::value, "F must return an expected"); return exp.has_value() ? detail::invoke(std::forward(f)) - : Ret(unexpect, exp.error()); + : Ret(unexpect, std::forward(exp).error()); } #endif