#ifdef(或其他预处理器指令)是否适用于函数声明(以测试函数是否存在)?
为什么以下代码不能按预期工作?
void foobar(int);
#ifndef foobar
printf("foobar exists");
#endif
它总是打印消息;它显然无法检测到函数作为实体的存在。 (这是一个重载问题吗?)
为什么#ifdef
(或其变体)无法检测函数声明?声明应该在预处理时可用,所以它应该有效,不是吗?如果没有,是否有替代方案或解决方法?
Why doesn’t the following code work as expected?
void foobar(int);
#ifndef foobar
printf("foobar exists");
#endif
It always prints the message; it obviously cannot detect the existence of a function as an entity. (Is it an over-loading issue?)
Why can’t #ifdef
(or its variants) detect function declarations? Declarations should be available at pre-processing, so it should work, shouldn’t it? If not, is there an alternative or work-around?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
预处理器在编译之前运行(因此称为“pre”),因此此时没有编译符号,只有文本和文本扩展。预处理器和编译器是完全分开的,并且彼此独立工作,除了预处理器修改传递给编译器的源代码这一事实之外。
使用预处理器执行类似操作的典型模式是使用相同的
define
常量将函数声明与函数用法配对:The pre-processor operates before the compilation (hence the "pre") so there are no compiled symbols at that point, just text and text expansion. The pre-procesor and the compiler are distinctly separate and work independantly of each other, except for the fact that the pre-processor modifies the source that is passed to the compiler.
The typical pattern to doing something like with the pre-processor is to pair the function declaration with the function usage using the same
define
constant:预处理器针对使用预处理器指令定义的预处理器标记进行工作。预处理器不适用于代码中声明的类型(无论是什么语言)。
您必须使用 #define 预处理器指令来声明对其他预处理器条件检查可见的标记。
The pre-processor works against pre-processor tokens that are defined using pre-processor directives. The pre-processor does not work against types declared in your code (whatever the language may be).
You must use the #define preprocessor directive to declare a token visible to other pre-processor condition checks.