5

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.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 6
    There is usually a `;` afterwards, and if assert became nothing, that lonely remaining `;` might trigger a warning. – Marc Glisse Mar 31 '16 at 09:33
  • The latter would allow `assert(true)` (without semicolon), whereas the former wouldn't. – Sander De Dycker Mar 31 '16 at 09:34
  • 3
    @MarcGlisse you should post it as an answer. For now, your comment is the only statement that actually answers the poster's question. – axiac Mar 31 '16 at 09:36

2 Answers2

4

((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)

anukul
  • 1,922
  • 1
  • 19
  • 37
3

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
Jts
  • 3,447
  • 1
  • 11
  • 14