Why use (void)1 as a no-op in C++?

962 views Asked by At

I'm reviewing a third party codebase and see this definition of an assert macro:

#define assert( x ) \
     if( !( x ) ) { \
        ThrowException( __FILE__, __LINE__ ); \
     } else \
        ((void)1)

What's the point in (void)1? How is it better than idiomatic (void)0?

2

There are 2 answers

2
Dietrich Epp On BEST ANSWER

There's no difference between (void)1 and (void)0.

0
Basile Starynkevitch On

I think it does not matter that much (and will be optimized away by the compiler). And <cassert> is a standard C++ header (using the standard <assert.h> C header) which defines a standard assert macro, so an application should not re-define it.