diff --git a/src/small_unique_ptr.hpp b/src/small_unique_ptr.hpp index af3cb04..b4c3d5e 100644 --- a/src/small_unique_ptr.hpp +++ b/src/small_unique_ptr.hpp @@ -238,8 +238,6 @@ class small_unique_ptr : private detail::small_unique_ptr_base constexpr small_unique_ptr& operator=(small_unique_ptr&& other) noexcept { - if (std::addressof(other) == this) [[unlikely]] return *this; - return operator=(std::move(other)); } @@ -248,6 +246,8 @@ class small_unique_ptr : private detail::small_unique_ptr_base { static_assert(!detail::is_proper_base_of_v || std::has_virtual_destructor_v); + if (std::addressof(other) == this) [[unlikely]] return *this; + if (!other.is_stack_allocated() || std::is_constant_evaluated()) { reset(std::exchange(other.data_, nullptr)); @@ -281,14 +281,17 @@ class small_unique_ptr : private detail::small_unique_ptr_base this->data_ = new_data; } - constexpr void swap(small_unique_ptr& other) noexcept requires(detail::is_always_heap_allocated_v) + constexpr void swap(small_unique_ptr& other) noexcept { - std::swap(this->data_, other.data_); - } - - constexpr void swap(small_unique_ptr& other) noexcept requires(!detail::is_always_heap_allocated_v) - { - if (is_stack_allocated() && other.is_stack_allocated()) + if constexpr (is_always_heap_allocated()) + { + std::swap(this->data_, other.data_); + } + else if (!is_stack_allocated() && !other.is_stack_allocated()) + { + std::swap(this->data_, other.data_); + } + else if (is_stack_allocated() && other.is_stack_allocated()) { const std::ptrdiff_t other_offset = other.offsetof_base(); const std::ptrdiff_t this_offset = this->offsetof_base(); @@ -306,10 +309,6 @@ class small_unique_ptr : private detail::small_unique_ptr_base this->data_ = this->buffer(other_offset); other.data_ = other.buffer(this_offset); } - else if (!is_stack_allocated() && !other.is_stack_allocated()) - { - std::swap(this->data_, other.data_); - } else if (!is_stack_allocated() && other.is_stack_allocated()) { const pointer new_data = this->buffer(other.offsetof_base()); @@ -328,7 +327,7 @@ class small_unique_ptr : private detail::small_unique_ptr_base pointer release() noexcept = delete; [[nodiscard]] - constexpr static bool is_always_heap_allocated() noexcept + static constexpr bool is_always_heap_allocated() noexcept { return detail::is_always_heap_allocated_v; }