Boost logo

Boost :

Subject: Re: [boost] [scope_exit] D-style scope(failure) and scope(success) in C++
From: Evgeny Panasyuk (evgeny.panasyuk_at_[hidden])
Date: 2013-09-29 17:23:46


27.09.2013 3:24, Emil Dotchevski:
> Is there a use case for uncaught_exception_count other than to
> implement D-style scope-exit/-failure?

scope(failure)/scope(success) would be main use case, however - there
are several others:

1. scope(failure/success) runs specified code block. Sometimes such code
block can be reused - in that case custom guard object is preferred over
copy-pasting code.

2. Temporary objects:
log() << may_throw() << endl;
log() returns some temporary object which does some finalizing work in
destructor. If that destructor is called due to stack unwinding - then
finalizing part will be different.
I think Andrey Semashev has something similar at Boost.Log.

3. Stack objects which do work in destructor that may fail.
Traditional example is File object which requires flush(may fail) and
release handle at the end of lifetime.
Typical solution is to place .flush() manually and release handle in
destructor:
{
    File a,b;
    // ...
    b.flush(); // may throw
    a.flush(); // may throw
}
If b.flush() will throw then a.flush() will not happen but only release
of file handle at a.~File().
Same effect can be achieved automatically using
uncaught_exception_count/unwinding_indicator:
{
    File a,b;
    // ...
    // conditional flush of b and a in destructors.
}

-- 
Evgeny Panasyuk

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