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

4

u/danillissimo Jul 19 '26

STL has nothing to do with complex data processing. My exact case is about long-running complex looped behaviors though, packed as coroutines in particular. They contain a notable amount of "on event X restart polling layer of logic A" and "on event Y level of polling B is done".
I'd like to say that labeld loops are actually a rare necessity, but recently I've faced a lot of cases where they would fit nicely, so I've invented this to get rid of "break/continue this layer of logic" flags.