Given this code:
#include <stdio.h>
int main( int argc, char** argv )
{
switch (0) {
case 1: printf("1");
case 2: printf("2");
default:
printf("default");
break;
}
return 0;
}
I'd expect that the compiler would tell me something about either conditional expression is constant (switch (0)
while VS2012 issues this warning for an if(0)
) or unreachable code (case 1
, case 2
).
However, neither on ideone nor on Visual Studio 2012 nor on recent GCCs I receive anything like that.
Why don't even decent compilers complain here?
I just tried it on CLANG with extended warning turned on and received the following warning:
Referring to (oddly enough) only the the first of the two lines in the switch that will never be executed:
case 1: printf("1");
From the C 2011 standard:
5.1.1.3 Diagnostics
1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
9) The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program.
Emphasis added.
So, a diagnostic is required if using a constant expression in a switch
or if
control expression is a constraint violation...
6.8.4.1 Theif
statement
Constraints
1 The controlling expression of anif
statement shall have scalar type.
...
6.8.4.2 Theswitch
statement
Constraints
1 The controlling expression of aswitch
statement shall have integer type.
2 If aswitch
statement has an associatedcase
ordefault
label within the scope of an identifier with a variably modified type, the entireswitch
statement shall be within the scope of that identifier.154)
3 The expression of eachcase
label shall be an integer constant expression and no two of the case constant expressions in the sameswitch
statement shall have the same value after conversion. There may be at most onedefault
label in a switch statement. (Any enclosedswitch
statement may have adefault
label orcase
constant expressions with values that duplicatecase
constant expressions in the enclosingswitch
statement.)
154) That is, the declaration either precedes theswitch
statement, or it follows the lastcase
ordefault
label associated with theswitch
that is in the block containing the declaration.
...which it isn't.
This is basically a quality of implementation issue; the implementation may issue a diagnostic for using a constant expression in an if
or switch
statement, but it doesn't have to. if( 0 )
and switch( 0 )
may strike most of us as being hinky, but the implementation is not required to issue any diagnostics over them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With