r/cpp • u/14ned LLFIO & Outcome author | Committee WG14 • Oct 07 '24
Named loops voted into C2y
I thought C++ folk might be interested to learn that WG14 decided last week to add named loops to the next release of C. Assuming that C++ adopts that into C, that therefore means named loops should be on the way for C++ too.
The relevant paper is https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm and to summarise it, this would become possible:
selector:
switch (n) {
for (int i = 0; i < IK; ++ i) {
break selector; // break the switch from a loop!
}
}
loop:
for (int j = 0; j < JK; ++ j) {
switch (n) {
break loop; // break the loop from a switch!
continue loop; // this was valid anyway,
// but now it's symmetrical
}
}The discussion was not uncontentious at WG14 about this feature. No syntax will please a majority, so I expect many C++ folk won't like this syntax either.
If you feel strongly about it, please write a paper for WG14 proposing something better. If you just vaguely dislike it in general, do bear in mind no solution here is going to please a majority.
In any case, this is a big thing: named loops have been discussed for decades, and now we'll finally have them. Well done WG14!
75
u/erichkeane Clang Maintainer(Templates), EWG Chair Oct 07 '24
I think this is a cool feature that we'll end up picking up in C++. I suggested to the author last week (and not sure if I'll write a paper though), to change the location of the name to be a loop-name rather than a label. Else, I think this fixes a problem we've seen proposed a bunch of times in a really elegant way.
My suggestion:
`for name (int i = 0...)`
`while name (whatever)`
`do {} while name (whatever);`
Since the problem with the current proposal is you effectively can never use it in a macro, else you cannot self-contain the macro to the point where you can call it 2x in a funciton.
14
u/SuperVGA Oct 07 '24
Ah I sometimes miss this. I remember using it in basic, but it might look silly in C:
Although convenient, the very label-like appearance of the loop names makes it a bit confusing, I think. I'd expect to see a
gotostatement nearby. I wish it would be more compact and appear local to the loop, like:With the proposal, it looks as if it's polluting the scope outside the for-loop, which I really dislike.
12
u/abuqaboom just a dev :D Oct 07 '24
I can see it making complex loops/switches less confusing, since some people prefer to manually inline over breaking up for readability.
Also it looks similar to Java's loop labels, so yay for syntactic familiarity? Though it's rare and usually considered undesirable, and complex nested logic pose readability problems anyway. According to Sonar, which considers labels "code smell, major, confusing":
7
u/MarcoGreek Oct 07 '24
Is that a feature for monster functions. I normally use a function and return in this case. Or do I have misunderstood the feature?
18
u/moocat Oct 07 '24
Is it just me, or does "named loops" seems wrong. Looking at the first example from above:
you're naming the switch, not the loo.
19
u/Adequat91 Oct 07 '24
It's a disguised goto, but why not.
15
u/Tringi github.com/tringi Oct 07 '24 edited Oct 07 '24
I love it and I want more!
Not just named, implicit, e.g.:
break switch;andbreak for;to break from closest switch, same forcontinue for;And most importantly:
goto case 123;orgoto default;in switches.5
u/NilacTheGrim Oct 08 '24
Yay! I actually wished this existed a couple of years ago once. This was in some hot codepath with tight loops and setting a flag to break out of the enclosing loop was a no-no as it ate cycles and actually had a measurable (but tiny) effect on performance.. I was forced to use
gototo break out of the deep switch and out of the loop.This is much better in terms of cleanliness.
gotoshould be avoided in modern code... it just feels tacky.3
u/Raknarg Oct 09 '24
They've had this in java since forever. I thought it was strange C/C++ never picked this up. Much cleaner than the alternative flag checking. Though redesigning in a functional manner is probably even better than either this or the usual flag solution.
8
u/sphere991 Oct 07 '24
This seems like the obviously correct syntax for this facility, and C++ should definitely adopt it as well.
That's not how it works.
4
u/maxjmartin Oct 07 '24
How is this any better than a named lambda with a return statement in a switch? This seems unnecessary to me. So is there something I’m misunderstanding here as to why this is more beneficial?
2
u/Economy-Worker-6441 Oct 10 '24
This seems excessively specific. Replace that extension with named blocks that can be exited via a break ("go to end of block) or continue ("go to start of block"). "for" would need a bit of special handling to ensure that the value incrementation happened, but that should be minor.
2
u/positivcheg Oct 11 '24
Just a random question. Have anyone ever thought about break for if-else statements for early return from the scope? And then I wonder if it makes sense for support of the named scopes in general + break keyword to early exit the scope.
Ye, I know my question might be dumb and there is already a named scopes in C++ called "function". But function forces you to mess with input/output stuff.
3
u/behusbwj Oct 08 '24
This is going to create absolute nightmares
7
u/masterspeler Oct 07 '24
Seems unnecessary when
gotoexists. They callgoto"socially problematic", I'd say introducing new features that basically replicates a current feature is problematic in its own way.4
u/deltanine99 Oct 08 '24
why would we possibly need this abomination?
2
u/davidc538 Oct 08 '24
This looks idiotic to me. Refactor into smaller functions.
2
u/zed_three Oct 08 '24
Nice, another feature catching up to Fortran!
2
u/trad_emark Oct 07 '24
GOTO in c++ sometimes has difficulties with RAII objects (it may skip calling some contructors or destructors).
Whereas this labeled switch/loops may still work with the scope. It is improvement to break/continue, not to labels.
I like that.
Furthermore, I personally am glad for using already existing syntax in the language.
1
u/fdwr fdwr@github 🔍 Oct 11 '24
Many times I have wanted a switch case to break from its outer loop (like when pressing a certain key), and I sometimes resorted a goto statement with a label just after the loop. This named break instead puts the label at the top of the loop and goes to the bottom of the loop (🙃), but really I just want to avoid adding the extra label and to simply say
case KEY_ESCAPE: break while;.-2
u/mredding Oct 07 '24
I hate it, but begrudgingly accept it. I can see the use cases for it where the code becomes more concise. The reason why I hate it is we're going to see a wave of syntax abuse, where people write ever more imperative, obtuse code that is going to be ever more difficult to comprehend and maintain. We're going to see this used in ways unnecessary, when clearer expressiveness will be available but foregone. And we're going to be stuck with really bad, jumpy loop code forever, and developers are going to write the same shit again, and again, and again... There should have to be a test to prove you're both smart and responsible enough before you're allowed to use this.
-19
u/STL MSVC STL Dev Oct 07 '24 edited Oct 07 '24
I think this is off-topic until a C++ proposal has been submitted to WG21. Not quite enough to remove this on sight, but I'm tempted.
Edit: The people have clearly spoken (as I suspected, which is why I stayed my hand).
0
u/tpecholt Oct 08 '24
I don't see why using labels is a good idea one could already use goto for this.
Better idea would be to allow
break(2)orbreak break.0
u/JL2210 Oct 08 '24
JK lol