断言失败时如何打印附加信息?
如果断言失败,人们通常希望打印出附加信息。一种方法是这样的:
assert(vec.size() > i ||
!(std::cerr << "False: " << vec.size() << ">" << i))
这样,当 assert
失败时,会打印实际大小。但它很丑陋,而且很容易忘记 !
,这将使断言条件为真,程序将继续。
人们使用什么来打印有关断言失败的附加信息,就像上面一样?
Often one wants to print out additional information if an assert
fails. A way to do that is this:
assert(vec.size() > i ||
!(std::cerr << "False: " << vec.size() << ">" << i))
This way the actual sizes are printed when the assert
fails. But it's ugly, and also it's easy to forget the !
, which will make the assertion condition true and the program will just continue.
What do people use instead to print additional information on assertion failure, like above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
用法:
结果:
您可以根据需要将
DebugBreak()
或exit(-1)
或 waterever 放入宏中。更新了宏,左侧和右侧分开:
用法:
结果:
Usage:
Result:
You can optionally put
DebugBreak()
orexit(-1)
or watever into the macro, depending on your needs.Updated macro with separated left and right side:
Usage:
Result:
一般来说,我只是添加一个字符串文字来描述条件的含义:
但也许是这样的宏:
不过,如果
statement
不会有副作用,那么这里会很好,改变如何条件
评估。 :)Generally I'd just add a string literal describing the meaning of the condition:
But perhaps a macro like this:
Here it would be nice, though, if
statement
wouldn't have side-effects, changing howcondition
evaluates. :)在许多编译器的发布版本中,assert() 不会编译任何内容。它对生产代码没有任何价值。
我使用这样的结构:
程序输出:
assert() compiles to nothing in Release build of many compilers. It is not something that has any value for production code.
I use a construct like this:
Program Output:
大多数扩展断言处理程序的形式如下:
您想要的是类似于
So:
call as:
函数 build_assert_string 是微不足道的。
Most extended assertion handlers are of the form:
what you want is something along the lines of
So:
call as:
The function build_assert_string is trivial.
我觉得下面这句话很有道理。而不是这样:
只需这样做:
其中
assert_msg
定义如下:I think the following makes sense. Instead of this:
just do this:
where
assert_msg
is defined as something like this:我使用这样的东西:
use 语法有点奇怪,如
ASSERT(vec.size(), >, 1)
或ASSERT(error, ==, 0)
代码>.好处是它还打印出左侧和右侧的值。在 Windows 上,我还喜欢添加 GetLastError() 和 WSAGetLastError()。I use something like this:
The use syntax is a little weird as in
ASSERT(vec.size(), >, 1)
orASSERT(error, ==, 0)
. The upside is that it also prints out the values of the left and right hand side. On Windows I also like to throw in GetLastError() and WSAGetLastError().这是我使用的,在失败的实际行上中断,而不是在堆栈的其他地方中断。适用于 MSVC 和 GCC,并使用一点 boost 魔法并生成一个断言对话框:
Here's what I use, breaks on the actual line that failed, rather than elsewhere in the stack. Works on MSVC and GCC, and uses a little boost magic and generates an assertion dialog:
我使用 wxWidgetsif 语句或 wxASSERT_MSG >。
如果您使用框架,请查看它是否提供了一些有用的断言工具。
I use either an
if
statement or wxASSERT_MSG from wxWidgets.If you use a framework, see if it provides some useful assertion tools.
我根据 Notinlist 答案为纯 C 做了这个(谢谢!):
my_assert.c:
my_assert.h:
用法:
同时检查最合适的单元测试框架
I made this for plain C, based on Notinlist answer (thank you!):
my_assert.c:
my_assert.h:
usage:
check also the seatest unit test framework