From 7e3e8672cdd19b19532de216e3df6fc3bae8ed7a Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 23 Jul 2026 16:50:12 +0200 Subject: kstd: fix some shared_ptr bugs --- libs/kstd/kstd/bits/shared_ptr.hpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'libs') diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp index 0d5e0285..7241487f 100644 --- a/libs/kstd/kstd/bits/shared_ptr.hpp +++ b/libs/kstd/kstd/bits/shared_ptr.hpp @@ -126,18 +126,18 @@ namespace kstd explicit make_shared_control_block(Args &&... args) : shared_ptr_control_block_base{1, 1} { - std::construct_at(std::bit_cast(m_storage.data()), std::forward(args)...); + std::construct_at(reinterpret_cast(m_storage.data()), std::forward(args)...); } [[nodiscard]] auto get_pointer() noexcept -> ObjectType * { - return std::bit_cast(m_storage.data()); + return std::launder(reinterpret_cast(m_storage.data())); } protected: constexpr auto destroy_object() -> void override { - std::destroy_at(std::bit_cast(m_storage.data())); + std::destroy_at(get_pointer()); } private: @@ -229,7 +229,7 @@ namespace kstd //! Construct a weak pointer which shares ownership of the object managed by other. template requires bits::is_shared_pointer_ctor_compatible::value - constexpr weak_ptr(weak_ptr const & other) + constexpr weak_ptr(weak_ptr const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -242,7 +242,7 @@ namespace kstd //! Construct a weak pointer which shares ownership of the object managed by other. template requires bits::is_shared_pointer_ctor_compatible::value - constexpr weak_ptr(shared_ptr const & other) + constexpr weak_ptr(shared_ptr const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -418,7 +418,10 @@ namespace kstd enable_shared_from_this(enable_shared_from_this const &) {} - auto operator=(enable_shared_from_this const &) -> enable_shared_from_this & {} + auto operator=(enable_shared_from_this const &) -> enable_shared_from_this & + { + return *this; + } ~enable_shared_from_this() = default; @@ -542,7 +545,7 @@ namespace kstd * * @param other The shared_ptr to copy from. */ - shared_ptr(shared_ptr const & other) + shared_ptr(shared_ptr const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -560,7 +563,7 @@ namespace kstd */ template requires(std::is_convertible_v) - shared_ptr(shared_ptr const & other) + shared_ptr(shared_ptr const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -732,7 +735,9 @@ namespace kstd * * @return Returns the object owned by *this, equivalent to *get(). */ - [[nodiscard]] auto operator*() const -> T & + template + requires(!std::is_void_v>) + [[nodiscard]] auto operator*() const -> U & { return *m_pointer; } @@ -841,11 +846,10 @@ namespace kstd }; /** - * @brief Specializes the std::swap algorithm for stl::unique_ptr. Swaps the contents of lhs and rhs. Calls - * lhs.swap(rhs). + * Swap the contents of lhs and rhs. * * @tparam T Type of the managed object. - * @param lhs, rhs Smart pointers whose contents to swap. + * @param lhs, rhs Shared pointers whose contents to swap. */ template auto swap(shared_ptr & lhs, shared_ptr & rhs) -> void -- cgit v1.2.3