g++ -g -MD -MP -Wall -O -Wuninitialized -Wunused -pedantic -Wformat -Winit-self -Wreturn-type -Wswitch-enum -Wunused -Wuninitialized -Wfloat-equal -Wshadow -Wpointer-arith -Wcast-qual -Wsign-compare -Wredundant-decls -Wno-long-long -I /usr/local/include/ -DHAVE_SENDFILE -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -I/usr/lib/perl5/5.8.8/i486-linux/CORE -DHAS_SETRESUID_PROTO -DHAS_SETRESGID_PROTO -c -o slave.o slave.cpp slave.cpp: In member function 'bool CSlave::AddQueueIDToLog(SMTPData*)': slave.cpp:475: warning: cast from type 'char**' to type 'const char**' casts away constness I would call this valid it the other way round thats wrong... gcc --version gcc (Gentoo 4.3.2-r3 p1.6, pie-10.1.5) 4.3.2 This is while compiling my own programs Reproducible: Always
-Wcast-qual Warn whenever a pointer is cast so as to remove a type qualifier from the target type. For example, warn if a "const char *" is cast to an ordinary "char *".
(In reply to comment #1) > -Wcast-qual > > Warn whenever a pointer is cast so as to remove a type qualifier > from the target type. For example, warn if a "const char *" is > cast to an ordinary "char *". > thats my point casting so as to remove the const is wrong but adding it isn't.... which is what its complaining about in this case
#include <stdlib.h> int main ( int argc, char **argv ) { char *buffer[1] = { NULL }; const char **test = (const char**)buffer; return 0; } gcc -o test -Wcast-qual test.cpp test.cpp: In function 'int main(int, char**)': test.cpp:5: warning: cast from type 'char**' to type 'const char**' casts away constness
no warning if compiled with gcc 3.4.6-r2
The warning is correct. A cast from 'char**' to 'const char**' is not as harmless as you might at first think. If it were legal to convert without a cast, it would enable the following: const char a = 'a'; // this is a constant it should never change char *x; const char **y = &x; // in reality this is illegal *y = &a; // now x points to a *x = 'b'; // now we have modified a As this would allow modifying 'const' variables, it is disallowed. You can, however, convert 'char**' to 'const char* const*', which doesn't have the same issues.