"#define assert(exp)((void)0)"是做什么的?

Por*_*rco 10 c assert

我在阅读Windows Research Kernel(WRK) 1.2中的源代码时遇到了这个预处理器定义:

#define assert(exp) ((void) 0)
Run Code Online (Sandbox Code Playgroud)

这段代码有什么作用?为什么定义?

bdo*_*lan 16

它将表达式assert(nothing)定义为什么都不做.

据推测,所使用的环境不支持ANSI C 断言语句,或者程序员不知道可以通过定义NDEBUG来禁用它.


Ste*_*sop 8

为了扩展bdonlan所说的内容,宏不扩展空的原因是因为如果它确实如此,那么类似于:

assert(something) // oops, missed the semi-colon
assert(another_thing);
Run Code Online (Sandbox Code Playgroud)

将在发布模式下编译,但不在调试模式下编译.原因((void) 0)不仅仅是0防止"无效声明"警告(或MSVC称之为的任何警告).