Boost logo

Boost :

From: Bronek Kozicki (brok_at_[hidden])
Date: 2003-12-29 13:31:31


I need to be able to assign auto_ptr from shared_ptr. Here's simplified
sample use:

class M {
  boost::shared_ptr<int> ptr1_;
  boost::shared_ptr<int> ptr2_;
public:
  M() : ptr1_ (boost::shared_ptr<int> (new int))
  {}

  void f()
  {
    ptr2_ = ptr1;
// both ptr1 and ptr2 have use count = 2
  }

  std::auto_ptr<int> source()
  {
    std::auto_ptr<int> ptr3 = ptr1_.release();
// ptr3 is now the only owner of pointer previously contained in ptr1_
// both ptr1_ and ptr2_ have use count = 0 and contain null pointer
// so their end-of-life will not destroy pointed object, and
// end-of-life of ptr3 will not leave ptr1_ and ptr2_ with bad pointer
    return ptr3;
  }
};

M m;
m.f();
// ...
std::auto_ptr<int> p = m.source();
// I need strong ownership semantics here

Lack of such release function is real problem for me. Why it's not
provided in shared_ptr? Is there any workaround I could use?

Thanks

B.


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