
hi all on 14.03.2010 at 18:15 Roland Bock wrote :
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. i came to the same thing this solution works perfect with msvc80 and i feel like going to adopt it for my personal use
but i think you missed the order of macro directives if i get it right it should be #ifdef NDEBUG #ifdef assert #undef assert #endif //swapped this and the following lines #define assert(cond) static_cast<void>(sizeof(cond? 0: 0)); #endif and i agree that defining an "advanced" project-specific macro is really a better solution than redefinition of 'assert()' on improving assertions: my only concern was to propose a solution to the warning issue while NDEBUG is defined (and similar situations) i picked 'assert()' only for illustration purposes of course any other assert-like system may (or may not?? i believe it does) be improved in this specific sense my opinion is that specific assertions (like contract statements mentioned) are defined the way the author prefer, i.e. there is no typical restrictions on that so the discussion on "improving assertions" or "contract programming implementation" is not my concern for now and i leave it up to you guys -- Pavel