问题描述
我一直在学习OpenCV的教程和整个断言
函数来了;它有什么作用?
I've been studying OpenCV tutorials and came across the assert
function; what does it do?
推荐答案
断言
如果参数原来将终止该程序(通常与消息引用断言语句)是假的。它在调试过程中的常用做,如果一个意想不到的情况发生时该计划失败更为明显。
assert
will terminate the program (usually with a message quoting the assert statement) if its argument turns out to be false. it's commonly used during debugging to make the program fail more obviously if an unexpected condition occurs.
例如:
assert(length >= 0); // die if length is negative.
您还可以添加更多的信息消息显示如果失败,像这样:
You can also add a more informative message to be displayed if it fails like so:
assert(length >= 0 && "Whoops, length can't possibly be negative! (didn't we just check 10 lines ago?) Tell jsmith");
要不这样的:
assert(("Length can't possibly be negative! Tell jsmith", length >= 0));
当你做一个版本(非调试)建立,还可以去除通过定义 NDEBUG评估
宏,通常是用编译器开关。这样做的必然结果是,你的程序的从不的依赖断言宏的运行。断言
语句的开销
When you're doing a release (non-debug) build, you can also remove the overhead of evaluating assert
statements by defining the NDEBUG
macro, usually with a compiler switch. The corollary of this is that your program should never rely on the assert macro running.
// BAD
assert(x++);
// GOOD
assert(x);
x++;
// Watch out! Depends on the function:
assert(foo());
// Here's a safer way:
int ret = foo();
assert(ret);
这篇关于什么是"断言"功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!