Date: Sat, 23 Aug 2025 12:08:44 +0200
> On Aug 23, 2025, at 11:00 AM, organicoman <organicoman_at_[hidden]> wrote:
>
> unique_ptr and shared_ptr have two distinct paradigms .
> 1-
> unique_ptr moves the ownership totally, and to get back ownership , the callee need to return a unique_ptr
> {
> unique_ptr p=...;
> //assuming callee1 returns a unique_ptr
> p = move(callee1(move(p)));
> }
> BUT!!!, By the time callee1 returns, we don't know if the original resource or different resource is returned.
You already suggested to only use references to the owning/original pointer. Then just use a reference to unique_ptr instead. This would mean only a single unique_ptr holds ownership.
> 2-
> shared_ptr models sharing ownership, but only the counter decides when to free the resource.
That’s why I suggested weak_ptr in addition: There can be just a single shared_ptr instance and all the others are weak_ptr (though they shortly need to become shared_ptrs to become usable). The weak_ptr will know when the shared_ptr was freed.
>
> With the proposal + 'passing by reference' guideline, we are modeling a paradigm, of allocate in one location, free from anywhere and all are notified.
As I said: use references to unique_ptr. Any reference can then free the memory and every other reference to the same unique_ptr will immediately know. It is exactly what you are proposing (just with a different syntax). I would personally count this as against the intent of unique_ptr, but it should work nevertheless. By experimenting with this you can gather some valuable insight how helpful your proposal actually would be. The committee likes to have some experience with new features before adopting them.
>
> But definitely, if it can be done by a compiler flag then it does the job.
I don’t think any compiler has a flag like this, but you could try for gcc or clang to implement it. Also, somebody else suggested using macros that do delete plus nullifying. This could also help gather some experience with your approach.
>
> unique_ptr and shared_ptr have two distinct paradigms .
> 1-
> unique_ptr moves the ownership totally, and to get back ownership , the callee need to return a unique_ptr
> {
> unique_ptr p=...;
> //assuming callee1 returns a unique_ptr
> p = move(callee1(move(p)));
> }
> BUT!!!, By the time callee1 returns, we don't know if the original resource or different resource is returned.
You already suggested to only use references to the owning/original pointer. Then just use a reference to unique_ptr instead. This would mean only a single unique_ptr holds ownership.
> 2-
> shared_ptr models sharing ownership, but only the counter decides when to free the resource.
That’s why I suggested weak_ptr in addition: There can be just a single shared_ptr instance and all the others are weak_ptr (though they shortly need to become shared_ptrs to become usable). The weak_ptr will know when the shared_ptr was freed.
>
> With the proposal + 'passing by reference' guideline, we are modeling a paradigm, of allocate in one location, free from anywhere and all are notified.
As I said: use references to unique_ptr. Any reference can then free the memory and every other reference to the same unique_ptr will immediately know. It is exactly what you are proposing (just with a different syntax). I would personally count this as against the intent of unique_ptr, but it should work nevertheless. By experimenting with this you can gather some valuable insight how helpful your proposal actually would be. The committee likes to have some experience with new features before adopting them.
>
> But definitely, if it can be done by a compiler flag then it does the job.
I don’t think any compiler has a flag like this, but you could try for gcc or clang to implement it. Also, somebody else suggested using macros that do delete plus nullifying. This could also help gather some experience with your approach.
Received on 2025-08-23 10:08:58