Subject: Re: [boost] D-style scope guards?
From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2010-09-19 02:59:41


On Friday 17 September 2010 19:56:30 John Bytheway wrote:
> On 17/09/10 11:37, Yakov Galka wrote:
> > BOOST_SCOPE_EXIT(empty capture list not supported yet)
> > cout << "Returning from foo()" << endl;
> > if(!std::uncaught_exception())
> > cout << "foo(): No Errors occurred!" << endl;
> > else
> > cout << "foo(): Exception caught!" << endl;
> > BOOST_SCOPE_EXIT_END
>
> That won't work in general; std::uncaught_exception might return true
> because of another pending exception that's going on 'outside' of foo()
> (e.g. if foo() is called from a destructor).

You could check the function in the constructor of the underlying C++ object,
too, in order to detect transitions.

That said, the killer phrases for me are these:

Martin Christiansson wrote:
> GUARDED_SCOPE_SUCCESS() will only run if the function is exited with
> wrapper for return keyword (GUARDED_SCOPE_RETURN). The wrapper must
> be used since it updates the hidden guard variables. Using plain
> "return" will behave as if exception was thrown.

Having to use a big and ugly macro is too intrusive, signaling failure when
returning normally is just wrong.

Uli