-6

Would the suggestion to have a syntax for a switch that does not need breaks all the time feasible?

My suggestion would be to insert auto both before switch and each case:

auto switch(expr) {
  auto case 1: statement; statement;
  auto case 2: statement; statement;
  default: statement; statement;
}

would be equivalent to

switch(expr) {
  case 1: statement; statement; break;
  case 2: statement; statement; break;
  default: statement; statement;
}

The compiler would complain with an error

  • if in a auto switch any case is not prepended with auto or
  • if a auto case is used in an auto-less switch.

Is there any parser problem to be expected, any ambiguities?

Is there additional benefit from less wrong-use-errors of users, like better optimization possibilities?

Update

A slightly more useful case would be where the location of the break is not so clear:

case(expr) {
  case 1:
      if(isThisTrue) {
        print(that);
      } else {
        break;
      }
      doSomethingElse();
      if(!isThisTrue) {
        break;
      }

...or something like this.

I think prevention of fall-through is useful to prevent some difficult to diagnose errors.

Quentin
  • 62,093
  • 7
  • 131
  • 191
towi
  • 21,587
  • 28
  • 106
  • 187
  • 9
    Why is `auto` in the beginning better than `break` in the end? – alexeykuzmin0 Dec 06 '16 at 12:12
  • 1
    @alexeykuzmin0 it is one letter less to type and it would be yet another keyword with completely different meanings, making c++ even more complicated ;) – 463035818_is_not_an_ai Dec 06 '16 at 12:14
  • because one missing break is difficult to detect by the compiler. But if you choose to use the `auto switch` you would be stopped from not using the `auto case` -- the "equivalent" of the `break`. – towi Dec 06 '16 at 12:15
  • How would you handle the case where some `case`'s are `break`ed and some aren't, e.g. `fallthrough`? The way `switch` works now is perfect. – DeiDei Dec 06 '16 at 12:15
  • @tobi303 right. But to put it on serious ground: `auto` has so many overloads already, one more doesnt count. Also, I find that this `auto` is indeed automation. To introduce a completely new keyword is out of the question in c++, I am sure. – towi Dec 06 '16 at 12:17
  • 1
    Maybe this belongs on [std-proposals](https://groups.google.com/a/isocpp.org/forum/#!forum/std-proposals)? – Kerrek SB Dec 06 '16 at 12:18
  • General observation: You don't make a language simpler by adding to it. You might make one particular thing simpler, but you've still made the language more complex. – Kerrek SB Dec 06 '16 at 12:19
  • Why not `auto default`? I put my defaults at the beginning. – Kerrek SB Dec 06 '16 at 12:20
  • @KerrekSB good point! – towi Dec 06 '16 at 12:22
  • 2
    @towi Can you give an example of undetected missing `break`? As far as I remember, I always have compiler warnings in such situations which makes it just impossible to forgot a `break`. – alexeykuzmin0 Dec 06 '16 at 12:23
  • 1
    There is really no motivation for this. If you want a guaranteed breaking `switch`-statement, you're looking for an `if`-statement. The mapping would be easy for the compiler, but again... no benefit. – Jonathan Mee Dec 06 '16 at 12:24
  • *"one missing break is difficult to detect by the compiler"* - [not if it is clang or gcc](http://stackoverflow.com/questions/27965722/c-force-compile-time-error-warning-on-implicit-fall-through-in-switch). The difficulty may be in programmers not heeding compiler warnings. – StoryTeller - Unslander Monica Dec 06 '16 at 12:28
  • @StoryTeller http://coliru.stacked-crooked.com/a/19971c6666c486aa cant get gcc to warn me – Hatted Rooster Dec 06 '16 at 12:32
  • @GillBates It's probably because it's [part of GCC 7.0](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=7652). So I should clarify that it's clang today, and gcc in the (maybe not so) near future – StoryTeller - Unslander Monica Dec 06 '16 at 12:35
  • `But to put it on serious ground: auto has so many overloads already, one more doesnt count.` (etc.) So, with your logic, a language where the source is a unary stream of "auto" is the best language. Ie. autoauto means something different than autoautoauto. – deviantfan Dec 06 '16 at 12:47
  • ...Imho, this whole idea is not just nonsense, but at least borderline trollish. – deviantfan Dec 06 '16 at 12:49

1 Answers1

2

This won't be a thing for many reasons!

  • The most obvious: it doesn't save any time totype auto rather than break

  • It is a lot less readable. Just like nobody would recommend the use of goto, your proposition is hard to decypher. Imagine a long case block, a reader would have to keep in mind this auto keyword the whole time he is reading the block. Unless you are using a lot of goto, c++ is pleasant to read because you don't to go up and down in the code constantly.

  • A case statement might not systematically break, and if I choose to add a condition that skips the break, your auto would most likely be forgotten and annoy me when compiling.

  • auto already means something. Adding new keywords to a language can be annoying, but re-using some with a different meaning is just bad!

To answer your question, at last: no this wouldn't cause any parsing issue. Just replace auto case[...]auto by case[...]break. You can, actually, define your own macro to do exacly this! And by doing so you will probably realize why it is not necessary.

Something like:

#define AUTO(x) x break;
switch(i){
case 1: std::cout<<1;
AUTO case 2: std::cout<<2;
AUTO default: std::cout<<i
}

Note: This macro is kinda twisting your point, I know. There's definitely a way to do something closer to what you have in mind, I'll think about it and update when I have time.

Flynsee
  • 596
  • 4
  • 17
  • I agree that "A case statement might not systematically break". Indeed in some cases you have write your case-statements differently. I do not agree with the other items. I am surprised that this even is a point for someone. Of course it does not save time to type, but it is *not less* readable. And the point that `auto` already means something (many, in fact) I thought would be ok, because this would be an "auto". So, I didn't want to have this a discussion (inappropriate for S.O.) but a "is it doable". I will therefore retract the question. Indeed, your macro is not what I mean. – towi Dec 06 '16 at 13:42