user188276
user188276

Reputation:

what is (__ASSERT_VOID_CAST (0))?

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.

Upvotes: 4

Views: 1013

Answers (3)

Henk Holterman
Henk Holterman

Reputation: 273464

Well, __ASSERT_VOID_CAST will be another macro somewhere, and when asserts are 'turned 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 wriggle it into an expression, like:

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

Upvotes: 5

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27652

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.

Upvotes: 2

sth
sth

Reputation: 229754

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

Upvotes: 1

Related Questions