From 8034ce643d36e8cbe4c4d6bc9e154aaf2eb08597 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 26 Feb 2025 00:14:00 +0100 Subject: Use deducing-this in some places --- asl/types/maybe_uninit.hpp | 8 +++-- asl/types/maybe_uninit_tests.cpp | 5 +++ asl/types/option.hpp | 77 +++++++++++----------------------------- asl/types/status_or.hpp | 20 +++-------- 4 files changed, 35 insertions(+), 75 deletions(-) (limited to 'asl') diff --git a/asl/types/maybe_uninit.hpp b/asl/types/maybe_uninit.hpp index 9134fe9..36429e4 100644 --- a/asl/types/maybe_uninit.hpp +++ b/asl/types/maybe_uninit.hpp @@ -64,9 +64,11 @@ public: } // @Safety Value must have been initialized - constexpr const T& as_init_unsafe() const& { return m_value; } - constexpr T& as_init_unsafe() & { return m_value; } - constexpr T&& as_init_unsafe() && { return ASL_MOVE(m_value); } + template + constexpr auto&& as_init_unsafe(this Self&& self) + { + return ASL_FWD_LIKE(decltype(self), ASL_FWD(self).m_value); + } }; } // namespace asl diff --git a/asl/types/maybe_uninit_tests.cpp b/asl/types/maybe_uninit_tests.cpp index 4b414cd..16786a7 100644 --- a/asl/types/maybe_uninit_tests.cpp +++ b/asl/types/maybe_uninit_tests.cpp @@ -20,3 +20,8 @@ TEST_TYPE_PROPERTIES(trivially_copy_assignable); TEST_TYPE_PROPERTIES(trivially_move_assignable); TEST_TYPE_PROPERTIES(trivially_destructible); +static_assert(asl::same_as&>().as_init_unsafe())>); +static_assert(asl::same_as&&>().as_init_unsafe())>); +static_assert(asl::same_as&>().as_init_unsafe())>); +static_assert(asl::same_as>().as_init_unsafe())>); +static_assert(asl::same_as>().as_init_unsafe())>); diff --git a/asl/types/option.hpp b/asl/types/option.hpp index 5cbb759..e79c09c 100644 --- a/asl/types/option.hpp +++ b/asl/types/option.hpp @@ -395,70 +395,35 @@ public: return value(); } - template - constexpr auto and_then(F&& f) & - requires is_option> - { - if (has_value()) - { - return invoke(ASL_FWD(f), value()); - } - return un_cvref_t>{}; - } - - template - constexpr auto and_then(F&& f) const& - requires is_option> - { - if (has_value()) - { - return invoke(ASL_FWD(f), value()); - } - return un_cvref_t>{}; - } - - template - constexpr auto and_then(F&& f) && - requires is_option> - { - if (has_value()) - { - return invoke(ASL_FWD(f), ASL_MOVE(value())); - } - return un_cvref_t>{}; - } - - template - constexpr auto transform(F&& f) & - { - using U = un_cvref_t>; - if (has_value()) - { - return option{ invoke(ASL_FWD(f), value()) }; - } - return option{}; - } - - template - constexpr auto transform(F&& f) const& + template< + typename F, + typename Self, + typename Result = result_of_t)> + > + constexpr auto and_then(this Self&& self, F&& f) + requires is_option { - using U = un_cvref_t>; - if (has_value()) + if (self.has_value()) { - return option{ invoke(ASL_FWD(f), value()) }; + return invoke(ASL_FWD(f), ASL_FWD_LIKE(decltype(self), ASL_FWD(self).value())); } - return option{}; + return Result{ asl::nullopt }; } - template - constexpr auto transform(F&& f) && + template< + typename F, + typename Self, + typename Result = result_of_t)> + > + constexpr auto transform(this Self&& self, F&& f) { - using U = un_cvref_t>; - if (has_value()) + if (self.has_value()) { - return option{ invoke(ASL_FWD(f), ASL_MOVE(value())) }; + return option>{ + invoke(ASL_FWD(f), ASL_FWD_LIKE(decltype(self), ASL_FWD(self).value())) + }; } - return option{}; + return option>{ asl::nullopt }; } template diff --git a/asl/types/status_or.hpp b/asl/types/status_or.hpp index 07cfd7f..5771d58 100644 --- a/asl/types/status_or.hpp +++ b/asl/types/status_or.hpp @@ -129,23 +129,11 @@ public: constexpr status&& throw_status() && { return ASL_MOVE(m_status); } - // @Todo(C++23) Deducing this - constexpr const T& value() const& + template + constexpr auto&& value(this Self&& self) { - ASL_ASSERT_RELEASE(ok()); - return m_value.as_init_unsafe(); - } - - constexpr T& value() & - { - ASL_ASSERT_RELEASE(ok()); - return m_value.as_init_unsafe(); - } - - constexpr T&& value() && - { - ASL_ASSERT_RELEASE(ok()); - return ASL_MOVE(m_value.as_init_unsafe()); + ASL_ASSERT_RELEASE(self.ok()); + return ASL_FWD(self).m_value.as_init_unsafe(); } template -- cgit