的部分在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