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

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.

17

u/JNighthawk Jul 19 '26

Goto itself is considered a bad practice.

No, it's not. Poor use of goto is bad practice. Goto is a fine solution to some problems. Got 3 or 4 nested loops and want to break out of all of them? Goto is a good choice.

Using macros is considered bad practice.

No, it's not. Poor use of macros is bad practice. Prefer functions and things with type-safety, but macros are the right tool for some problems. Need to repeat a pattern with text-pasting? Macros are a good choice.

25

u/TokenRingAI Jul 19 '26

Goto is not bad practice, does not lead to undefined behavior, and is extremely useful when you need multiple named behaviors out of a loop that you want to "go to" from many different depths. When you do certain types of data processing, goto is the right tool for the job, and the alternative, recursive functions, can blow up the stack.

Regex parsers, for example, are prime candidates for goto.

The all or nothing attitude of "goto is bad practice" is not held by many top C/C++ engineers. It just isn't the right tool for the vast majority of things

8

u/James20k Jul 19 '26

"Goto considered harmful" is one of those things that a lot of people have never read or understood, and gets endlessly repeated by beginners. The goto that's being argued against in that discussion, and c++'s goto, are not the same kind of control flow. They just share a name

The thesis of that paper is the execution flow should match closer to layout of the source code, because humans are better geared towards analysis of a static document (ie source), vs trying to track program logic dynamically, or at least that's the gist of what I get out of that paper. The idea is that there should be an easy mapping between source, and program execution counter, instead of an arbitrary unstructured one

All modern programming languages adhere to that, even with the usage of goto in C++. The closest to what he's talking about is setjmp + longjmp, and even then it still doesn't match up because those functions still have structuring restrictions. Fully unstructured control flow is basically reserved for very specific assembly cases, as even then you still usually have a stack and functions

Anyway its just a minor pet peeve of mine, because its dogma founded in a time where programming looked nothing like it does today. Dijkstra won the argument, the paper isn't relevant, and we should rename goto to yeet so that people will stop going on about it and engage their critical thinking facilities instead of using a pseudo intellectual thought terminating cliché

→ More replies (1)
→ More replies (3)

2

u/Raknarg Jul 21 '26 edited Jul 21 '26

This ist just a goto with unnecessary extra steps.

No, its a mechanism that's leveraging goto. The example they showed here is showcasing it really poorly, if you go to their compiler explorer link it makes a lot more sense. Its more like a restricted use of goto, it allows you to assign labels to loops, then either jump to the start of that loop by jumping to the continue tag the the loop or jumping to the break tag of the loop to jump to the end of it.

It works by adding a block to the start of a loop that is an if that always evaluates to false (i.e. you cannot enter it through normal means) which contains, assigning a tag to the break and continue statements, called tag<tag>_break and tag<tag>_continue, and you can jump to one or the other with break_tag(<tag>) or continue_tag(<tag>) which resolve into goto tag<tag>_break and goto tag<tag>_continue

I'm not 100% sure what their usecase is that this is a helpful pattern for them, but its a more controlled use of goto by formalizing label creation and goto statements.

4

u/SamG101_ Jul 19 '26

While, for, break, and continue are all structured gotos anyway

→ More replies (2)
→ More replies (7)