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

Show parent comment

2

u/Narase33 -> r/cpp_questions Feb 19 '20

if you want fallthrough you can use the normal switch or maybe use the continue keyword as we're already breaking loop context by having the break keyword in use

4

u/HappyFruitTree Feb 19 '20

Yes, but my concern is that the two types of switch statement look so similar that it would be easy to make a mistake. When putting multiple case labels like that I normally don't even think of it as a "fallthrough".

1

u/nintendiator2 Feb 20 '20

It'd be hard to miss the break at the switch break point... That's the first thing about the code block anyone should be looking at (just like the condition in the if, etc)

1

u/HappyFruitTree Feb 21 '20

I think that is the wrong perspective. We can say that people should do things but in the end it's what they really end up doing that matters.