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;
}
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?
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.
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.
As for if you're using Ensures
properly, then yes you are.
GSL_TERMINATE_ON_CONTRACT_VIOLATION
is removed, always calling terminate()
.