86 lines
3.1 KiB
Markdown
86 lines
3.1 KiB
Markdown
[assertions]
|
||
|
||
# 19 Diagnostics library [[diagnostics]](./#diagnostics)
|
||
|
||
## 19.3 Assertions [assertions]
|
||
|
||
### [19.3.1](#general) General [[assertions.general]](assertions.general)
|
||
|
||
[1](#general-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L452)
|
||
|
||
The header <cassert> provides a macro for documenting C++ program assertions and a mechanism
|
||
for disabling the assertion checks through defining the macro NDEBUG[.](#general-1.sentence-1)
|
||
|
||
### [19.3.2](#cassert.syn) Header <cassert> synopsis [[cassert.syn]](cassert.syn)
|
||
|
||
#define assert(...) *see below*
|
||
|
||
### [19.3.3](#assert) The assert macro [[assertions.assert]](assertions.assert)
|
||
|
||
[1](#assert-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L465)
|
||
|
||
If NDEBUG is defined as a macro name
|
||
at the point in the source file where <cassert> is included,
|
||
the assert macro is defined as#define assert(...) ((void)0)
|
||
|
||
[2](#assert-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L473)
|
||
|
||
Otherwise, the assert macro puts a diagnostic test into programs;
|
||
it expands to an expression of type void which
|
||
has the following effects:
|
||
|
||
- [(2.1)](#assert-2.1)
|
||
|
||
__VA_ARGS__ is evaluated and contextually converted to bool[.](#assert-2.1.sentence-1)
|
||
|
||
- [(2.2)](#assert-2.2)
|
||
|
||
If the evaluation yields true there are no further effects[.](#assert-2.2.sentence-1)
|
||
|
||
- [(2.3)](#assert-2.3)
|
||
|
||
Otherwise, the assert macro's expression
|
||
creates a diagnostic on the standard error stream (ISO/IEC 9899:2024, 7.23.3) in animplementation-defined
|
||
format and calls abort()[.](#assert-2.3.sentence-1)
|
||
The diagnostic contains #__VA_ARGS__ and
|
||
information on
|
||
the name of the source file,
|
||
the source line number, and
|
||
the name of the enclosing function
|
||
(such as provided by source_location::current())[.](#assert-2.3.sentence-2)
|
||
|
||
[3](#assert-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L496)
|
||
|
||
If __VA_ARGS__ does not expand to
|
||
an [*assignment-expression*](expr.assign#nt:assignment-expression "7.6.19 Assignment and compound assignment operators [expr.assign]"),
|
||
the program is ill-formed[.](#assert-3.sentence-1)
|
||
|
||
[4](#assert-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L501)
|
||
|
||
The macro assert is redefined according to
|
||
the current state of NDEBUG each time that<cassert> is included[.](#assert-4.sentence-1)
|
||
|
||
[5](#assert-5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L506)
|
||
|
||
An expression assert(E) is a [constant subexpression](defns.const.subexpr "3.15 constant subexpression [defns.const.subexpr]"), if
|
||
|
||
- [(5.1)](#assert-5.1)
|
||
|
||
NDEBUG is defined at the point where assert is last defined or redefined, or
|
||
|
||
- [(5.2)](#assert-5.2)
|
||
|
||
E [contextually converted to bool](conv#def:conversion,contextual_to_bool "7.3 Standard conversions [conv]") is a constant subexpression that evaluates to the value true[.](#assert-5.sentence-1)
|