C++ Assertions that Can Display a Custom String with Boost or STL?
Question
I really want to be able to go: (in C++)
assert( num > 0, "The number must be greater than zero!");
In C# XNA, they have a method that does exactly this:
Debug.Assert( num > 0, "The number must be greater than zero!");
Is there some way to do this so that the runtime gives me a meaning full error not just "an assertion failed" error?
Solution
You can use this in most any assertion:
assert(("The number must be greater than zero!", num > 0));
OTHER TIPS
The runtime usually displays the expression that failed, so you can do something like this:
assert(num > 0 && "The number must be greater than zero!");
Which will not affect the logic, but the assertion failure message will contain a descriptive string.
In addition, this works without needing the STL or Boost.
Use assert( num > 0 && "The number must be greater than zero!" );
Using BOOST you can use their assert as described in http://www.boost.org/doc/libs/1_50_0/libs/utility/assert.html:
#include <boost/assert.hpp>
/* ... */
BOOST_ASSERT_MSG( num > 0, "The number must be greater than zero!");
would do exactly as requested.