C++ Logo

std-proposals

Advanced search

Re: [std-proposals] !continue

From: sasho648 <sasho648_at_[hidden]>
Date: Tue, 7 Nov 2023 14:13:45 +0200
Anyone mentioned goto? - With goto you can do exactly what the OP is
asking for:



for ( ; SerialSimGsm.available(); ++p )
rev_con:
            {
                if ( p >= &buf[sizeof(buf) - 1u] ) break;
                p[0u] = SerialSimGsm.read();
                p[1u] = '\0';
                unsigned const len = std::strlen(buf);
                char *ending = nullptr;
                if ( nullptr != (ending = std::strstr(buf, "\r\n")) )
                {
                    ending[0u] = '\0';
                    ending[1u] = '\0'; // Overwrite the "\r\n" with "\0\0"
                    if ( ending != buf ) this->ProcessInput();
                    buf[0u] = '\0';
                    p = buf;
                    goto rev_con; // Don't allow 'p' to be incremented
                }
            }

On Tue, Nov 7, 2023 at 9:21 AM Brian Bi via Std-Proposals <
std-proposals_at_[hidden]> wrote:

>
>
> On Mon, Nov 6, 2023 at 4:58 AM Frederick Virchanza Gotham via
> Std-Proposals <std-proposals_at_[hidden]> wrote:
>
>> 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;
>> }
>>
>
> I think the situations where you need to not only skip the incrementation
> statement but *also* re-check the condition *despite not having
> incremented* are very rare.
>
>
>>
>> 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);
>> }
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>
>
> --
> *Brian Bi*
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-11-07 12:13:58