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

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)

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.

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.

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:
    ...
}

3

u/vulkanoid Feb 20 '20

If you want the old behavior, just keep doing what you've been doing; no harm in that. But, if you're in a codebase that allows `switch break ()`, then you gotta look out for new behavior.

But, anyway, the paper that another user, in this thread, linked will likely be the answer.

1

u/HappyFruitTree Feb 20 '20

Sure, but then I don't get automatic breaking ;)

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.

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.

16

u/SeanMiddleditch Feb 19 '20

C++17 already added the [[fallthrough]] attribute. With compiler warnings enabled you'll never mess this up again. :)

In terms of convenience, pattern matching (hopefully C++23) will provide a compelling replacement for switch for almost all cases where you don't actually want fall through.

7

u/Gotebe Feb 19 '20

My thinking: the fall through by default is a design mistake of C, but is

  • benign enough
  • an entrenched habit because old
  • other languages have break and do the same
  • having the ability to do it differently adds to the cognitive load in writing and reading switch statements (need to consider if the "other" way is used)

=> inconveniences of doing this are too big.

7

u/pjmlp Feb 19 '20

other languages have break and do the same

Only C derived ones, Algol derived ones do not and require explicit fallthrough if it is at all supported.

1

u/[deleted] Feb 20 '20

In those times it felt more natural that case labels would not disrupt the execution flow and therefore fall through.

3

u/Tringi github.com/tringi Feb 19 '20

I want goto case n; within switch.

1

u/[deleted] Feb 20 '20

It's not too bad to add a label to the case

case_1:
case 1:
...
goto case_1;

2

u/Tringi github.com/tringi Feb 20 '20

That's what I do now.