Boost logo

Boost :

Subject: Re: [boost] [interest] rich-typed smart pointers
From: Jeremy Maitin-Shepard (jeremy_at_[hidden])
Date: 2013-10-02 18:10:02


Julian Gonggrijp <j.gonggrijp <at> gmail.com> writes:

> I have designed a smart pointer that relies on the type system to
> offer safe and flexible automatic memory management at no runtime
> cost. It combines single ownership as in std::unique_ptr and reliable
> access from multiple places as in std::shared_ptr with some additional
> features. I believe it to be about as safe and practically useful as a
> smart pointer in C++11 could get.

>From reading the documentation, it seems that owner_ptr/data_ptr are
basically equivalent in functionality to std::unique_ptr, and weak_ptr is
basically equivalent to a raw pointer/raw reference. (Referring to
std::shared_ptr seems a bit misleading given that you don't address the
problem of multiple ownership.) As I understand it, the goal of your
library is not to add any functionality over std::unique_ptr, but to provide
a little bit more compile-time safety at the cost of some additional typing
(pun intended). It does seem that wrappers around unique_ptr and shared_ptr
that disable default construction and discourage storing a null pointer
would be useful for many people. It would be much more useful if they
explicitly interoperated with those types, though.

I think the primary use case for your proposed weak_ptr (possibly renamed
param_ptr or arg_ptr) would be as the declared type for function parameters.
 When used only in that context, the implicit conversion from an owning
pointer to a raw pointer is safe, because any values used as function
arguments are guaranteed to live for the duration of the function.

For any other use, though, the implicit conversion is actually dangerous,
and I would say that the warning signal that syntax like:

T *x = foo.get();

or

T &x = *foo;

provides is helpful.


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