【发布时间】:2017-05-08 07:12:21
【问题描述】:
本着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) {
}
动物.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 被聪明的编译器检测为未使用并从代码中优化。我不想听到它;潜在地,当我扩展到更多 ANIMALs 和更多动作时,它会变得分散注意力。但是,我不想干扰关于 foo 未使用的可能警告。
MSVC (14) 有#pragma warning(push),但显然不检查; gcc (4.2) 和 clang 有 -Wunused-function。我试过https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html,但它们似乎不适用于功能。有没有办法在不同的编译器中获取关于 foo 而不是关于 SleepyLlama 的所有警告?
【问题讨论】:
-
何必纠结于那些“模板”。您可以轻松地使用一个简单的函数来完成这项工作......
-
在 gcc 中执行
Wno-unused-function(-Wunused-function表示开启)。或者您可以使用不同的技术来处理标题中的静态函数 -
如果可以把它变得复杂,为什么要简单呢?这是一些混淆类吗?说真的:不要太喜欢宏!不仅代码可读性差,而且调试起来也困难得多。专注于编写可读代码!并删除不相关的标签。 C 和 C++ 是不同的语言!
-
您要解决的实际问题是什么?
-
为什么不在虚拟包装器中使用它们?创建一个虚拟函数,它只调用生成的所有这些函数以及它自己。
标签: c visual-studio gcc compiler-warnings compiler-directives