Boost logo

Boost :

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


Emil Dotchevski wrote:
> On Mon, Mar 15, 2010 at 11:19 AM, Roland Bock <rbock_at_[hidden]> wrote:
>
>> Emil Dotchevski wrote:
>>
>>> On Mon, Mar 15, 2010 at 10:28 AM, Roland Bock <rbock_at_[hidden]> wrote:
>>>
>>>> #define assert(cond) static_cast<void>(sizeof(cond? 0: 0));
>>>>
>>> Presumably, (void)sizeof(cond?0:0) gives you a warning for using
>>> C-style cast? :)
>>>
>> Not that I know of :-) . I prefer C++ style because they are more easy to
>> spot than C style (e.g. by searching for "static_cast").
>>
>
> Yeah, that's one of the reasons why C++ casts exist: to be able to
> spot dangerous casts in the code. Except that in this case we're not
> casting anything; static_cast here only makes other dangerous casts
> harder to spot.
>
OK, that is certainly true.
>
>>> How about disabling a few lame warnings and leaving assert() alone?
>>>
>> Sometimes, they remind me of something I had forgotten to implement
>> or to clean up.
>>
>
> Sometimes they do that, however the fact that assert leaves its
> argument unused is its major design feature. Anything you do to fool
> the compiler may also fool the optimizer. If you're OK with that risk,
> you should be OK with not defining NDEBUG to begin with.
>

Hmm, you got a point there, too.

So maybe (I still want to get rid of those warnings without disabling
them), it would be better to define a companion to assert, like this:

#include <cassert>

#ifdef NDEBUG
   #define DEBUG_CODE(some_code)
#else
   #define DEBUG_CODE(some_code) some_code
#endif

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

What do you think?

Regards,

Roland


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