开发者

what is (__ASSERT_VOID_CAST (0))?

From assert.h file in C:

#define assert(expr)        (__ASSERT_VOID_CAS开发者_如何学运维T (0))

I wonder what is (__ASSERT_VOID_CAST (0))? I try to find its implementation but could not find anywhere.


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) { ... }


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜