From 66d1050af64c175f232d5bcb643741662fa1477f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Dee=20=28J=C5=8Dshin=29?= Date: Thu, 17 Apr 2025 14:01:20 -0700 Subject: [PATCH] Correctly implement weak_ptr assignment/copy/moves (#1399) --- ctl/shared_ptr.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ctl/shared_ptr.h b/ctl/shared_ptr.h index c387f2198..78cf4aeb2 100644 --- a/ctl/shared_ptr.h +++ b/ctl/shared_ptr.h @@ -382,6 +382,34 @@ class weak_ptr rc->keep_weak(); } + weak_ptr(weak_ptr const& r) noexcept : p(r.p), rc(r.rc) + { + if (rc) + rc->keep_weak(); + } + + template + requires __::shared_ptr_compatible + weak_ptr(weak_ptr const& r) noexcept : p(r.p), rc(r.rc) + { + if (rc) + rc->keep_weak(); + } + + weak_ptr(weak_ptr&& r) noexcept : p(r.p), rc(r.rc) + { + r.p = nullptr; + r.rc = nullptr; + } + + template + requires __::shared_ptr_compatible + weak_ptr(weak_ptr&& r) noexcept : p(r.p), rc(r.rc) + { + r.p = nullptr; + r.rc = nullptr; + } + ~weak_ptr() { if (rc) @@ -410,6 +438,12 @@ class weak_ptr swap(rc, r.rc); } + weak_ptr& operator=(weak_ptr r) noexcept + { + swap(r); + return *this; + } + shared_ptr lock() const noexcept { if (expired())