Boost logo

Boost :

From: Hillel Y. Sims (hsims_at_[hidden])
Date: 2002-09-27 17:40:05


"David Abrahams" <dave_at_[hidden]> wrote in message
news:050a01c26642$032f6e70$6501a8c0_at_boostconsulting.com...
>
> I don't really like scopeguard-like things for such jobs, since the danger
> of the above is that you have to remember to throw;, while you need to
> remember to cancel the scopeguard. It's an even trade. Of course if you
> factor in Mr. Terekhov's favorite issue (weird entaglements with
> not-really-C++ platform "exceptions"), the scopeguard might be a better
> solution.
>
>

{
     vector1.push_back(e1);
     try {
         vector2.push_back(e2):
     }
     catch(...) {
         vector1.pop_back();
         throw;
    }
}

vs.

{
    vector1.push_back(e1);
    ScopeGuard guard =
MakeObjScopeGuard(vector1,{typeof(vector1)}::pop_back);
    vector2.push_back(e2);
    guard.Dismiss();
}

The ScopeGuard version is 1) fewer lines of code, 2) easier to test -- if
you forget to Dismiss(), you'll find out immediately during ordinary
testing, vs. missing the throw; in more difficult to test (possibly even
untested?) exception-handling code, 3) scales better (what if you have
three/four/etc. updates to synchronize?), 4) "not-really-C++" all you like,
but still exists as a real-world issue, especially if you are trying to
write safe portable code (besides, they could be implemented as "really-C++"
exceptions on a given platform too).

hys

--
Hillel Y. Sims
FactSet Research Systems
hsims AT factset.com

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