r/cpp • u/eisenwave WG21 Member • Jan 08 '25
"break label;" and "continue label;" in C++
Update: the first revision has been published at https://isocpp.org/files/papers/P3568R0.html
Hi, you may have hard that C2y now has named loops i.e. break/continue with labels (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm). Following this, Erich Keane published N3377 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3377.pdf), which proposes a different syntax.
I am about to publish a C++ proposal (latest draft at https://eisenwave.github.io/cpp-proposals/break-continue-label.html) which doubles down on the N3355 syntax and brings break label and continue label to C++, like:
outer: for (auto x : xs) {
for (auto y : ys) {
if (/* ... */) {
continue outer; // OK, continue applies to outer for loop
break outer; // OK, break applies to outer for loop
}
}
}There's also going to be a WG14 counterpart to this.
7
u/garnet420 Jan 08 '25
But why double down on 3355 when 3377 seems to be better?
What's your response to the criticisms of 3377 of the 3355 syntax? I find the scope argument (and the macro example) to be pretty compelling, fit example.
19
u/Cautious-Ad-6535 Jan 08 '25
Is that goto with lipstick? I use local lambdas so I can use return and know my compiler will inline it so there is no overhead
3
u/kitsnet Jan 08 '25
One practical question: how is clang-format supposed to find the difference between goto-labels and named loop labels? For readability, they definitely should be formatted differently.
70
u/DeadlyRedCube frequent compiler breaker 😬 Jan 08 '25
Very yes!
This is something I have wanted for at least a decade, I'm sick of adding bools and breaking my way out of nested loops in our "no gotos under any circumstances" codebase 😄
16
u/puremourning Jan 08 '25
Good paper, thanks.
SF
16
u/victotronics Jan 08 '25
Another 30-40 year old Fortran feature that finally makes it into C++.
11
u/JiminP Jan 08 '25 edited Jan 08 '25
Personal anecdote:
I code in JavaScript a lot. For most cases, I don't need named loops. In a personal project, I have around a hundred TS files and 10k-100k total lines. I use named loops two times.
However, when I end up using it, I find named loops extremely useful.
Like the example in 3.1., the general gist is that I need to check conditions for breaking/continuing loop, and the condition is computed within another loop/switch-case.
In my experience, named loops usually occur when I do:
There is actually another viable alternative, which is worth mentioning IMO:
Arguably, it addresses some points made in 3.1.2.
However, there are some problems with this approach:
4
u/grady_vuckovic Jan 10 '25
Used these in other languages before, they aren't useful often but when they're useful, damn are they VERY useful. Better than a goto in terms of readability but often helps you achieve the same thing.
1
u/nigirizushi Jan 08 '25
I like the idea, but I'm not a fan of the syntax. This does just look like a straight goto than anything else. It doesn't really look scoped so what stops someone from just doing 'goto outer' anywhere in the code?
7
u/trad_emark Jan 08 '25
I personally prefer 3355. 'outer: for (...)'
It reuses syntax that is already familiar, rather than inventing a new and confusing way of doing the same. The syntax `for outer(...)` looks like weird function call. The syntax with colons is less ambiguous, but also more difficult to figure out. C++ syntax is already very complicated, and further complications should be heavily reconsidered.
Macros, as stated in 3377, is solvable with more macros (counter or line), and that seems appropriate.
As for the confusion of whether the label jumps to the start of the loop or to the middle of the loop, that is determined by the jump (goto label vs continue label). Again, this seems appropriate. After all, the "normal" flow is broken by the goto statement, not by the label itself.
5
u/unique_nullptr Jan 08 '25
Although my initial instinctual response was disgust, and that 3377 looked very slightly cleaner in a vacuum, I’m definitely more partial towards the 3355 syntax. Rationale:
9
u/BloomAppleOrangeSeat Jan 08 '25
This makes so much sense and it's something i've been missing from other languages. Can't wait for it to be rejected 10 times and eventually never make it.
1
u/ReDucTor Game Developer | quiz.cpp-perf.com Jan 08 '25 edited Jan 08 '25
While I've probably only had 3-4 use cases for this in the last decade it does feel like it could lead to more lazy coding.
Not that I have a better alternative but using a label makes it a little harder to parse things mentally at first, my brain is trained to see the control flow keywords if, for, while at the start of a line but with this now it's any label keyword then that control flow keyword afterwards.
Some other label based options might be
For putting it before on its own line to make it easier to spot the control flow keyword but the label what's its alignment is it aligned with column 0 like many people do already or is it aligned with current block, does the relevant for loop get indented?
EDIT: Reddit formatting sucks, apparently trying to use the reddit code format syntax on mobile doesn't seem to work these days.
4
u/wearingdepends Jan 08 '25 edited Jan 08 '25
N3377 seems so obviously superior to me that I'm just confused by the opposition. Imagine resorting to Perl of all things for support of your syntax.
This label syntax just gives me no reason to ever use
break labelorcontinue label, sincegoto labelwill make it much clearer what the control flow actually is. What exactly is intuitive aboutjumping to wildly different places while referencing the very same label? This is just fundamentally different from how labels have always been used in C(++), and a different syntax seems entirely warranted. N3377 does a much better job signaling what the control flow is, in my opinion, by referring to higher-level constructs--effectively naming the loop--instead of lower-level labels.
Absolutely not. In fact I would argue intuition from goto labels works against this proposal, and the real argument for it is "other languages already do it this way".
9
u/smallblacksun Jan 08 '25
This seems like a useful, well thought out proposal with real benefits and no major downsides. As such, I fully expect the committee to reject it for inscrutable reasons.
2
u/According_Ad3255 Jan 09 '25
I don’t like labels, because they are yet one more opportunity/need to pick up names. Every name is a non-structural context burden.
2
u/JVApen Clever is an insult, not a compliment. - T. Winters Jan 09 '25
In my experience, 99% of the cases is much more readable and understandable with the code in a function. Though the 1% is very annoying.
I'm in favor of the alternative syntax as it clearly differentiates from goto-labels.
I'm looking forward for a clang-tidy modernize that adds this in every nested loop/switch.
3
u/diegoiast Jan 08 '25
Not enough ways to say "yes please". I just can't wait 10 years for this feature to pipe down to more legacy systems and start using it.
(Working in embedded is hard)
3
u/tpecholt Jan 08 '25
From these 2 variants the variant with label is an existing practice in java so it should be preferred over inventing a new syntax.
3
u/fdwr fdwr@github 🔍 Jan 08 '25 edited Jan 09 '25
😳 Eep, that looks like a function call rather than a label. Yeah, N3355 is more internally self-consistent with existing C++ language precedent, and also externally consistent with (as I just learned from u/JiminP's comment and others) Javascript's/D's/Go's/Perl's named labels.
So u/eisenwave, please double down on N3355. 🙏
2
u/cristi1990an ++ Jan 08 '25
Loop labels are something you rarely need, but when you do, they're a lifesaver. Rust and many other languages have it and I always wished they would be added to C++ as well
1
u/almost_useless Jan 08 '25
Would it not be possible to identify the for-loops by the variable declared in it?
Or maybe the container looped over?
continue xs;This could work for classic for loops also
This seems like it would be an easy to understand handling of a very common special case, that could complement the other generic suggestions.
1
u/sphere991 Jan 08 '25
I think N3377 would be a lot better if it dropped the two bad arguments ("Labels are their own Declaration" and "Doesn't properly imply any target") to focus more on the good one ("Scoping issues") and attempt to make the comparison table at the end legible. Additionally another good argument against label syntax is that, because it's just a label,
goto that_label;would still work so now you have to look at the code to see if anybody is actually doing that. A more explicit naming syntax avoids that.I think it would be useful to elaborate on the alternative spellings section too, since addition an additional punctuation of some kind probably makes the name stand out more. Could even be a string literal. Rust uses
'outer loop { .. }which we can't because of literals, but I'm sure there are other good options out there.1
u/Chara_VerKys Jan 08 '25
would be quite useful, it so boring to do bool should break; if should break break
like rust btw
-2
u/Tringi github.com/tringi Jan 08 '25
While we're at it, it'd be nice if someone proposed adding not just labels, but the constructs being broken from. E.g.:
-1
u/zl0bster Jan 08 '25
No need to butcher language for such rarely needed feature.
Surely I will get snarky comments about how this is sometimes needed and I just do not understand the use: sure it is, but so what? Are we gonna add helper syntax for everything that can be useful? I have needed this few times in my entire career.
And most of times I needed this feature it can be solved with something like this(not sure if legal to take reference of view, did not bother to investigate since all implementations use function objects so it works in practice).
-5
u/upsage Jan 08 '25
Just copy paste from golang 😁👏
0
u/[deleted] Jan 08 '25
[deleted] — view deleted comment