From f18103b61702d8056d07a4ff054a06f880e127c8 Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Fri, 29 Mar 2024 02:10:02 +0100 Subject: [PATCH] Further minor tweaks to result<>'s failure type Signed-off-by: Christian Parpart --- src/crispy/result.h | 2 +- src/crispy/result_test.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/crispy/result.h b/src/crispy/result.h index 42dd203549..efe048c349 100644 --- a/src/crispy/result.h +++ b/src/crispy/result.h @@ -19,7 +19,7 @@ class failure constexpr failure(const failure&) = default; constexpr failure(failure&&) noexcept = default; - template + template >> constexpr explicit failure(Err&& e): _value { std::forward(e) } { } diff --git a/src/crispy/result_test.cpp b/src/crispy/result_test.cpp index 803198bba0..65c5c680dd 100644 --- a/src/crispy/result_test.cpp +++ b/src/crispy/result_test.cpp @@ -114,6 +114,20 @@ TEST_CASE("result.or_else") // test const lvalue reference auto const c = b.or_else([](error_code e) -> result { return failure { error_code(int(e) + 1) }; }); REQUIRE(c.error() == error_code::E3); + + // ensure chaining works over rvalues + auto const b2 = result { failure { error_code::E1 } } // E1 + .or_else([](error_code) { return result { 12 }; }) + .or_else([](error_code) { return result { 13 }; }); + REQUIRE(b2.value() == 12); + + // ensure chaining works over lvalues + auto const someError = result { failure { error_code::E1 } }; + auto const c2 = someError // E1 + .or_else([](error_code) { return result { 14 }; }) + .or_else([](error_code) { return result { 15 }; }) + .or_else([](error_code) { return result { 16 }; }); + REQUIRE(c2.value() == 14); } TEST_CASE("result.emplace")