Boost logo

Boost :

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


Peter Dimov wrote:
> Steven Watanabe:
>
>> 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)
>
> C4800 is avoided with expr? true: false. In this case, sizeof( expr?
> 0: 0 ) will also work, as we don't care about the value.
>
Oh! So the following should do the trick? That would be neat. It
certainly works for gcc.

#include <cassert>

#ifdef NDEBUG
   #ifdef assert
      #undef assert
      #define assert(cond) static_cast<void>(sizeof(cond? 0: 0));
   #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