bool setValue(int & i) { i = 1; return true; }
…
int i = 0;
assert(setValue(i));
上边的代码,在debug版中能正常运行,但在release中,运行后i的值仍为0,setValue(i)没有执行
assert,包括MFC的ASSERT,在release中“形同虚设”,导致setValue(i)也虚设了
因此,使用assert要非常小心,只能assert简单的判断语句
要避免这样的问题还有一个办法,MFC提供VERIFY,VERIFY中的语句能够正常执行,只是不会检查值是否true。
C runtime library中没找到类似的功能
from MSDN:
In the release version of MFC, VERIFY evaluates the expression but does not print or interrupt the program. For example, if the expression is a function call, the call will be made
本文探讨了在Debug与Release两种编译模式下assert与VERIFY的行为差异。指出在Release模式中,assert会失效,而VERIFY则仍会执行表达式但不进行中断。通过示例代码说明了这一现象,并提出了在实际开发中如何正确使用断言的建议。
12万+

被折叠的 条评论
为什么被折叠?



