Boost logo

Boost :

Subject: Re: [boost] D-style scope guards?
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2011-03-05 21:42:39


On 3/5/2011 4:24 PM, Lorenzo Caminiti wrote:
[...]
> 1. SCOPE EXIT
>
> First of all, note that Boost.ScopeExit code (and therefore,
> Boost.Local exit code) is *not* executed when throwing an exception:
>
> #include<boost/scope_exit.hpp>
> #include<iostream>
> #include<stdexcept>
>
> void f() {
> bool error = false;
>
> BOOST_SCOPE_EXIT( (&error) ) {
> std::cout<< "returning"<< std::endl; // Not executed on throw.
> } BOOST_SCOPE_EXIT_END
>
> throw std::runtime_error("some error");
> }
>
> int main() {
> f();
> return 0;
> }
>
> This will *not* print "returning" because the throw terminates `f()`
> *without* executing local variables' destructors (which execute the
> scope exit code).
[...]

Wait...I thought that was the point of Boost.ScopeExit? Aren't the
destructors of local variables always invoked during stack unwinding?

- Jeff


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