Let’s say you have an enumeration type with 50+ entries for its value. There have been times where I’ve switch/cased an enum value with this amount of entries before, but only to check for, say, anywhere from 2 to 5 entries out of this.
For example, OpenGL sticks the majority of its enumerations in one enum type, referred to as GLenum. These values range from GL_VERTEX_SHADER to GL_STREAM_DRAW to GL_TRIANGLES to GL_ARRAY_BUFFER, etc.
As you can see, while these are related in terms of a bigger picture, the context in which they would be used does not relate with one another.
I’ve generated warnings on MinGW/GCC in Linux, where each warning accounts for every enum value listed in its type declaration. I’m not sure if this happens on VC++, but I figured I’d account for it anyway just in case.
In other words, the amount of warnings generated is TotalEnumValues - AmountOfCaseStatements for each enum value, providing not all enumerations have been accounted for in the statement itself, which can easily lead to 100+ compiler warnings if your enumeration type contains at least 100 entries.
I’ve often used if/else to accommodate this, but when checking enum values for more than just one entry (which is honestly quite rare, since the purpose of enums in general requires conditional logic to compliment it), I’ve always been prone to using switch/case, since I find it more aesthetically pleasing (even if it just compiles into an if/else statement anyway 😉 )
So, is this warning possible to turn off? If so, how?
Add a
default:case that does nothing. The compiler will see that you are “handling” all the possibilities (thus removing the warning) but it won’t change at all the generated code if you don’t specify any action for it.To clarify: I’m talking about this:
vs this:
the second one does not modify at all the behavior, so the compiler shouldn’t change the generated code, but will tell it that you didn’t forget about the other (im)possible cases.