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

3

u/defaultguy_001 Jul 19 '26

Correct, it isn't bad, it's just people who are low skilled who create bad code with it.

0

u/Cpt_Chaos_ Jul 19 '26

That is exactly why I wrote it with this "all or nothing" wording. The examples given by others here are perfectly fine use cases. If it's the best tool for the job and you can explain it to me like that, that's fine and I won't argue.

However, I've seen too many abuses of various language features to just say "yeah, it's perfectly fine to write code like that". In a professional environment, readability FOR OTHERS is a lot more important than many devs think. And the macros shown by OP fall into the category of "What the heck are you trying to accomplish here? Couldn't you have expressed your intentions in more readable ways?"

1

u/100GHz Jul 24 '26

People can write gibberish in any alphabet. Maybe the alphabet isn't the problem?

Goto, preprocessor directives etc are fine.