C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Sun, 16 Feb 2025 16:51:38 +0100
Hi John, how about using a pointer to pointer instead?   -----Ursprüngliche Nachricht----- Von:JOHN MORRISON via Std-Proposals <std-proposals_at_[hidden]> Gesendet:So 16.02.2025 16:11 Betreff:Re: [std-proposals] A non-owning but self zeroing smart pointer for single ownership An:std-proposals_at_[hidden]; CC:JOHN MORRISON <inglesflamenco_at_[hidden]>; * Sebastian Wittmeier wrote: * The described use case would be the following, right? * 1. an object is allocated on the heap pointed to by p1 * 2. a function f is called copying (with arguments) the pointer to p2 * 3. during the run of the function f, f calls some other code; there p1 is freed (ideally p2 is also set to nullptr) * 4. the function f tests p2 for nullptr, if == 0, skip 5 * 5. f dereferences p2 one or multiple times * The solution described would work, if step 3 does not happen between the test in 4 and the dereferencing in 5   Yes that is correct. p1 would be a unique_ptr and p2 would be a ptr_to_unique. With a pure function that has no access to the ownership of p1, p2 can be just a raw pointer. However if is is some kind of framework function that can affect the ownership of p1 then you would pass in p2 as a ptr_to_unique and test it after any action or call that might touch the ownership of p1.   Mainly it is intended for persistent pointers like class members but yes, there are situation where you might use it as an argument type or local variable.     -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-02-16 15:55:56