Boost logo

Boost :

From: Hugo.Duncan_at_[hidden]
Date: 2002-03-19 11:42:26


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
{
 }

Hugo

At the moment this gives a compiler error

Borland: Error E2034 c:\usr\boost\boost/weak_ptr.hpp 45: Cannot convert 'void * const' to 'int *' in function weak_ptr<int>::weak_ptr(const
weak_ptr<void> &)
MSVC6: c:\usr\boost\boost/weak_ptr.hpp(46) : error C2440: 'initializing' : cannot convert from 'void *const ' to 'int *' Conversion from 'void*' to
pointer to non-'void' requires an explicit cast
gcc: c:/usr/boost/boost/weak_ptr.hpp:45: ANSI C++ forbids implicit conversion from `void *' in assignment


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