C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A non-owning but self zeroing smart pointer for single ownership

From: JOHN MORRISON <inglesflamenco_at_[hidden]>
Date: Sun, 16 Feb 2025 20:48:33 +0000
Jereny Rifkin wrote: As others have pointed out, weak pointers are the tool to use here. I think
it's very hard to make an argument for some sort of weak pointer to unique
(which implies a system that only works with some unique_pointers or an
entirely new unique_ptr type that always has a control block) when you can
just use shared pointers.

Hi Jeremy,

I did consider using shared_ptr/weak_ptr just to get that safe testable secondary reference. But no, it is not a good way to solve the problem. It requires a switch from single ownership to shared ownership and that is very often expressly not wanted. Single ownership is valued for many reasons and trading it for shared ownership is often unacceptable or a complete misrepresentation of the situation being modelled with messy consequences.

There is no new kind of unique_ptr involved here. You just add a deleter hook to any unique_ptrs that you want to reference with ptr_to_unique when you need to. It doesn't turns the unique_ptr into something else, it just intercepts deletions so it can notify a control block.

There is a fear that ptr_to_unique may result in new undefined behaviour because it can't acquire ownership to keep something alive when its lifetime is vulnerable to the action of another thread. This fear is unfounded because we already have it clear that singly owned objects must never be visible to another thread. See my previous post on this Re: A non-owning but self zeroing smart pointer for single ownership<https://lists.isocpp.org/std-proposals/2025/02/12468.php>




Received on 2025-02-16 20:48:35