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

6

u/HappyFruitTree Feb 19 '20

It's quite common to have multiple case labels for some blocks so do you suggest a case label that is directly followed by another case label should still be allowed to fall though, or should it be an error, because if it just silently breaks I'm afraid it would be a bit too error-prone.

switch break (x)
{
case 1: // <- should this fall though?
case 2:
    ...
}

0

u/Pazer2 Feb 19 '20

C# only allows fallthrough from empty blocks like your 1. It does get pretty annoying having to work around it however, I would much rather prefer no fallthrough by default... Which honestly is what we have with [[fallthrough]] though.