From 686dff506d600b84f7e924e81ea1efd3b26f1fb3 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Sat, 3 Aug 2024 15:55:26 +0200 Subject: [PATCH] Made Optional::then() use deducing this so it supports const and non-const optionals. --- source/mijin/container/optional.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/source/mijin/container/optional.hpp b/source/mijin/container/optional.hpp index 6d49780..da39933 100644 --- a/source/mijin/container/optional.hpp +++ b/source/mijin/container/optional.hpp @@ -157,17 +157,16 @@ public: [[nodiscard]] constexpr bool empty() const noexcept { return storage_.empty(); } inline void reset() noexcept; - - template - auto then(TCallable&& onSuccess, TErrorCallable&& onError = 0) + template + auto then(this TSelf&& self, TCallable&& onSuccess, TErrorCallable&& onError = 0) { using result_t = std::invoke_result_t>; if constexpr (std::is_same_v) { - if (!empty()) + if (!self.empty()) { - std::invoke(std::forward(onSuccess), get()); + std::invoke(std::forward(onSuccess), self.get()); } else if constexpr (!std::is_same_v) { @@ -176,9 +175,9 @@ public: } else if constexpr (requires(result_t obj) { {obj == NULL_OPTIONAL} -> std::convertible_to; }) { - if (!empty()) + if (!self.empty()) { - return std::invoke(std::forward(onSuccess), get()); + return std::invoke(std::forward(onSuccess), self.get()); } else {