Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-04-06 06:37:49


From: "Gary Powell" <Gary.Powell_at_[hidden]>

> Ok, I now have a "need" for auto_ptr_new as described by the above.
> Specifically I have
>
> std::pair<boost::any, boost::any>
>
> and in this particular case I have a pair of shared_ptr<string>'s, as a
> member of my class. And as I was trying to write the constructor, I
realized
> that
>
> my_any_pair(shared_ptr<string>(auto_ptr<string>(new string("text") ),
> auto_ptr<string>(new string("text2") );
>
> where I don't really want to have default construction, and then do the
> assignment the "safe" way.

Again, you don't need the auto_ptr. shared_ptr<string>(new string("text"))
is guaranteed not to leak. If you successfully enter the constructor,
shared_ptr takes responsibility for the pointer.

The safe way to write the above is

shared_ptr<string> p1(new string("text"));
shared_ptr<string> p2(new string("text2"));

my_any_pair(p1, p2);

or alternatively you need shared_ptr_new:

my_any_pair(shared_ptr_new<string>("text"),
shared_ptr_new<string>("text2"));

--
Peter Dimov
Multi Media Ltd.

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