diff --git a/tests/extensions.cpp b/tests/extensions.cpp index 1524612..9eb024a 100644 --- a/tests/extensions.cpp +++ b/tests/extensions.cpp @@ -237,9 +237,14 @@ TEST_CASE("Monadic operations", "[monadic]") { REQUIRE(!o18r); const tl::optional o19 = tl::nullopt; - auto o19r = - std::move(o19).and_then([](int i) { return tl::make_optional(42); }); + auto o19r = std::move(o19).and_then([](int i) { return tl::make_optional(42); }); REQUIRE(!o19r); + + int i = 3; + tl::optional o20{i}; + std::move(o20).and_then([](int& r){return tl::optional{++r};}); + REQUIRE(o20); + REQUIRE(i == 4); } SECTION("constexpr and_then") { diff --git a/tl/optional.hpp b/tl/optional.hpp index cdebc24..35354b3 100644 --- a/tl/optional.hpp +++ b/tl/optional.hpp @@ -1702,11 +1702,11 @@ public: /// \group and_then /// \synopsis template \nconstexpr auto and_then(F &&f) &&; template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { - using result = detail::invoke_result_t; + using result = detail::invoke_result_t; static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } @@ -1725,11 +1725,11 @@ public: /// \group and_then /// \synopsis template \nconstexpr auto and_then(F &&f) const &&; template constexpr auto and_then(F &&f) const && { - using result = detail::invoke_result_t; + using result = detail::invoke_result_t; static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } #endif