Boost logo

Boost :

From: Jason Stewart (jstewart_at_[hidden])
Date: 2005-09-22 14:54:31


I sent this earlier but it never showed up on the list.

> > By the way, that might work well for smart
> > pointers; why don't shared_ptr and friends use this idiom?
>
>Maybe because it allows more than a literal zero?

I personally like the guard/dismiss names, they are reminiscent of
commands you would give a real guard.

However, maybe using the shared_ptr semantics would work. I.e. if you
can create a guard that is "uninitialized" and then reset it later to
an active guard.

{
         scope_guard g;

         if (some condition)
         {
                 g = scope_guard(my_undo_function);
                 // do something here
         }

         if (some other condition)
         {
                 // we changed our mind, dismiss it.
                 g = scope_guard;

                 // or ...
                 // g.dismiss();
                 // g.reset(); // like shared_ptr
         }
}

It seems to me that is as easy to read as having an explicit "guard"
or "activate" command is. I confess though that I didn't follow the
early discussion closely so I apologize is this has been discussed already.

Jason Stewart


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