From assert.h file in C:
#define assert(expr) (__ASSERT_VOID_CAST (0))
I wonder what is (__ASSERT_VOID_CAST (0))? I try to find its implementation but could not find anywhere.
From assert.h file in C:
#define assert(expr) (__ASSERT_VOID_CAST (0))
I wonder what is (__ASSERT_VOID_CAST (0))? I try to find its implementation but could not find anywhere.
From assert.h
, a 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
Well, __ASSERT_VOID_CAST
will be another macro somewhere, and when asserts are 'tuned off' it will expand to something equivalent with
((void) 0)
which is a way to get a void
expression. In older implementations assert()
just expanded to an empty string, but a void-expression will let you use the comma-operator to do wriggle it into an expression, like:
while(assert(n > 0), k/n > 10) { ... }
In the assert.h on my particular system it says:
#if defined __cplusplus && __GNUC_PREREQ (2,95)
# define __ASSERT_VOID_CAST static_cast<void>
#else
# define __ASSERT_VOID_CAST (void)
#endif
So it's a cast to void, and the reason to use it is to avoid warnings about unsed values when NDEBUG is set to true.