Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

casting 0 to void

Tags:

c++

c

macros

void

On my implementation of C++ (Visual Studio 2008 implementation) I see the following line in <cassert>

#ifdef  NDEBUG
#define assert(_Expression) ((void)0)

I do not understand the need to cast 0 to void. It seems to me that

#ifdef  NDEBUG
#define assert(_Expression) (0)

or even simply

#ifdef  NDEBUG
#define assert(_Expression) 0

would do, considering the contexts in which assert(expr) can be used.

So, what's the danger of 0 of type int instead of 0 of type void in this case? Any realistic examples?

like image 366
Armen Tsirunyan Avatar asked Aug 09 '11 20:08

Armen Tsirunyan


People also ask

What does casting to void mean?

Casting to void is used to suppress compiler warnings. The Standard says in §5.2. 9/4 says, Any expression can be explicitly converted to type “cv void.” The expression value is discarded.

What does casting to void pointer do?

static_cast converts the pointer of the void* data type to the data type of that variable whose address is stored by the void pointer. In the above example, we used static_cast to convert the void pointer into an int pointer, so that we could print the contents of the variable var.

Can you cast to void in C?

Finally, a function call can be cast to void in order to explicitly discard a return value. For example, printf returns a value, but it is seldom used. Nevertheless, the two lines of code that follow are equivalent: printf ("Hullo!\

Is (( void *) 0 same as null?

NULL is a macro defined in several standard headers for the null pointer; it can be defined as ((void*)0). It is to be used for pointers only and it would cause problems with anything except pointers. The sizeof(NULL) is the same as sizeof(void*).


1 Answers

The only purpose of the complicated expression (void)0 is to avoid compiler warnings. If you just had a naked, useless expression, the compiler might warn about an expression that has no effect. But by explicitly casting something to void you indicate that you mean to do this.

(Think about how confusing it would be to the user if the compiler suddenly said, "Warning: expression 0; has no effect.", when all you've done is switched to release mode.)

This was also common practice in C, where you'd say (void)printf("Hello"); to tell the compiler that you intentionally chose to ignore the return value of the function.

The (void) cast is not merely a choice by a particular implementation; it's required by the C standard. Quoting the 2011 ISO C standard (similar wording appears in the 1990 and 1999 editions):

If NDEBUG is defined as a macro name at the point in the source file where <assert.h> is included, the assert macro is defined simply as

#define assert(ignore) ((void)0)

The C++ standard requires the contents of the <cassert> header to be the same as the Standard C <assert.h> header.

like image 56
Kerrek SB Avatar answered Oct 26 '22 11:10

Kerrek SB