r/cpp • • 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

48 comments sorted by

View all comments

Show parent comment

-1

u/RogerV Jul 19 '26

not really when the loop is nested in a loop and need to break out of both

break should have been designed to accept a label target - that would have solved things nicely so could have avoided introducing that scary word "goto"

0

u/SamG101_ Jul 19 '26

A while loop runs and gotos the top, continue is goto the top, break is goto after the loop tho? Like what I'm saying is normal looping and flow control just abstracts over goto statements, but in a controlled way so the flow remains clear. And yh labeled loops would've been cool, like rusts ones