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

-2

u/TokenRingAI Jul 19 '26

Usually, but there are types of functions that are infinitely recursive, so you can't do that without blowing up the stack.

The non-goto way is to package them as work in queue and iterate the work queue, but then you can end up with dynamic allocation and ad hoc object lifespans which are way worse to deal with and way less performant than simply using goto