
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.
Since in current implementation scope_guard is a typedefed reference the example above will not work. Besides, I don't really like the idea to create the whole object just to change the status. In addition, the code readability suffers.