My assert macro is like this:
#ifdef DEBUG
#define ASSERT(x) ((void)(!(x) && assert_handler(#x, __FILE__, __LINE__) && (exit(-1), 1)))
#else
#define ASSERT(x) ((void)sizeof(x))
I thought this was more or less bulletproof but I seem to be using it a lot in the context of asserting the return value of functions which are important for their side effects. If in my release build I end up compiling
ASSERT(fgets(buffer,sizeof(buffer)/sizeof(buffer[0]),file));
which would become
((void)sizeof(fgets(buffer,sizeof(buffer)/sizeof(buffer[0]),file)));
Is there a chance this will get completely optimized out? I am fairly certain that it won’t (I’m calling a function, fgets), but what exactly is the condition that assures it? Are there any operations with side effects which the optimizer might throw out?
The usual meaning of assert is to be optimized out, so it might be better to stick to those semantics and do
If you insist on it not being optimized out, why not just do
?