r/cpp • • Feb 05 '20

Fall through on switch case - warnings?

So, for years, in both c and c++, I've used a technique where a case: does a bunch of stuff, call it X, and the case: above it needs to do exactly that, plus Y stuff, so I put the Y in the upper case: and let it fall through to the X case. It's always worked great, never had a problem.

So I went to port a relatively large app (this one) from OSX, where I developed it, to Windows. Installed Qt, built it, the qt install picked a compiler (mingw), etc., and off I went to cross-compile.

The new compiler, predictably, went at things a little differently, and TBH, it caught a few things I was glad to have caught. So far so good.

But it also pushed warnings about my fall-through case: statements, and looking at them, they were doing just what I had intended. I hadn't yet found a way to switch the warnings off (laptop is off getting more RAM and an SSD, so I can't even look at it today) but probably there's a way.

So what I'm asking, though, is what folks think of this technique. Apparently, the compiler authors think I shouldn't be doing this. Anyone agree? Disagree? Other? If I can turn this off, should I? Yes? No? Other?

TIA.

[EDIT: End goal is zero errors, zero warnings, always. I can just code this differently, two cases plus an if inside the case, for instance... it just seems inelegant.]

7 Upvotes

23 comments sorted by

View all comments

8

u/stilgarpl Feb 05 '20

It's not that you shouldn't be doing it, you just have to be explicit that you really want it and you haven't just forgotten to write break; Just use [[fallthrough]] attribute and you won't get a warning for doing it.