c++c++11assert

C++ Assumptions


Once I saw a way in C++ to assume something, for example:

int x=7;

assume (x==7);//if not right a red error will appear and program will end.

Can someone please tell me what was the exact code for that? I have done a lot of research but found nothing since I forgot the original phrase. (I want to use this for debugging)


Solution

  • You are probably looking for assert, cf. https://en.cppreference.com/w/cpp/error/assert.

    There is also static_assert, which does checking during compile time.

    There was a proposal to add more pronounced system of "assumptions" to C++, called contracts (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1866.html), but its introduction to the language is postponed. If you are learning, you don't really need to read the document under that last URL.