Correctly implement weak_ptr assignment/copy/moves (#1399)

This commit is contained in:
Steven Dee (Jōshin) 2025-04-17 14:01:20 -07:00 committed by GitHub
parent fbc4fcbb71
commit 66d1050af6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<typename U>
requires __::shared_ptr_compatible<T, U>
weak_ptr(weak_ptr<U> 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<typename U>
requires __::shared_ptr_compatible<T, U>
weak_ptr(weak_ptr<U>&& 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<T> lock() const noexcept
{
if (expired())