Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-07-19 05:15: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
> 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.

> 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

It might have to be renamed... make_strong_reference? Too long. make_strong?
Too stupid. :-) Ideas?

Boost list run by bdawes at, gregod at, cpdaniel at, john at