Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-09-30 09:47:49


From: "Hillel Y. Sims" <hsims_at_[hidden]>
>
> {
> 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();
> }

vs.

{
    transaction t1 = do_push_back(vector1, e1);
    transaction t2 = do_push_back(vector2, e2);
    transaction t3 = do_push_back(vector3, e3);
// ...
    t1.commit();
    t2.commit();
    t3.commit();
// ...
}

> 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?),

... but at some point the overhead of writing do_push_back is amortized and
do_push_back wins against "raw" ScopeGuard.


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