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

2

u/tialaramex Jul 21 '26

Yes the "goto" in C and C++ is not the arbitrary control flow change Dijkstra was writing about, but it is still a bad idea and it's about time it was fixed, N3355 voted into C2y does so and I expect a similar fix or the one from C2y itself will be taken for C++29 or C++32)

However, if you don't like drama but did want to keep "goto" you shouldn't rename it "yeet" because that's currently a placeholder keyword in Rust so you'd ignite all the Rust controversy and all the "stupid keyword" controversy and because C++ doesn't have a syntax change mechanism you'd get all that controversy back again too.

If you don't believe me that "yeet" is a placeholder in Rust, https://doc.rust-lang.org/std/ops/struct.Yeet.html documents the current status of this feature flag. Its purpose was to delay bikeshed arguments about naming until the feature works - if the feature is bad then it doesn't matter what it's called, so you need a feature everybody can live with before you spend six months arguing whether to name it e.g. "throw" or "raise" or what.

Now, yeet is an idea for how to work with Try which is also not yet stable. And Try is the current proposal for why that ? operator you've doubtless seen in Rust works. The Try proposal says that operator is named Try, in the same way the + operator is named Add and so the reason it works is that the standard library has implemented this named trait. Most likely that explanation will stabilize, but then perhaps I'd have said that about the previous one, so what do I know.

In any case, the ? operator is stable and widely embraced as a good idea, the Try trait is widely favoured and will probably stabilize, but yeeting is clearly just a placeholder.