Boost logo

Boost :

Subject: Re: [boost] D-style scope guards?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-03-06 10:05:41


On Sun, Mar 6, 2011 at 6:10 AM, Eric Niebler <eric_at_[hidden]> wrote:
> On 3/6/2011 9:42 AM, Jeffrey Lee Hellrung, Jr. wrote:
>> On 3/5/2011 4:24 PM, Lorenzo Caminiti wrote:
>>>
>>> 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?
>
> Not if main exits via an uncaught exception. Then all bets are off.

Yes, of course this is true (and it doesn't take anything away from
the usefulness of Boost.ScopeExit or Boost.Local exits). However, the
point is more about how D works than how C++ works because the
original author of this email thread was trying to emulate D-style
scope guards.

I am not a D programmer (so help me here :) ), but I tried this code
and it looked like D executes the scope exit even when main() doesn't
catch:

import std.stdio;

void f() {
    scope(exit) {
        writeln("returning");
    }

    throw new Exception("some error");
}

int main() { // Doesn't catch Exception but still prints "returning"
(differently from C++ when using ScopeExit or Local exits).
    f();
    return 0;
}

E:\sandbox\boost-sandbox\local\libs\local\example>c:\d\dmd2\windows\bin\dmd ex00
.d

E:\sandbox\boost-sandbox\local\libs\local\example>ex00
returning
object.Exception_at_ex00.d(9): some error

Therefore, I was trying to address also this case to mimic D behavior.

Shall I ignore this case and require main() to always catch for the
C++ emulations of D-style scope guards to work?

Thanks.

-- 
Lorenzo

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