From af3288263359cdce86f3d0c753961eee9560e4cf Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 23 Jul 2026 00:11:27 +0200 Subject: kstd: add shared_ptr aliasing constructor --- libs/kstd/kstd/bits/shared_ptr.hpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'libs/kstd') diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp index 7af0a00e..8d5dc1a8 100644 --- a/libs/kstd/kstd/bits/shared_ptr.hpp +++ b/libs/kstd/kstd/bits/shared_ptr.hpp @@ -360,7 +360,7 @@ namespace kstd } /** - * @brief Constructor a shared_ptr from a weak_ptr. If other is not expired, constructs a shared_ptr which shares + * @brief Construct a shared_ptr from a weak_ptr. If other is not expired, constructs a shared_ptr which shares * ownership of the object managed by other. Otherwise, constructs an empty shared_ptr. * * @param other The weak_ptr to construct from. @@ -386,6 +386,23 @@ namespace kstd } } + //! Aliasing constructor. + //! + //! @tparam U The type of the object managed by other. + //! @param other The shared_ptr to share ownership with. + //! @param pointer The pointer this shared_ptr should own. + template + requires bits::is_shared_pointer_ctor_compatible::value + shared_ptr(shared_ptr const & other, T * pointer) noexcept + : m_storage{pointer} + , m_control_block{other.m_control_block} + { + if (m_control_block != nullptr) + { + ++(m_control_block->strong_count); + } + } + /** * @brief Copy constructor. * @@ -734,6 +751,12 @@ namespace kstd return shared_ptr(new T(std::forward(args)...)); } + template + [[nodiscard]] auto static_pointer_cast(shared_ptr const & other) noexcept -> shared_ptr + { + return shared_ptr{other, static_cast(other.get())}; + } + /** * @brief Equality operator for shared_ptr. Two shared_ptr instances are equal if they point to the same object * @tparam T, U Types of the managed objects of the shared_ptr instances being compared. -- cgit v1.2.3