Boost logo

Boost :

Subject: Re: [boost] suggestion on assertion macros
From: Roland Bock (rbock_at_[hidden])
Date: 2010-03-12 15:25:25


Steven Watanabe wrote:
> AMDG
>
> Roland Bock wrote:
>> Stewart, Robert wrote:
>>> C4800 seems ridiculous. Writing code using int as a Boolean is
>>> common to C code. Why would VC 9 inflict useless noise when
>>> compiling such code? Besides, if the variable is int, and it is
>>> needed in a Boolean context, what else would the developer do? I've
>>> seen use of the conditional operator to avoid that warning. I
>>> suspect that produces worse code than what is done when "forcing
>>> value to bool 'true' or 'false'."
>>>
>> I agree. Still, I hope that static_cast is allowed to remove C4800
>
> Nope.
>
> .\scratch.cpp(20) : warning C4800: 'int' : forcing value to bool
> 'true' or 'false' (performance warning)
>
But the rest of the warnings are gone?

In that case, adding a few more versions of check should do the trick,
right? I added one for int, now, more would probably be required, I guess.

#include <cassert>

#ifdef NDEBUG
   #ifdef assert
      #undef assert
      class assert_helper
      {
      public:
         static const bool check(const bool&) { return true; }
         static const bool check(const int&) { return true; }
      };
      #define assert(cond) static_cast<void>(sizeof(assert_helper::check(cond)));
   #endif
#endif

int main()
{
  int i = 0;
  assert(i);
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk