的部分在What are the consequences of ignoring: warning: unused parameter精神,但我有一個未使用的靜態函數,沉默未使用的靜態函數的警告代碼
#include <stdlib.h> /* EXIT_SUCCESS */
#include <stdio.h> /* fprintf */
#define ANIMAL Sloth
#include "Animal.h"
#define ANIMAL Llama
#include "Animal.h"
int main(void) {
printf("%s\n%s\n%s\n%s\n%s\n", HelloSloth(), SleepySloth(), HelloLlama(),
GoodbyeSloth(), GoodbyeLlama());
return EXIT_SUCCESS;
}
static void foo(void) {
}
Animal.h
#ifndef ANIMAL
#error ANIMAL is undefined.
#endif
#ifdef CAT
#undef CAT
#endif
#ifdef CAT_
#undef CAT_
#endif
#ifdef A_
#undef A_
#endif
#ifdef QUOTE
#undef QUOTE
#endif
#ifdef QUOTE_
#undef QUOTE_
#endif
#define CAT_(x, y) x ## y
#define CAT(x, y) CAT_(x, y)
#define A_(thing) CAT(thing, ANIMAL)
#define QUOTE_(name) #name
#define QUOTE(name) QUOTE_(name)
static const char *A_(Hello)(void) { return "Hello " QUOTE(ANIMAL) "!"; }
static const char *A_(Goodbye)(void) { return "Goodbye " QUOTE(ANIMAL) "."; }
static const char *A_(Sleepy)(void) { return QUOTE(ANIMAL) " is sleeping."; }
#undef ANIMAL
我絕對要SleepyLlama
到被一個聰明的編譯器檢測爲未使用並且被代碼優化。我不想聽到它;潛在地,當我擴展到更多的行動時,它變得分散注意力。但是,我不想幹涉foo
未被使用的可能警告。
MSVC
(14)有#pragma warning(push)
,但顯然不檢查; gcc
(4.2)和clang
有-Wunused-function
。我試過https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html,但它們似乎不適用於功能。有沒有辦法在不同的編譯器中獲得關於foo
的所有警告,而不是關於SleepyLlama
的警告?
爲什麼要這些「模板」。你可以很容易地使用一個簡單的函數來完成這項工作.... – LPs
gcc中的'Wno-unused-function'('-Wunused-function'意思是把它打開)。或者你可以在標題中使用不同的技術來實現靜態功能 –
爲什麼簡單如果可以使它變得複雜?這是一些混淆課嗎?認真:不要太看中宏!不僅代碼不易讀,而且調試也很難。集中精力編寫可讀代碼!並刪除不相關的標籤。 C和C++是不同的語言! – Olaf