Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-03-19 11:58:29


From: <Hugo.Duncan_at_[hidden]>
> I am trying to use weak_ptr to maintain information on a heterogeneous
> collection of objects held in shared_ptr's.
>
> As part of this I would like to be able to write the following:
>
> boost::shared_ptr<int> i;
> boost::weak_ptr<void> j;
> i=boost::make_shared(boost::weak_ptr<int>(j));
>
> Would it be possible to add the following overload for the constructor to
enable this usage, or is this considered a mis-use of weak_ptr ?
>
> weak_ptr(weak_ptr<void> const & r): px((element_type*)r.px), pn(r.pn) //
never throws
> {
> }

Allowing implicit conversions from void* to int* (or weak_ptr<void> to
weak_ptr<int>, or shared_ptr<void> to shared_ptr<int>) is dangerous; in
addition, I've removed all weak_ptr casts due to thread safety issues.

Use

boost::shared_ptr<int> i = boost::shared_static_cast<int>(
boost::make_shared(j) );

instead, if you are sure that j points to an 'int'.


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