问题
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