Compiler nanny

This post reminded me on the case when C++ compiler warnings caused me more grief than benefits.

I spent some time yesterday making some [previously external] component to use asserts defined in VS headers instead of its own internal ones. Most of the time I wasted inventing ways to fool the compiler and to make trivial ASSERT definition to compile with -W4. The original definition looked like

#define ASSERT(x) do { if(x) { ... } } while(0)

Pretty standard stuff. However, compiler didn't like constants in conditionals and emitted a warning. Since VS build system enforced warning as errors, the warning would cause a build break. I didn't want to reduce warning level to 3 even temporarily. The solution was to declare external function and invoke it. The function had to be placed in a separate file so compiler wouldn't know that it always returned 0.

#define ASSERT(x) do { if(x) { ... } } while(AlwaysReturnsZero())

That fixed while(0). However, then statement

ASSERT(FALSE);

refused to compile since it turned into if(FALSE). Arrrrgh, compiler-nanny again. I hand to change the macro to

#define ASSERT(x) do { if( (x) | AlwaysReturnsZero() ) { ... } } while(AlwaysReturnsZero());

Now it compiles fine.

Bottom line: I love stick shifts. No tiptronic will ever come close :-)