Why is assert defined as (void)0?

邮差的信 提交于 2020-05-15 02:42:06

问题


Why #define assert(expression) ((void)0), rather than #define assert(expression) is used in release mode?(strictly speaking, when NDEBUG is defined)

I heard that there are some reasons, but I've forgot it.


回答1:


((void)0) defines assert(expression) to do nothing.
The main reason to use it is that #define assert(expression) would allow assert(expression) to compile without a semicolon but it will not compile if the macro is defined as ((void)0)




回答2:


The reason why ((void)0) is used in empty macros is make them behave like a function, in the sense that you need to specify the semicolon ; at the end

For example:

#define assert1(expression) (void)0
     assert(1) // compile error, missing ;

#define assert2(expression) 
     assert(1) // works


来源:https://stackoverflow.com/questions/36329271/why-is-assert-defined-as-void0

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