在许多 C 和 C++ 文件中,我都看到过这样的宏:
#ifdef X
#undef X // no statements in between
#endif
我觉得,简单写就够了:
#undef X
如果宏 X
没有定义,那么 #undef
应该没有效果。
如果我只想取消定义一个宏,是否可以ok放置独立的#undef
?无论如何,这会使编码实践变得糟糕吗?
最佳答案
参见 ISO C99 6.10.3.5 第 2 段。
A preprocessing directive of the form
# undef identifier new-line
causes the specified identifier no longer to be defined as a macro name. It is ignored if the specified identifier is not currently defined as a macro name.
偶数 Visual C++ 6 (因不符合标准而臭名昭著)允许这样做:
You can also apply the
#undef
directive to an identifier that has no previous definition. This ensures that the identifier is undefined. Macro replacement is not performed within#undef
statements.
关于c++ - 我们什么时候需要在#undef 之前使用#ifdef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090536/