c++ - 我们什么时候需要在#undef 之前使用#ifdef?

标签 c++ c coding-style macros undef

在许多 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/

相关文章:

C编程内存子分配器的循环列表

objective-c - 是否断言在Objective C中成功创建了每个对象?

html - 使用 CSS 以适合 Selenium 的样式定位元素?

language-agnostic - 麻烦想出函数的好名字

c++ - 在 C 中使用共享库很好,但在 C++ 中使用相同的代码不好吗?

c++ - 如何用函数计算,用void函数显示?

c++ - 将 antlrcpp::Any 转换为 std::string

c - 我想知道我在 C 中如何错误地读取(或写入)数组中的元素?

c++ - 将 C++ 类拆分为文件现在不会编译

c - Eclipse CDT宏自动格式化