3
這裏是我的測試程序:爲什麼GCC 4.2.1在某些情況下忽略#pragma GCC診斷忽略「-Wcast-qual」?
#include <stdio.h>
#pragma GCC diagnostic ignored "-Wcast-qual"
static void proc(char *buf)
{
printf("buf=%p\n",buf);
}
int main(int argc,char **argv)
{
const char *cbuf;
char *buf = (char *)cbuf;
proc(buf);
return(0);
}
這裏是我的編譯:
$ g++ -Wcast-qual x.cpp
x.cpp: In function ‘int main(int, char**)’:
x.cpp:13: warning: cast from type ‘const char*’ to type ‘char*’ casts away constness
$
這裏是沒有-Wcast-QUAL編譯:
$ g++ x.cpp
$
我用#pragma GCC diagnostic ignored
在我的代碼的其他地方沒有問題。這裏不起作用。有人可以告訴我爲什麼嗎?