
On Thu, 22 Sep 2005 15:54:31 -0400, Jason Stewart <jstewart@pobox.com> wrote:
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.
I don't think this will work well, because the guard could be fairly complex. I'd hate to have several identical multi-line guard statements in one function. -- Be seeing you.