3 ms·
Because it warns even on unreachable cases. weird_non_trivial_copy_constructor_type foo() { switch(value & 3) { case 0: return a; case 1:
by byuu 10y ago
Because it warns even on unreachable cases.
weird_non_trivial_copy_constructor_type foo() {
switch(value & 3) {
case 0: return a;
case 1: return b;
case 2: return c;
case 3: return d;
}
}
You can fix it by adding a default:, which will generate worse code; or by adding a dummy return (usually easy, but not always) after the switch; or by using compiler-specific intrinsics like __atribute__((unreachable)). Or you can turn off the warning.
If they add [[unreachable]] to the standard, then I'll consider switching to that and re-enabling this warning. Because I'll admit, I have made the mistake of omitting a return type before.
- to3m 10y agoRight, yes. I guess a 4-value enum could be an option too, but I've never tried. For gcc and clang I've also had some success with the diagnostic pragmas, which I stick in pshpackN.h-/poppack.h-style header files. For example, I use these to stop both moaning about flexible array members in C++ code. Push header: #ifdef __GNUC__ #pragma GCC diagnostic push #ifdef __clang__ #pragma GCC diagnostic ignored "-Wc99-extensions" #else #pragma GCC diagnostic ignored "-Wpedantic" #endif #endif Pop header: #ifdef __GNUC__ #pragma GCC diagnostic pop #endif