Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2001-10-18 02:42:02


on 10/18/01 12:24 AM, Darin Adler at darin_at_[hidden] wrote:

> scoped_ptr<T> sp(p);
> pn = new long(1); // may throw
> --*old_pn;
> px = sp.release();

Oops. There's no such thing as scoped_ptr::release. Well, rather than open
that can of worms, another version:

   void reset(T* p=0) {
      if (px == p) return; // fix: self-assignment safe
      if (*pn == 1) {
         T* old_px = px;
         px = p;
         checked_array_delete(old_px);
      }
      else { // allocate new reference counter
         long* old_pn = pn;
         try { pn = new long(1); }
         catch (...) {
           checked_delete(p);
           throw;
         }
         --*old_pn;
         px = p;
      } // allocate new reference counter
   } // reset

I think the scoped_ptr one is slightly more elegant, but I was dissuaded
from adding a scoped_ptr::release by the smart pointer documentation.

    -- Darin


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