r/cpp • u/danillissimo • 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
View all comments
Show parent comment
17
u/JNighthawk gamedev Jul 19 '26
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.
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.