“#define assert(exp) ((void) 0)”是什么意思? 做?
I came across this preprocessor definition while reading the source code in Windows Research Kernel (WRK) 1.2:
#define assert(exp) ((void) 0)
What does this code do? Why is it defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它定义表达式assert(anything) 不执行任何操作。
据推测,所使用的环境不支持 ANSI C 断言语句,或者程序员不知道可以通过定义 NDEBUG 来禁用它。
It defines the expression assert(anything) to do nothing.
Presumably, the environment being used does not support the ANSI C assert statement, or the programmer was unaware of the fact that it could be disabled by defining NDEBUG.
扩展一下 bdonlan 所说的内容,宏不展开为空的原因是因为如果它展开为空,则类似:
会在发布模式下编译,但不会在调试模式下编译。 它是
((void) 0)
而不仅仅是0
的原因是为了防止“语句无效”警告(或任何 MSVC 所说的警告)。To expand on what bdonlan says, the reason the macro does not expand empty is because if it did, then something like:
would compile in release mode but not in debug mode. The reason it is
((void) 0)
rather than just0
is to prevent "statement with no effect" warnings (or whatever MSVC calls them).补充一下,当
NDEBUG
被定义为预处理器指令时,这也是 newlib 中 assert 的定义。 Newlib 是开源 C 库,用于 Cygwin 和嵌入式系统。来自 newlib 中的 assert 手册:
Just to add, this is the definition of assert in newlib too, when
NDEBUG
is defined as a preprocessor directive. Newlib is the open source C library that is used on Cygwin and embedded systems.From the assert manual in newlib: