What does “#define assert(exp) ((void) 0)” do?

泄露秘密 提交于 2019-12-10 02:21:35

问题


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?


回答1:


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.




回答2:


To expand on what bdonlan says, the reason the macro does not expand empty is because if it did, then something like:

assert(something) // oops, missed the semi-colon
assert(another_thing);

would compile in release mode but not in debug mode. The reason it is ((void) 0) rather than just 0 is to prevent "statement with no effect" warnings (or whatever MSVC calls them).




回答3:


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:

The macro is defined to permit you to turn off all uses of assert at compile time by defining NDEBUG as a preprocessor variable. If you do this, the assert macro expands to (void(0))



来源:https://stackoverflow.com/questions/886013/what-does-define-assertexp-void-0-do

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!