C++ Logo

std-proposals

Advanced search

Re: [std-proposals] !continue

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 6 Nov 2023 14:58:27 +0000
I reply in series below to Ville, Giuseppe, Jonathan, Marcin and Brian


Ville Voutilainen <ville.voutilainen_at_[hidden]> wrote:
>
> Yeah well, perhaps the solution to that problem isn't a facility
> spelled "not continue". Because for
> !continue, that's a valid alternative spelling.


I've no idea what you mean here. "!continue" is presently a compiler
error with g++:

        <source>: In function 'int main()':
        <source>:10:36: error: expected primary-expression before 'continue'
           10 | for ( int i = 0; i < 5; ++i ) !continue;
                | ^~~~~~~~
        Compiler returned: 1


Giuseppe D'Angelo wrote:
>
> In Perl this is called `redo`, which is a much much much better name
> than `!continue`.


Yeah fair enough we could have "__redo" or "_Redo" instead of
"!continue". Personally I like "!continue" but the exact syntax isn't
important right now.


Jonathan Wakely wrote:
>
> Yeah, obviously it means "don't continue" i.e. stop looping. But we have break for that.


I'm trying to learn German at the moment, and I still pause for a
second before saying "You must not do that", because I don't want to
mistakenly say "You don't need to do that". First I remind myself that
"Du musst das nicht machen" imposes no restrictions, and so then by
deductive reasoning I know that I've to pick out a different verb to
say "You must not".

Similarly with C++, if everyone knows that 'break' breaks out of a
loop, then everybody's gonna know that "!continue" must do something
else. Nobody who's been programming in C++ for more than a week is
going to think that "!continue" breaks out of a loop.

Here's another few possible syntaxes:
    --continue;
    continue(skip);


Marcin Jaczewski wrote:
>
> There is idiom like this with `erase`:
>
> ```
> if (condition)
> {
> p = v.erase(p);
> }
> else
> {
> ++p;
> }
> ```
>
> but do not know if this is good code practice as it is very easy to
> make mistakes.


This is exactly what I'm getting at here -- having a simple language
feature that allows us to write simple code. When we have to do
gymnastics -- and yes I'll admit it's fun sometimes especially when
programming microcontrollers -- those gymnastics can have bugs in
them, and just make the code more complex.

By the way at the start of this thread, Brian said that he jumped back
to the start of the loop when he wanted to skip the post-iterative
step. Brian, if you consider the following loop:

    for ( unsigned i = 0u; i < 67u; ++i )
    {
        if ( !Erase() ) !continue;
    }

which behaves the same as:

    for ( unsigned i = 0u; i < 67u; ++i )
    {
        if ( !Erase() ) { --i; continue; }
    }

then how would you use goto here? something like:

    for ( unsigned i = 0u; i < 67u; ++i )
    {
    start_of_loop:
        if ( !Erase() ) goto start_of_loop;
    }

That of course would skip the pre-iterative condition check, and so
we'd have to write:

    for ( unsigned i = 0u; ; ++i )
    {
    start_of_loop:
        if ( !(i < 67u) ) break;
        if ( !Erase() ) goto start_of_loop;
    }

But that isn't a very nice loop. Sure it works, but it would be much
nicer to have it as:

    for ( unsigned i = 0u; i < 67u; ++i )
    {
        if ( !Erase() ) !continue;
    }

or maybe:

    for ( unsigned i = 0u; i < 67u; ++i )
    {
        if ( !Erase() ) --continue;
    }

or maybe:

    for ( unsigned i = 0u; i < 67u; ++i )
    {
        if ( !Erase() ) continue(skip);
    }

Received on 2023-11-06 14:58:40