Boost logo

Boost :

Subject: Re: [boost] Interest in breakable labeled scope emulation?
From: Rob Stewart (robertstewart_at_[hidden])
Date: 2013-01-14 05:55:48


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.

___
Rob


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