The contents of <cassert>
are the same as the C standard library header <assert.h>
, except that a macro named static_assert
is not defined.1
Prefer <cassert>
.
All of the <xxx.h>
C headers (including <assert.h>
) are deprecated:
D.5 C standard library headers [depr.c.headers]
Update regarding the static_assert
macro from C
In D.5 [depr.c.headers], the C++ standard refers to the <xxx.h>
headers as "the C headers:
1 For compatibility with the C standard library, the C++ standard library provides the C headers shown in Table 141.
In C++14, the specification referenced C99 (ISO/IEC 9899:1999). C99 did not define the macro static_assert
(in any header). C++14 had this to say about <cassert>
in 19.3 [assertions]:
2 The contents are the same as the Standard C library header <assert.h>
.
C++17 references C11 (SO/IEC 9899:2011) which does define static_assert
in <assert.h>
, and has this to say about <cassert>
in 22.3.1 [cassert.syn]:
1 The contents are the same as the C standard library header <assert.h>
, except that a macro named static_assert
is not defined.
Both C++14 and C++17 define <assert.h>
only by reference to their respective C specifications, and also by this:
See also: ISO C 7.2.
(which is the C section that specifies <assert.h>
)
The way I read this, techincally <assert.h>
, when compiled with a C++17 compiler, actually does define a macro named static_assert
. However doing so would be pointless, and I can't imagine that any implementation actually bothers to do so.
In any event, I stand by my recommendation above:
Prefer <cassert>
.
It's just the C++ way to do things. And at least in C++98/03/11/14/17, it avoids depending on deprecated functionality. Who knows what C++20 will bring. But C++20 definitely will not deprecate <cassert>
.
1 22.3.1 Header synopsis [cassert.syn]
2 Link to the C++11 specification.
3 Link to the C++17 specification.