Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

assert — checking assumptions and NDEBUG

The assert macro from <cassert> checks program assumptions at runtime. NDEBUG disables both checking and condition evaluation, so assertions are not a substitute for input validation.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <cassert>
assert(condition);

assert(condition) is a macro from <cassert>, not a function named std::assert. In C++23, when checking is enabled, the argument must be an expression of scalar type. A class object is not guaranteed to be accepted merely because it is contextually convertible to bool. The expression is evaluated: if the result compares equal to zero, the macro writes a diagnostic to the standard error stream and calls abort(). Otherwise, execution continues. The diagnostic format is implementation-defined.

If NDEBUG is defined when <cassert> is included, assert does not evaluate the condition and does nothing. Its presence matters: #define NDEBUG 0 also disables checking. Never put required side effects in the condition. Use ordinary error handling for input validation, which must not disappear this way.

This entry targets C++23; the supplied page is an evolving draft, not frozen C++23 text. Use one argument; extra parentheses, as in assert((condition)), protect commas within the expression from splitting macro arguments. The C++23 compiler flag alone does not attest to the NDEBUG setting or production runtime behavior.

Examples

#include <cassert>
#include <iostream>

int main() {
    const int count = 3;
    assert(count > 0);
    std::cout << count << '\n';
}

Result

3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources