Using asserts

An assert checks that a condition is true. The assertion means just that: the program should not continue if the condition is not true. Clearly asserts should not be called in release code and hence conditional compilation must be used. Asserts should be used to check for conditions that should never happen: never events. Since the conditions do not happen there should be no need for asserts in release builds.

The C Runtime provides the assert macro that is available through the <cassert> header file. The macro, and any functions called in the expression passed as its only parameter, will be called unless the NDEBUG symbol is defined. That is, you do not have to define the _DEBUG symbol to use asserts and you should have taken ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.