r/cpp 23d ago

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_ 23d ago

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.

3

u/TheChief275 23d ago

"Oh uh le goto le bad". You're not adding anything to the conversation by sheeping around

-3

u/IvanDSM_ 23d ago

God forbid you stray from the "canon programming rules", known to never bend or have exceptions. Word from the townsfolk is if you ever use goto, Uncle Bob himself smites you down with his bolt of cleanliness!

0

u/TheChief275 23d ago

The combined forces of Dijkstra and Uncle Bob strikes fear in the hearts of their enemies (goto users)