$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] suggestion on assertion macros
From: Roland Bock (rbock_at_[hidden])
Date: 2010-03-12 13:31:43
DE wrote:
> there were a discussion recentrly about verifying arguments of
> assertion macros while checks are turned off
>   
I guess this would be closer to what you want:
// ------------------------------------------
#include <cassert>
#ifdef NDEBUG
   #ifdef assert
      #undef assert
      static bool assert_helper(const bool&) { return true; }
      #define assert(cond) if (sizeof(assert_helper(cond))) {}
   #endif
#endif
// ------------------------------------------
The following compiles without warning, regardless of NDEBUG being 
defined or not (I used g++ test.cpp -Wall -Wextra -O3):
// --------------------
int main()
{
   int i = 0;
   assert(i);
}
// --------------------
And the next snippet always fails, regardless of NDEBUG being defined or 
not, because an instance of A cannot be interpreted as bool.
// --------------------
class A {};
int main()
{
   assert(A());
}
// ---------------------
And finally, the next snippet demonstrates that it is still safely 
optimized away in case of NDEBUG being defined.
// --------------------
#include <iostream>
int test()
{
   std::cerr << "NDEBUG is OFF" << std::endl;
   return 0;
}
class A {};
int main() {
   assert(test());
}
// ---------------------
Regards,
Roland