Boost logo

Boost :

From: Dan Gohman (dgohman_at_[hidden])
Date: 2002-11-16 16:15:27


I've been using scoped_ptr and scoped_array in several large
projects, and I'm very happy with them.

One feature that they lack that would be useful for me is a set
member function. It would be similar to reset, except that it
would require the stored pointer to be 0. This would allow user
code to more explicitly describe what is happening.

For example, in a constructor, it is not always easy to
initialize scoped_ptr members until the body of the constructor.
Using reset to initialize them works, but reset means ``invoke
delete on the current pointer and then assign it with this new
value''. In the constructor body (and in similar situations)
there would never be an existing object, so a set method would
be more descriptive.

The requirement that the stored pointer be 0 is important
because it ensures that the set function wouldn't compromise
any of the guarantees that scoped_ptr and scoped_array provide.

Here's how it would look in scoped_ptr:

Documentation:

set

void set(T * p); // never throws

Stores a copy of p, which must have been allocated via a C++ new
expression or be 0. Behavior is undefined if the stored pointer
is not 0.

Implementation:

  void set(T * p) // never throws
  {
      BOOST_ASSERT(ptr == 0);

      ptr = p;
  }

Dan

-- 
Dan Gohman
dgohman_at_[hidden]

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