可以使用Boost或STL显示自定义字符串的C ++断言?
题
我真的希望能够:(在C ++中)
assert( num > 0, "The number must be greater than zero!");
在C#XNA中,他们有一个方法可以完成这个:
Debug.Assert( num > 0, "The number must be greater than zero!");
是否有某种方法可以做到这一点,以便运行时给我一个意义上的完整错误,而不仅仅是“断言失败”。错误?
解决方案
您可以在大多数断言中使用它:
assert(("The number must be greater than zero!", num > 0));
其他提示
运行时通常会显示失败的表达式,因此您可以执行以下操作:
assert(num > 0 && "The number must be greater than zero!");
这不会影响逻辑,但断言失败消息将包含描述性字符串。
此外,这可以在不需要STL或Boost的情况下工作。
使用assert(num> 0&&"数字必须大于零!);
使用BOOST,您可以使用 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!");
完全按照要求行事。
不隶属于 StackOverflow