My $0.02.

I think what you are describing is similar to ScopeGuard.

It is somewhat equivalent to "release at destruction unless released previously (or told not to)", though it does both releases automatically (via the resp. destructors.)


I always though of scoped_handle as a more practical and general boost::scoped_ptr<>.

It allows a non-delete release function.

In this sense you would be breaking away from the scoped_ptr interface (not necessarily a bad thing), but it, sort of, goes against the simple spirit of scoped_handle (and scoped_ptr).


I see that it might be useful in various situations, but I think the syntax should be more verbose, no?


Adi


P.S.

C++2x will finally have a proper policy based smart_ptr<> that will support all of these ideas and many more. Yippy... :-P


Matthew Wilson wrote:

I've been mulling over some ideas for scoped_handle, in particular the idea of one instance being able to "take over" the management of a resource being managed by another instance.

Consider:

{
  stlsoft::scoped_handle<void*>  sh1(::malloc(10), ::free);

  if(some-condition)
  {
    stlsoft::scoped_handle<void*>  sh2(&sh1);

  } // if "some-condition", memory is freed here

} // if "!some-condition", memory is freed here

It'd be very simple to implement.

Thoughts?