UP | HOME
../../

Assertions in C++

1 Assertions in C++ Programs

  1. #include <cassert> C++ /usr/include/c++/7.*/cassert (Version number may be newer.)
  2. #include <assert.h> C /usr/include/assert.h
  3. Defines the assert() macro. Its argument must be a valid Boolean expression.
  4. #define NDEBUG
    1. In the absence of this, the assert's boolexp gets compiled in. When executed, if it evaluates to "false", the macro-expanded compiled-in code witll print an error message including the line number of the source code, and aborts the process.
    2. In the presence of this (above the #include), the assert macro becomes empty, and there is no trace of the boolexp in the executable image.
  5. Remember: 0 is false, and non-0 is true in C/C++

2 C++ Examples with Assertions

3 References

  1. "Googletest", https://code.google.com/p/googletest/wiki/Primer#Assertions 2018 Google's C++ test framework. Recommended Reading
  2. Stephen H. Edwards, Murali Sitaraman, Bruce W. Weide, and Joseph Hollingsworth, Contract-Checking Wrappers for C++ Classes, http://www.cse.ohio-state.edu/rsrg/documents/2004/04ESWH.pdf IEEE Transactions on Software Engineering, Vol. 30, No. 11, November 2004, pp 794 - 810 Recommended Reading

4 End


Copyright © 2018 pmateti@wright.edu www.wright.edu/~pmateti