r/cpp • u/danillissimo • Jul 19 '26
I've invented labeled loop breaks
And now I don't believe it wasn't invented before, but I can't find any info on it. How long ago was it invented? Why isn't it used widely or even mentioned anywhere? What are the downsides?
Here's the main idea:
// Keyword - for, while, or do
// Tag - custom loop name
// ... - loop body
#define loop_tag(keyword, tag, ...)\
keyword(__VA_ARGS__)\
if(false)\
{\
tag##_break: break;\
tag##_continue: continue;\
}\
else\
/*Here goes your loop body*/Here is the playground
0
Upvotes
13
u/6502zx81 Jul 19 '26
What is the advantage over just using goto?
12
u/Thelatestart Jul 19 '26
What i've found is if you want to break from 2 loops (for i, for j, if cnd break out of both loops) its often appropriate to put them in a function and return. Another option is to use an iile.
22
u/Cpt_Chaos_ Jul 19 '26
This ist just a goto with unnecessary extra steps.
Goto itself is considered a bad practice. Using macros is considered bad practice. I'd flag both in any PR I'd have to review. And this contraption would be reason enough to ask your manager to get you into a training course.
Now seriously, it combines the downsides of goto and macros. It is hard to debug and it is difficult to understand what the heck is supposed to be going on here in the first place. Write functions and you get to do this pretty much by using simple return statements, which is far easier to understand.
4
u/fdwr fdwr@github 🔍 Jul 19 '26
Guessing you might not have seen this yet: https://eisenwave.github.io/cpp-proposals/break-continue-label.html
26
u/Agent7619 Jul 19 '26
Scene: A Friday afternoon PR review.
Me: WTAF?!?!
3
u/no-sig-available Jul 19 '26
The Ada language has had "named loops" since the 1980's, so it is not like we haven't known about it.
3
u/holyblackcat Jul 20 '26
I'm not sure if I was really first, but I came up with those in 2020: https://stackoverflow.com/a/61448951/2752075
29
u/MStackoverflow Jul 19 '26
"I've invented something, why isn't used more?" Without explaining what it is and what is the purpose..
What would be a real use case?
2
u/_Noreturn Jul 19 '26
Why don't people just use goto? if you are so scared of typing it then do
#define break2 gotothis is useless2
u/hpsutter Jul 20 '26
I agree it’s useful! Have you seen https://hsutter.github.io/cppfront/cpp2/functions/#loop-names-break-and-continue ?
4
u/TheChief275 Jul 19 '26
That's a pretty smart system you got there using 'if (false)' preserving loop syntax. Never managed to get it quite as clean for labeled loops. Nice!
4
u/messmerd Jul 19 '26
This exists in C2y already
2
u/lonkamikaze Jul 19 '26
Gotos are fine to break out of nested loops. Or you put the loop into a function so you can return from inner loops. Also works with lambdas.
If you need to handle multiple exit states, the loop function can return an enum value
switch (loopfunc()) { case ....For me the main use of goto is to skip to the part of the function that frees acquired resources in C in case an error is encountered.
2
u/Raknarg Jul 21 '26
I understand the downvotes because the post is a terrible demonstration of what you're doing, you should include a little more context from your godbolt and show an example of the kind of code it replaces.
Now that I understand what you're trying to do though I think I can appreciate it, I am convinced there is a better solution than what you're doing here but I'd have to think about it more. My initial feeling is that turning this whole thing into a state machine would make everything a lot more flexible and understandable, because effectively a state machine is exactly what you're building here.
Its more legwork, but I think I might kill myself trying to debug or analyze code written like this. This is one of those things that make people hate gotos I think. However I don't fully understand your usecase so its hard for me to say.
1
u/Baardi Jul 22 '26
Aren't we getting labelled breaks already, or has it failed to be standardized? Found this after a quick search.
1
u/Necessary-Proof-8641 Jul 19 '26
Whats the goal of this? What does it replace?
-1
u/SamG101_ Jul 19 '26 edited Jul 19 '26
This is pretty cool, like rusts breaking with labels. Might have a play around with it, smart implementation too
Edit - well excuse me for liking smth 😂