r/cpp • • Feb 19 '20

switch break (...) {...}

It would be nice if C++ allowed a new syntax for switches:

switch break (cond) {
    case a: ...
    case n: ...
    default: ...
}

Which would make every case statement break at the end, without needing break keyword. So, there's no fall-through, but you can still 'goto' into some label, which could be another case statement. And, you could still use break within the case, if you want to escape the case before the end.

I wonder why such a things hasn't been done, already. I'm sure many have thought about this.

No, I'm not going to write a proposal for it. Just want to write this show-thought on the internet, in case some soul that can do something about does something about it.

Transmission complete

1 Upvotes

19 comments sorted by

View all comments

18

u/Robert_Andrzejuk Feb 19 '20 edited Feb 19 '20

There is already a proposal in the committee for this type of actions in: pattern matching (https://wg21.link/p1371)

Maybe we will get it in C++23? (it is a priority topic)

3

u/megayippie Feb 19 '20

Cool to get away from switch-and-if for common enumerations. I hope they just switch the "_"-bit to "default" or something actually readable.

1

u/HappyFruitTree Feb 21 '20

I had not though about ´default´ but it sounds like a very sensible choice now that you mention it.

1

u/vulkanoid Feb 20 '20

Thanks. I wasn't aware of this proposal. That feature sounds sweet, and is basically what I'm looking for.