C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal regarding labeld breaking and continuation

From: hypatia.sva_at <hypatia.sva_at_[hidden]>
Date: Fri, 17 Feb 2023 14:50:58 +0000
Hello,

Actually, I had an example of that. The algorithm was the Smith normal
form of an integer matrix, and I had to add / subtract columns from each
other until the gcd was in the pivot position a[i][i]. The short form of
the algorithm is like this:

First, Make sure a[0][0] isnt 0; if the whole matrix is 0, return,
otherwise swap columns / rows
The main loop: subtract the first column/row from the others as much as
possible while staying positive (so subtract the column a[i][0] idiv
a[0][0] times). If for any now a[i][0] or a[0][i] is smaller than
a[0][0] (i.e. a[i][0] % a[0][0] > 0), then swap the columns, _and begin
the loop again_!
This means, inside the loop going over the rows / columns, you have to
restart, effictively you have

for(;;)
outer: {
    for(row: rows) {
       transform();
      if(necessary) {
        swap_rows(row,0);
        continue outer;
      }
     }
     //the same with columns.
}

I agree, its relatively specialized, and you can refactor it into a
function, but the fact Fortran has had it for so long and the fact its
so trivial to implement for the compiler, but not the user, means that I
do think its useful.

Regarding the cleanup, yes, if its type related, thats true, but
sometimes, for search, sort algorithms etc. it might not be. But I'm not
an expert on this, so I might wait what others have to say about this.

Hypatia of Sva.



Am 17.02.2023 15:25 schrieb Bo Persson via Std-Proposals:
> On 2023-02-17 at 13:30, Hypatia (of) Sva via Std-Proposals wrote:
>> Hello Members or WG21,
>>
>> I have written up this proposal for labeled breaking and continuation,
>> because I was kind of annoyed that this feature, which Fortran got in
>> 1990, is still not present in neither C nor C++. I would be interested
>> if anyone would have comments on both the content and form ot this
>> proposal. As by the advice on isocpp.org I took a draft from
>> https://github.com/mpark/wg21/, namely the one about pattern matching,
>> and changed it to what I wanted to write, I would be interested if
>> this form seems appropriate or if I made any mistakes here. I would
>> also be interested to know how the document numbers come about, it
>> seemed to be based on the date so that's what I went with, but that
>> may be wrong so I'm waiting for your feedback on this.
>>
>> Hypatia of Sva.
>>
>> Formatted, see:
>> https://github.com/hypatia-of-sva/documents/blob/main/P230217R0.pdf
>>
>> Markdown Text (see also
>> https://raw.githubusercontent.com/hypatia-of-sva/documents/main/P230217R0.md
>> ):
>>
>
> The critique might be to question how useful this really is.
> Personally I have never had much problem with exiting nested loops.
> Occationally I have used a condition flag, but most often the solution
> is to put the loop in a function/lambda and use a return to terminate
> the outer loop.
>
> I have never, ever, felt a need to restart an outer loop from an inner
> loop. Does that really happen?
>
> Hardly ever use continue either, as an
>
> if (condition)
> continue;
>
> most often can be replaced by
>
> if (!condition)
> {
> // rest of the loop
> }
>
> so I feel no strong need to "improve" continue at all.
>
>
> The goto error and cleanup seems like something the C committee
> perhaps could be interested in. In C++ we already have destructors for
> the cleanup, so not much need for another way of doing a similar
> thing.
>
>
> And also, I find the label in the left margin looking really, really
> ugly. But perhaps that is a good thing, as it urges me to avoid using
> it!

Received on 2023-02-17 14:51:01