aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/kstd/bits/shared_ptr.hpp1
-rw-r--r--libs/kstd/kstd/bits/shared_ptr.tests.cpp61
2 files changed, 61 insertions, 1 deletions
diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp
index 1171d7fe..34332ace 100644
--- a/libs/kstd/kstd/bits/shared_ptr.hpp
+++ b/libs/kstd/kstd/bits/shared_ptr.hpp
@@ -392,7 +392,6 @@ namespace kstd
//! @param other The shared_ptr to share ownership with.
//! @param pointer The pointer this shared_ptr should own.
template<typename Y>
- requires bits::is_shared_pointer_ctor_compatible<T *, Y *>::value
shared_ptr(shared_ptr<Y> const & other, T * pointer) noexcept
: m_storage{pointer}
, m_control_block{other.m_control_block}
diff --git a/libs/kstd/kstd/bits/shared_ptr.tests.cpp b/libs/kstd/kstd/bits/shared_ptr.tests.cpp
new file mode 100644
index 00000000..cf63e0aa
--- /dev/null
+++ b/libs/kstd/kstd/bits/shared_ptr.tests.cpp
@@ -0,0 +1,61 @@
+#include <kstd/bits/shared_ptr.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+namespace
+{
+
+ struct base // NOLINT(cppcoreguidelines-virtual-class-destructor)
+ {
+ [[nodiscard]] virtual auto id() const noexcept -> int
+ {
+ return 1;
+ }
+ };
+
+ struct derived final : base
+ {
+ [[nodiscard]] auto id() const noexcept -> int override
+ {
+ return 2;
+ }
+ };
+
+} // namespace
+
+SCENARIO("Shared pointer casts", "[kstd][shared_ptr]")
+{
+ GIVEN("a shared_ptr<derived>")
+ {
+ auto derived_ptr = kstd::make_shared<derived>();
+
+ WHEN("casting it to a shared_ptr<base> via static_pointer_cast")
+ {
+ auto base_ptr = kstd::static_pointer_cast<base>(derived_ptr);
+
+ THEN("both pointer observer the same, still-derived object")
+ {
+ REQUIRE(base_ptr.get() == static_cast<base *>(derived_ptr.get()));
+ REQUIRE(base_ptr->id() == 2);
+ }
+
+ THEN("ownership is shared")
+ {
+ REQUIRE(derived_ptr.use_count() == 2);
+ REQUIRE(base_ptr.use_count() == 2);
+ }
+
+ AND_WHEN("the original shared pointer is reset")
+ {
+ derived_ptr.reset();
+
+ THEN("the object is kept alive by the aliased base shared pointer")
+ {
+ REQUIRE(base_ptr);
+ REQUIRE(base_ptr->id() == 2);
+ REQUIRE(base_ptr.use_count() == 1);
+ }
+ }
+ }
+ }
+}