
2013/1/14 Rob Stewart <robertstewart@comcast.net>
On Jan 13, 2013, at 7:30 AM, TONGARI <tongari95@gmail.com> 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.