From 47e4f60cf1d6cd515e755f8a3ffcd3629ad5d1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Dee=20=28J=C5=8Dshin=29?= Date: Thu, 29 Aug 2024 09:01:51 -0700 Subject: [PATCH] Fix shared_ptr_compatible A shared_ptr is not anonymous, it's just a wrapped void*. --- ctl/shared_ptr.h | 26 +++++++++++++------------- test/ctl/shared_ptr_test.cc | 5 +++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/ctl/shared_ptr.h b/ctl/shared_ptr.h index 076a19d8e..a55fe0cf6 100644 --- a/ctl/shared_ptr.h +++ b/ctl/shared_ptr.h @@ -154,8 +154,8 @@ class shared_emplace : public shared_ref } }; -template -concept shared_ptr_convertible = is_convertible_v || is_void_v; +template +concept shared_ptr_compatible = is_convertible_v; } // namespace __ @@ -175,15 +175,15 @@ class shared_ptr } template - requires is_convertible_v + requires __::shared_ptr_compatible explicit shared_ptr(U* const p) : shared_ptr(p, default_delete()) { } template - requires is_convertible_v + requires __::shared_ptr_compatible shared_ptr(U* const p, D d) - : p(p), rc(__::shared_pointer::make(p, move(d))) + : p(p), rc(__::shared_pointer::make(p, move(d))) { } @@ -203,7 +203,7 @@ class shared_ptr } template - requires __::shared_ptr_convertible + requires __::shared_ptr_compatible shared_ptr(const shared_ptr& r) noexcept : p(r.p), rc(r.rc) { if (rc) @@ -211,7 +211,7 @@ class shared_ptr } template - requires __::shared_ptr_convertible + requires __::shared_ptr_compatible shared_ptr(shared_ptr&& r) noexcept : p(r.p), rc(r.rc) { r.p = nullptr; @@ -231,7 +231,7 @@ class shared_ptr } template - requires is_convertible_v + requires __::shared_ptr_compatible explicit shared_ptr(const weak_ptr& r) : p(r.p), rc(r.rc) { if (r.expired()) { @@ -241,7 +241,7 @@ class shared_ptr } template - requires is_convertible_v + requires __::shared_ptr_compatible shared_ptr(unique_ptr&& r) : p(r.p), rc(__::shared_pointer::make(move(r))) { @@ -260,7 +260,7 @@ class shared_ptr } template - requires __::shared_ptr_convertible + requires __::shared_ptr_compatible shared_ptr& operator=(shared_ptr r) noexcept { shared_ptr(move(r)).swap(*this); @@ -277,14 +277,14 @@ class shared_ptr } template - requires is_convertible_v + requires __::shared_ptr_compatible void reset(U* const p2) { shared_ptr(p2).swap(*this); } template - requires is_convertible_v + requires __::shared_ptr_compatible void reset(U* const p2, D d) { shared_ptr(p2, d).swap(*this); @@ -362,7 +362,7 @@ class weak_ptr constexpr weak_ptr() noexcept = default; template - requires is_convertible_v + requires __::shared_ptr_compatible weak_ptr(const shared_ptr& r) noexcept : p(r.p), rc(r.rc) { if (rc) diff --git a/test/ctl/shared_ptr_test.cc b/test/ctl/shared_ptr_test.cc index 312f1d167..c9f9f0516 100644 --- a/test/ctl/shared_ptr_test.cc +++ b/test/ctl/shared_ptr_test.cc @@ -140,6 +140,11 @@ main() shared_ptr y(x); } + { + // You can also create a shared pointer to void in the first place. + shared_ptr x(new int); + } + { // You can take a shared pointer to a subobject, and it will free the // base object.