diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-02-26 00:14:00 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-02-26 00:14:00 +0100 |
commit | 8034ce643d36e8cbe4c4d6bc9e154aaf2eb08597 (patch) | |
tree | bd8b8078347aeb7604935fffa9423d575f45cf0e /asl | |
parent | 8b112960586fce4aad791f9cf52c94a70b476ce0 (diff) |
Use deducing-this in some places
Diffstat (limited to 'asl')
-rw-r--r-- | asl/types/maybe_uninit.hpp | 8 | ||||
-rw-r--r-- | asl/types/maybe_uninit_tests.cpp | 5 | ||||
-rw-r--r-- | asl/types/option.hpp | 77 | ||||
-rw-r--r-- | asl/types/status_or.hpp | 20 |
4 files changed, 35 insertions, 75 deletions
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<typename Self> + 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<int&, decltype(asl::declval<asl::maybe_uninit<int>&>().as_init_unsafe())>); +static_assert(asl::same_as<int&&, decltype(asl::declval<asl::maybe_uninit<int>&&>().as_init_unsafe())>); +static_assert(asl::same_as<const int&, decltype(asl::declval<const asl::maybe_uninit<int>&>().as_init_unsafe())>); +static_assert(asl::same_as<const int&&, decltype(asl::declval<const asl::maybe_uninit<int>>().as_init_unsafe())>); +static_assert(asl::same_as<int&&, decltype(asl::declval<asl::maybe_uninit<int>>().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<typename F> - constexpr auto and_then(F&& f) & - requires is_option<result_of_t<F(T&)>> - { - if (has_value()) - { - return invoke(ASL_FWD(f), value()); - } - return un_cvref_t<result_of_t<F(T&)>>{}; - } - - template<typename F> - constexpr auto and_then(F&& f) const& - requires is_option<result_of_t<F(const T&)>> - { - if (has_value()) - { - return invoke(ASL_FWD(f), value()); - } - return un_cvref_t<result_of_t<F(const T&)>>{}; - } - - template<typename F> - constexpr auto and_then(F&& f) && - requires is_option<result_of_t<F(T)>> - { - if (has_value()) - { - return invoke(ASL_FWD(f), ASL_MOVE(value())); - } - return un_cvref_t<result_of_t<F(T)>>{}; - } - - template<typename F> - constexpr auto transform(F&& f) & - { - using U = un_cvref_t<result_of_t<F(T&)>>; - if (has_value()) - { - return option<U>{ invoke(ASL_FWD(f), value()) }; - } - return option<U>{}; - } - - template<typename F> - constexpr auto transform(F&& f) const& + template< + typename F, + typename Self, + typename Result = result_of_t<F(copy_cref_t<Self&&, T>)> + > + constexpr auto and_then(this Self&& self, F&& f) + requires is_option<Result> { - using U = un_cvref_t<result_of_t<F(const T&)>>; - if (has_value()) + if (self.has_value()) { - return option<U>{ invoke(ASL_FWD(f), value()) }; + return invoke(ASL_FWD(f), ASL_FWD_LIKE(decltype(self), ASL_FWD(self).value())); } - return option<U>{}; + return Result{ asl::nullopt }; } - template<typename F> - constexpr auto transform(F&& f) && + template< + typename F, + typename Self, + typename Result = result_of_t<F(copy_cref_t<Self&&, T>)> + > + constexpr auto transform(this Self&& self, F&& f) { - using U = un_cvref_t<result_of_t<F(T)>>; - if (has_value()) + if (self.has_value()) { - return option<U>{ invoke(ASL_FWD(f), ASL_MOVE(value())) }; + return option<un_cvref_t<Result>>{ + invoke(ASL_FWD(f), ASL_FWD_LIKE(decltype(self), ASL_FWD(self).value())) + }; } - return option<U>{}; + return option<un_cvref_t<Result>>{ asl::nullopt }; } template<typename F> 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<typename Self> + 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<typename U> |