Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensures() - guideline support library

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?

like image 268
NJMR Avatar asked Apr 01 '16 06:04

NJMR


People also ask

What is GSL in c++?

The GNU Scientific Library (GSL) is a numerical library for C and C++ programmers. It is free software under the GNU General Public License. The library provides a wide range of mathematical routines such as random number generators, special functions and least-squares fitting.

What is a GSL owner?

gsl::owner<T*> marks a pointer that has ownership of the referenced object. You should use gsl::owner<T> if you can not use resource handles such as smart pointers or containers. The key point about the owner is that you have to free the resource explicitly.

What is Microsoft GSL?

Under the leadership of Raghu Ramakrishnan as Chief Technical Officer (CTO) for Azure Data, Gray Systems Lab (GSL) designs, develops, and evaluates novel database system technologies, with a focus on transitioning the best ideas into Azure Data product lines.

What is GSL Lite?

gsl-lite is a single-file header-only implementation of the C++ Core Guidelines Support Library originally based on Microsoft GSL and adapted for C++98, C++03.


1 Answers

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.


Updates on 2021

GSL_TERMINATE_ON_CONTRACT_VIOLATION is removed, always calling terminate().

like image 189
Some programmer dude Avatar answered Sep 20 '22 13:09

Some programmer dude