マクロから展開されたコードでg ++警告を無効にしようとしています。私の理解では、_Pragma
はマクロの使用法に従うべきで、g++
してコンパイルされたときに、これはWparentheses
をトリガしないでください。G ++無視_Pragma診断
#include <stdio.h>
#define TEST(expr) \
int a = 1; \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wparentheses\"") \
if (a <= expr) { \
printf("filler\n"); \
} \
_Pragma("GCC diagnostic pop")
int main(){
int b = 2, c = 3;
TEST(b == c);
}
私はg++
でこれをコンパイルすると、私は無効にしようとしていますWparentheses
警告を取得します。
[email protected]:/mnt/c/ubuntu$ g++ -Wall -Wextra test3.c
test3.c: In function ‘int main()’:
test3.c:8:11: warning: suggest parentheses around comparison in operand of ‘==’ [-Wparentheses]
if (a <= expr) { \
^
test3.c:15:5: note: in expansion of macro ‘TEST’
TEST(b == c);
^
予想通りgcc
を使用している場合、それは動作しますが:
[email protected]:/mnt/c/ubuntu$ gcc -Wall -Wextra test3.c
test3.c: In function ‘main’:
test3.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
私はg++
バージョン4.8.5を使用しています。
gcc 4.8.5は、投稿されたソースコードを 'gcc -Wall -Wextra test3.c'でコンパイルしません。 iostreamはCヘッダファイルではないので、 'fatal error:iostream:No such file or directory'で失敗します。実際のコードを投稿してください。 –
@MikeKinghan申し訳ありませんが、質問を促した.cppファイルからコピーが貼り付けられました。今は 'printf'を使用しています – Xarn