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 15:11:41 +0000
  *
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.



Received on 2025-02-16 15:11:45