C++ Logo

std-proposals

Advanced search

Re: [std-proposals] goto is not harmful (was: "once" keyword)

From: Barry Revzin <barry.revzin_at_[hidden]>
Date: Wed, 24 May 2023 09:40:00 -0500
On Wed, May 24, 2023, 8:27 AM Alejandro Colomar via Std-Proposals <
std-proposals_at_[hidden]> wrote:

>
>
> On 5/24/23 15:21, Lénárd Szolnoki via Std-Proposals wrote:
> > Hi,
> >
> > On 24 May 2023 14:07:28 BST, Federico Kircheis via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
> >> On Wed, May 24, 2023 at 03:02:56PM +0200, Alejandro Colomar via
> Std-Proposals wrote:
> >>> On 5/24/23 14:57, LUCE Jean-Sébastien via Std-Proposals wrote:
> >>>> Hello
> >>>>
> >>>> It is often useful to support multiple cancel conditions, like
> 'return' in
> >>>> a function, but without having to isolate the code in such a function
> >>>> (which is called from a single point).
> >>>>
> >>>> I'm used to write code like
> >>>>
> >>>> "
> >>>> do {
> >>>> if (Failed(Condition0))
> >>>> break;
> >>>> if (Failed(Condition1))
> >>>> break;
> >>>> Code_to_execute;
> >>>> } while (0);
> >>>> "
> >>>
> >>> The keyword exists, with a lovely name: goto
> >>>
> >>> ```c
> >>> if (failed(foo))
> >>> goto x;
> >>> if (failed(bar))
> >>> goto x;
> >>>
> >>> baz();
> >>> x:
> >>> ```
> >>>
> >>>
> >>>
> >>> [...]
> >>>
> >>
> >> Unfortunately it cannot be used in constexpr functions, maybe it is a
> good time to lift that restriction...
> >
> > Absolutely agree, I just bumped into this. I went with the do {...
> break; ...} while(false) workaround.
> >
> > Is this hard to implement?
>
> Don't know if it's hard, but GCC-12 has implemented it (possibly
> before 12, I don't know):
>

Yes, implementors have reported that it would be very difficult to
implement.


> $ cat goto.c++
> constexpr int
> foo(int x)
> {
> if (x >= 3) return x;
> inc:
> x++;
> if (x < 3)
> goto inc;
>
> return x;
> }
> $ g++ -Wall -Wextra -std=c++2b -c goto.c++
> $
>

This... doesn't actually test what you think it's testing.

If you try to constant-evaluate foo(0) then you'd see that it fails to
compile.

Barry

>

Received on 2023-05-24 14:40:13