Boost logo

Boost :

Subject: Re: [boost] [exception] uncaught_exception_count, scope(failure), scope(success)
From: Evgeny Panasyuk (evgeny.panasyuk_at_[hidden])
Date: 2013-02-05 18:25:41

06.02.2013 2:57, Emil Dotchevski:

> I mean that in D/Java/C#/etc you don't know when the destructor will
> get called automatically, which means that you're stuck with having to
> manually keep track of all allocated resources exactly like in C,
> except for memory.

scope(exit) (in C++) has similar behaviour to normal destructors, and in
fact it is implemented on top of destructors.
scope(failure) and scope(success) differs from scope(exit) only by
additional condition - code in scope(failure) is called only when scope
is exited by exception, and code in scope(success) is called only during
"normal" scope exit.

D's documentation says ( ) :
"The ScopeGuardStatement executes NonEmptyOrScopeBlockStatement at the
close of the current scope, rather than at the point where the
ScopeGuardStatement appears."

Regarding C# and Java: they have some "workarounds" for resource
management: C# has "using", and Java has "try-with-resources".

Regarding C: there are also approaches to manage resources
automatically, but that's another story.

>> While scope(exit) is some kind of ad-hoc replacement for RAII wrappers,
>> scope(failure) and scope(success) are not.
>> Nowadays scope(failure) and scope(success) are emulated in C++ via
>> ScopeGuard idiom.
> Could you clarify what does uncaught_exceptions_count() use look like?
> How is it different scope guard?

For instance, check first example at Boost.ScopeExit documentation
(C++98 syntax):

void world::add_person(person const& a_person) {
    bool commit = false;

    BOOST_SCOPE_EXIT(&commit, &persons_) {

    // ...

    commit = true;

Using SCOPE_FAILURE it would became (C++98 syntax):

void world::add_person(person const& a_person) {
    SCOPE_FAILURE(&persons_) {

    // ...

Also, there are side-by-side examples at .

Evgeny Panasyuk

Boost list run by bdawes at, gregod at, cpdaniel at, john at