The <cassert>
header (from the C standard <assert.h>
header) declares the assert
macro. The <cassert>
header is unique in that you can #include
it multiple times to obtain different effects (depending on whether the NDEBUG
macro is defined at the time of #include
<cassert>
).
void assert(int expression)
The assert
macro ensures the expression
is true. If so, nothing happens, and execution continues normally. If the expression evaluates to zero, assert
prints a message to the standard error file and calls abort
. The format of the message is implementation-defined, but it includes a textual representation of expression
, and the file name and line number where the assert
call appears (that is, the value of the __FILE__
and __LINE__
macros).
abort function, throw keyword
#define NDEBUG #include <cassert>
The NDEBUG
macro is not defined by <cassert>
or anywhere else in the standard C++ library. Instead, you can define the macro before including the <cassert>
header to disable the assert
macro.
In one source file, you can define and undefine NDEBUG
multiple times, each time followed by #include
<cassert>
to enable or disable the assert
macro multiple times in the same source file.
assert macro