Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 334
sharptooth Avatar asked Feb 22 '23 12:02

sharptooth


2 Answers

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

like image 130
Dietrich Epp Avatar answered Mar 05 '23 06:03

Dietrich Epp


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.

like image 41
Basile Starynkevitch Avatar answered Mar 05 '23 04:03

Basile Starynkevitch