C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Improving break & continue

From: Robin Savonen Söderholm <robinsavonensoderholm_at_[hidden]>
Date: Tue, 26 Nov 2024 07:20:18 +0100
Labeled loops are on the move in C, so we will probably have it in C++ soon
too: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm

// Robin

On Tue, Nov 26, 2024, 01:29 Oliver Hunt via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> This seems ambiguous/confusing, take:
>
> ```cpp
> while (..) {
> …
> if (x) {
> break while;
> } else {
> while (…) {
> …
> break while;
> ...
> }
> }
> ```
>
> The specification semantics are unambiguous (you go to the nearest
> appropriate control structure) but for a developer you can restructure code
> and your code behavior can change completely, and silently. For example
> let’s refactor the above:
> ```cpp
> while (..) {
> …
> if (x) {
> break while;
> } else {
> - while (…) {
> + for (…) {
> …
> break while;
> ...
> }
> }
> ```
>
> The target of the break silently changes with no real ability to produce
> even warning diagnostic.
>
> I agree that improving break/continue is something that we should do, and
> seems like it should be very low cost (so I assume it has been proposed
> previously, and if so I don’t know the rationale for not doing so).
>
> Most other modern languages just allow continue and break to take a label,
> such that
>
> ```cpp
> foo: for (…) {
> bar: switch (…} {
> case X:
> wibble: while (…) {
> …
> If (a) break bar;
> If (b) continue wibble;
> If (c) break foo;
> …
> ```
>
> Which allows what you want, doesn’t have the hazards of targeting
> implicitly based on what syntactic construct is used, and makes it
> absolutely explicit what the intended target of the control statement is.
>
> Again, given this is a feature that is present in numerous languages, I
> assume that this has been proposed for C++ in the past, and there were
> problems that made it non viable (though I am curious what those problems
> are).
>
> —Oliver
>
> On Nov 25, 2024, at 3:23 PM, Filip via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
> Hi everyone,
>
> This is my first time posting like this.
> I have an idea to improve the use of break and continue keywords in loops
> and switch statements.
>
> Examples:
> #1
> ``` c++
> while ( … ){
> for ( … ) {
> if ( … ){
> break while; // break the while loop, not the for loop
> // break for while; // identical in functioning to the above version
> }
> }
> }
> ```
>
> #2
> ``` c++
> for ( … ){
> for ( … ) {
> if ( … ){
> break for for; // break out of outer loop
> }
> }
> }
> ```
>
> #3
> ``` c++
> while ( … ){
> for ( … ) {
> switch ( … ){
> case 0 : break while;
> case 1 : continue while;
> case 2 : break switch; // explicit break of switch statement
> identical to break in this case
> case 3 : continue switch; // fall through without using comment or
> [[fallthrough]]
> default: return …;
> }
> }
> }
> ```
>
> You could replace break with continue in any place above, it would avoid
> making additional boolean variables,
> extracting some simple loops into functions and not use labels all goto
> like.
>
> You could be more specific with the amount of different for, while, do or
> switch statements, but they should match the closest breakable statement
> with the least amount of keywords.
>
> Empty or traditional break / continue should match the closest breakable
> statement.
>
> Thanks for reading.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-11-26 06:20:36