Source code: 1.cpp #include <stdio.h> static int i; int main (void) { i = -1; switch ((signed char) i) { case 255: printf("255\n"); break; default: printf("default\n"); break; } }
Compiling command : g++ 1.cpp && ./a.out result : 255 The expected result: default According to C++ standard, an integral promotion of expression "(signed char) i" should be performed firstly, the result of control expression should be 0xffff; then the label of case statment will be converted to int-type. So the expected result should be default in my opinion. Thanks very much. C++ standard: The condition shall be of integral type, enumeration type, or of a class type for which a single conversion function to integral or enumeration type exists (12.3). If the condition is of class type, the condition is converted by calling that conversion function, and the result of the conversion is used in place of the original condition for the remainder of this section. Integral promotions are performed. Any statement within the switch statement can be labeled with one or more case labels as follows: case constant-expression : where the constant-expression shall be an integral constant-expression. The integral constant-expression (5.19) is implicitly converted to the promoted type of the switch condition. -- Summary: The implement of Switch statment is against with C++ standard Product: gcc Version: unknown Status: UNCONFIRMED Severity: critical Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: shenrfen at gmail dot com GCC build triplet: x86 GCC host triplet: x86 GCC target triplet: x86 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40335