What is (__ASSERT_VOID_CAST (0))?

From assert.h file in C:

#define assert(expr) (__ASSERT_VOID_CAST (0)) 

Interestingly, what is this (__ASSERT_VOID_CAST (0))? I am trying to find its implementation, but I can not find it anywhere.

+4
source share
3 answers

Well, __ASSERT_VOID_CAST will be another macro somewhere, and when statements are disabled, it will expand to something equivalent with

 ((void) 0) 

which is a way to get the void expression. In older implementations, assert() simply expanded to an empty string, but the void expression allows you to use a comma to extract it into the expression, for example:

 while(assert(n > 0), k/n > 10) { ... } 
+4
source

Assert.h on my specific system says:

 #if defined __cplusplus && __GNUC_PREREQ (2,95) # define __ASSERT_VOID_CAST static_cast<void> #else # define __ASSERT_VOID_CAST (void) #endif 

So this is different from void, and the reason for using it is to avoid warnings about unsed values ​​when NDEBUG is set to true.

+2
source

From assert.h few lines above the definition of assert (linux, kubuntu):

 #if defined __cplusplus && __GNUC_PREREQ (2,95) # define __ASSERT_VOID_CAST static_cast<void> #else # define __ASSERT_VOID_CAST (void) #endif 
+1
source

All Articles