Boost logo

Boost :

From: Steven Kirk (steven.kirk_at_[hidden])
Date: 2002-07-19 08:38:54


> From: "Steven Kirk" <steven.kirk_at_[hidden]>
> > The new(ish) weak_ptr has the template function make_shared, would it be
a
> > good idea to create a make_shared function for creation of a shared_ptr
> from
> > a raw pointer?
> >
> > This would make code slightly easier to read IMHO:
> >
> > return boost::shared_ptr<some_class>(new some_class(1));
> >
> > would become
> >
> > return boost::make_shared(new some_class(1));
>
> The idiomatic way to create a shared_ptr is to always use a named
variable:
>
> shared_ptr<some_class> p(new some_class(1));
> return p;
>
> This simple (to teach and enforce) rule helps in situations like:
>
> f( shared_ptr<T>(new T), g() );
>
> where the 'new T' may leak if g() throws.
>
> shared_ptr temporaries are best avoided.
>

Ah, I see. Maybe it would be a good idea to add this to the shared_ptr docs?
I mean, explicitly point out this potential pitfall and describe how it
works - I was certainly unaware of this, and I'm not entirely sure I
understand the details even now!

> > This would serve a similar purpose to the standard library's make_pair
> > function for example.
>
> You have a point. make_shared is not the best name for the current
function,
> since it sounds like a make_X helper, although its semantics are very
> different.
>
> It might have to be renamed... make_strong_reference? Too long.
make_strong?
> Too stupid. :-) Ideas?
>

I'm not sure, I did mistake make_shared for a make_X helper, but I'm not
sure
that's a big problem.


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