Made Optional::then() use deducing this so it supports const and non-const optionals.

This commit is contained in:
Patrick 2024-08-03 15:55:26 +02:00
parent 7658e8fbda
commit 686dff506d

View File

@ -157,17 +157,16 @@ public:
[[nodiscard]] constexpr bool empty() const noexcept { return storage_.empty(); }
inline void reset() noexcept;
template<typename TCallable, typename TErrorCallable = int>
auto then(TCallable&& onSuccess, TErrorCallable&& onError = 0)
template<typename TSelf, typename TCallable, typename TErrorCallable = int>
auto then(this TSelf&& self, TCallable&& onSuccess, TErrorCallable&& onError = 0)
{
using result_t = std::invoke_result_t<TCallable, std::add_rvalue_reference_t<TValue>>;
if constexpr (std::is_same_v<result_t, void>)
{
if (!empty())
if (!self.empty())
{
std::invoke(std::forward<TCallable>(onSuccess), get());
std::invoke(std::forward<TCallable>(onSuccess), self.get());
}
else if constexpr (!std::is_same_v<TErrorCallable, int>)
{
@ -176,9 +175,9 @@ public:
}
else if constexpr (requires(result_t obj) { {obj == NULL_OPTIONAL} -> std::convertible_to<bool>; })
{
if (!empty())
if (!self.empty())
{
return std::invoke(std::forward<TCallable>(onSuccess), get());
return std::invoke(std::forward<TCallable>(onSuccess), self.get());
}
else
{