Boost logo

Boost :

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


Mateusz Loskot wrote:
>> Currently I get compiler warnings about my_param being unused when
>> compiling with NDEBUG defined.
>>
>
> Yes, that is a problem though of different nature than categories of
> assertions.
>
>
>> I'd be glad to get rid of them. And I
>> certainly do not want to have my code sprinkled with
>>
>> #ifdef NDEBUG
>> void my_method(const int&)
>> #else
>> void my_method(const int& my_param)
>> #endif
>> {
>> assert(my_param);
>> }
>>
>
> AFAIK, this is an issue of particular implementation you use.
> The assert macro from GNU C Library wraps its argument
> cast to void if NDEBUG is defined. No warning is issued.
>
> Best regards,
>

Hmm. I use gcc (4.2.4) and I get the warning (-Wall -Wextra). That's why
I proposed that code in the other part of this discussion (with great
help from Peter and Steven):

#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);
}

This way, I get no warning about variable i being unused, it is even
being made sure that the condition for the assertion is verifiable and
still the code results in nothing if NDEBUG is defined.

I plan on using that myself. I think it would be useful for others, too.

Regards,

Roland


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