Boost logo

Boost :

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


Stewart, Robert wrote:
> Roland Bock wrote:
>
>> Steven Watanabe wrote:
>>
>>> MSVC 9.0 /W4
>>>
>>> .\scratch.cpp(14) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
>>> .\scratch.cpp(14) : warning C4127: conditional expression is constant
>>> .\scratch.cpp(7) : warning C4505: 'assert_helper' : unreferenced local function has been removed
>>>
>>>
>> OK, seems I need a sterner compiler.
>>
>
> s/sterner/noisier/?
>
> 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'."
>
> C4505 seems useless. Why would one need to know that unused code isn't included in the object code? Either it is used and will cause a link error or it isn't and its absence isn't important. Did I miss a case?
>
>
I agree. Still, I hope that static_cast is allowed to remove C4800 and
C4505 does not have a brother which talks about unreferenced classes or
their methods.

C4127 is a valid warning, of course. I used it to get rid of "statement
has no effect" warning. I just read that that kind of warning can be
gotten rid of by casting to void (works for g++ 4.2.4).

So here is my new attempt (probably the last for this week):

#include <cassert>

#ifdef NDEBUG
   #ifdef assert
      #undef assert
      class assert_helper
      {
      public:
         static const bool check(const bool&) { return true; }
      };
      #define assert(cond) static_cast<void>(sizeof(assert_helper::check(static_cast<bool>(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