[This is both a C and C++ bug report, not sure how to classify that.] int a[256]; int A(char c) { return a[c]; } // C and C++ warnings, OK. int D(void) { return a['%']; } // Spurious C++ warning, no C warning int B(void) { return a['å']; } // C++ warning, missing C warning int C(void) { return a['\300']; } // C++ warning, missing C warning
So, g++ -Wchar-subscripts warns "array subscript has type 'char'" about arr['%'] even though the index is guaranteed positive. gcc does not. OTOH, gcc does not warn about a['\300'] or a['å'] (8-bit a-ring), even with -fsigned-char which ensures that the subscript is negative. Note, arr['@'] COULD be a bug in the program with characters like @ which are not in the basic execution character set: C99 5.2.1p3; I haven't got the C++ standard. Only characters in that character set are guaranteed to be positive: C99 6.2.5p3. If the program is intended to be portable to other character sets, it is buggy. Even if gcc doesn't support a machine with that charset - if anyone does these days:-) So it might make sense to have a -Wchar-subscripts=2 warning which only assumes the basic execution character set, not the current character set. In preprocessor expressions I don't know if it should check the source or execution character set, or both... -- Summary: Issues with -Wchar-subscripts Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: h dot b dot furuseth at usit dot uio dot no GCC build triplet: i686-pc-linux-gnu GCC host triplet: i686-pc-linux-gnu GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29455