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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top