Namespaces
Variants
Actions

assert

From cppreference.com
< cpp‎ | error
 
 
 
Defined in header <cassert>
Disabled assertion
(1)
#define assert(condition) ((void)0)
(until C++26)
#define assert(...)       ((void)0)
(since C++26)
Enabled assertion
(2)
#define assert(condition) /* unspecified */
(until C++26)
#define assert(...)       /* unspecified */
(since C++26)

The definition of the macro assert depends on another macro, NDEBUG, which is not defined by the standard library.

1) If NDEBUG is defined as a macro name at the point in the source code where <cassert> or <assert.h> is included, the assertion is disabled: assert does nothing.
2) Otherwise, the assertion is enabled:

assert checks if its argument (which must have scalar type):

  • If the argument compares unequal to zero, there are no further effects.
  • Otherwise, assert creates a diagnostic on the standard error stream and calls std::abort().
(until C++26)

assert puts a diagnostic test into programs and expands to an expression of type void. __VA_ARGS__ is evaluated and contextually converted to bool:

  • If the evaluation yields true, there are no further effects.
  • Otherwise, assert creates a diagnostic on the standard error stream and calls std::abort().
(since C++26)

The diagnostic information has an implementation-defined format, but it always includes the following information:

  • the text of condition
(until C++26)
  • #__VA_ARGS__
(since C++26)
  • the source file name (i.e., __FILE__)
  • the source line number (i.e., __LINE__)
  • the name of the enclosing function (i.e., __func__)

The expression assert(E) is guaranteed to be a constant subexpression, if either

  • NDEBUG is defined at the point where assert is last defined or redefined, or
  • E, contextually converted to bool, is a constant subexpression that evaluates to true.
(since C++11)

Contents

[edit] Parameters

condition - expression of scalar type

[