问题描述
我试图了解如何在代码中使用Ensures()
.如示例,如果我尝试如下使用Ensures()
...
I am trying to understand how to use Ensures()
in code. As given in the example, if I tried using Ensures()
as follows...
int main(void)
{
int result = 0;
// Some calculation
Ensures(result == 255);
return 0;
}
如果result
变量不等于255
,则程序将崩溃,并显示以下输出"terminate called without an active exception"
.我的问题是如何正确使用Ensures()
?
If the result
variable is not equal to 255
, the program crashes with the following output "terminate called without an active exception"
. My question is how to use Ensures()
properly?
推荐答案
您是否正在使用 Microsoft GSL实现一个>?然后,如果您检查 gsl_assert.h
头文件,您将可以看到,如果定义了GSL_TERMINATE_ON_CONTRACT_VIOLATION
(默认设置),则Ensures
将调用 std::terminate
,它将为您提供错误信息.
Are you using the Microsoft GSL implementation? Then if you check the gsl_assert.h
header file you will see that if GSL_TERMINATE_ON_CONTRACT_VIOLATION
is defined (which is default) then Ensures
will call std::terminate
which will give you the error you get.
如果要引发异常(带有文件和行号信息),则需要在包含GSL之前定义GSL_THROW_ON_CONTRACT_VIOLATION
.
If you want an exception to be thrown (with file and line-number information) then you need to define GSL_THROW_ON_CONTRACT_VIOLATION
before including the GSL.
至于您是否正确使用了Ensures
,那么您肯定会使用.
As for if you're using Ensures
properly, then yes you are.
这篇关于sures()-准则支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!