r/cpp • u/fyngyrz • 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.]
24
u/jedwardsol const & Feb 05 '20
C++17 has a way to indicate that you are happy with the fall through
https://en.cppreference.com/w/cpp/language/attributes/fallthrough