Boost logo

Boost :

Subject: Re: [boost] Interest in breakable labeled scope emulation?
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2013-01-13 14:11:45


Le 13/01/13 13:30, TONGARI a écrit :
> Hi all,
>
> 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.
>
> Thoughts? Sorry if this idea is too simple and somewhat rejected before...
>
>
Beta language provides restart and leave a named blocks which are a
little bit more structured than gotos.

L:: {

  restart L;

  leave L;

}

which in C/C++ could be implemented as

restart_L:
{
   //restart L;
   goto restart_L;

   //leave L;
goto leave_L;

}
leave_L;

Maybe adapting your macros both can be provided

#define BOOST_RESTART(name) \
   goto BOOST_NAMED_BLOCK_RESTART_##name

#define BOOST_LEAVE(name) \
   goto BOOST_NAMED_BLOCK_LEAVE_##name

#define BOOST_NAMED_BLOCK(name) \
   BOOST_NAMED_BLOCK_RESTART_##name: \
   if (const bool BOOST_NAMED_BLOCK_##name = false){
(void)BOOST_NAMED_BLOCK_##name; BOOST_NAMED_BLOCK_LEAVE_##name:;} else

With these macros we can be able to do the following

BOOST_NAMED_BLOCK(L)
{

BOOST_RESTART(L);

BOOST_LEAVE(L);

}

The problem is that BOOST_NAMED_BLOCK(L) is not an statement but two.
Maybe you see a way to workaround this limitation.

I don't know however if the name LEAVE is a better name on the c++ context.

Best,
Vicente


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