Boost logo

Boost :

Subject: Re: [boost] Is there any interest in non-owning pointer-like types?
From: Brook Milligan (brook_at_[hidden])
Date: 2017-02-01 10:07:45


> On Feb 1, 2017, at 12:47 AM, Joseph Thomson <joseph.thomson_at_[hidden]> wrote:
>
> The `observer_ptr<T>` class template is a pointer-like type that does not
> do any resource management, and is intended to be used in place of `T*`
> wherever `T*` is used as a non-owning reference to an object of type `T`.
> `observer_ptr<T>` has three main advantages over `T*`:

From my own experience, which includes writing my own version of exactly what you are describing and using it extensively in a large code base, I wholeheartedly agree with your sentiment and would welcome a standardized version of this. Just as std::unique_ptr and std::shared_ptr explicitly document their semantics, so too does observer_ptr, therefore making the code clearer to readers, users, and maintainers. Large code bases are always maintained by more than one person, and unless everyone agrees on the meaning of T* there can be confusion, subtle errors, and maintenance problems. The idea of an observer_ptr class avoids that and enables legacy code to be transformed at will.

If it would be helpful, I would be happy to contribute anything from my own incarnation of an observer_ptr. Perhaps merging ideas would ensure that the API handles use cases well.

Something that you did not mention that I have found useful is a make_observer() function for construction. This is mostly useful to explicitly construct an observer when that helps code clarity.

Given the increased clarity and decreased ambiguity I have seen in my code when using this (isn’t that ultimately the goal of the STL and other designed libraries?), I have always wondered about the antipathy this idea faces, so I hope your proposal makes better progress.

Cheers,
Brook


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk