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


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 🙂


Comments (5)

Cancel reply

  1. asdf says:

    Maybe you guys should implement _Pragma from C99 finally so you can temporarily disable this warning in macros.

  2. Mikhail Arkhipov (MSFT) says:

    In fact, there is a pragma for that in the compiler, but build system may have it disabled since otherwise people may start adding disabling pragmas and we end up with poor code which just seems to be compiling under W4.

  3. Michael: Can’t you just Push/Pop away that warning?

    Also, stick shifts suck. I know, I’ve got one. You need to drive a real car to realize it though 🙂

Skip to main content