Boost logo

Boost :

Subject: Re: [boost] Interest in breakable labeled scope emulation?
From: TONGARI (tongari95_at_[hidden])
Date: 2013-01-14 07:49:36


2013/1/14 TONGARI <tongari95_at_[hidden]>

> 2013/1/14 Rob Stewart <robertstewart_at_[hidden]>
>
>> On Jan 13, 2013, at 7:30 AM, TONGARI <tongari95_at_[hidden]> wrote:
>>
>> > One of the few things I appreciate in Java is the labeled-break feature.
>> > I think C++ would provide it as well in future standard but it seems
>> that there's no even such a proposal.
>> >
>> > In lack of the language feature, a simple emulation can be used:
>> >
>> > ---------------------------------------------------
>> > #define BOOST_SCOPE(name) \
>> > if (const bool LABELED_SCOPE_##name = false){break(name); name:;} else
>> >
>> >
>> > #define break(name) \
>> > (void)LABELED_SCOPE_##name; goto name;
>> > ---------------------------------------------------
>> > Now we can write:
>> >
>> > BOOST_SCOPE(a)
>> > {
>> > break(a);
>> > cout << "123\n";
>> > }
>> >
>> > The real world usage would reside in nested loop&switch where
>> labeled-break
>> > really shines.
>>
>> While I understand the value of being able to exit such compound scopes
>> cleanly, goto is a poor solution as it bypasses destructors, which makes
>> this idea error prone.
>>
>
> No. At least for g++ & clang that I tested the stack is correctly unwound and
> the dtors are called.
>

You may want to see 6.7/3 "Declaration statement".
It's standard guaranteed.


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