This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 24 Jan 2015 15:01:46 +0000
- Subject: [Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?
- Auto-submitted: auto-generated
- References: <bug-64767-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Last reconfirmed| |2015-01-24
Ever confirmed|0 |1
Severity|enhancement |normal
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Confirming as a bug not an extension.
In C++03 the code is valid because pointer conversions are allowed for integral
constant expressions, such as '\0', but in C++11 pointer conversions are only
allowed for an integer-literal and not a char-literal.
So this should be an error in C++11, and adding a warning in C++03 at the same
time would make sense (maybe enabled by -Wzero-as-null-pointer-constant and/or
-Wc++11-compat)
Clang gives a hard error with -std=c++11
warn.cc:4:9: error: comparison between pointer and integer ('void *' and 'int')
if (p == '\0')
~ ^ ~~~~
1 error generated.
Similarly for initializing a pointer with a char-literal, for which clang gives
an error in C++11 mode, and a warning for C++98.
warn.cc:3:9: error: cannot initialize a variable of type 'void *' with an
rvalue of type 'char'
void* p = '\0';
^ ~~~~
1 error generated.
warn.cc:3:13: warning: expression which evaluates to zero treated as a null
pointer constant of type 'void *' [-Wnon-literal-null-conversion]
void* p = '\0';
^~~~
1 warning generated.