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
2
u/Cpt_Chaos_ Jul 19 '26
The question I would ask here is: Why do you need to do these nested loops in the first place? In many cases, STL already provides algorithms that help you doing this sort of logic, which allows you to express what you want to do in less code. I'm not saying you're wrong, but I'd ask the question in a PR.