Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what cases we need to include <cassert>?

In what cases should we include cassert?

like image 717
Jane Avatar asked May 16 '12 19:05

Jane


People also ask

What does #include Cassert do?

it's a directive to the compiler to "include" the specified header file. and that's about it... headers contain function skeletons and various other "metadata", enough to let the compiler figure out if you're calling the included functions correctly, but doesn't contain any actual executable code.

What is #include assert H in C++?

Answer: An assert in C++ is a predefined macro using which we can test certain assumptions that are set in the program. When the conditional expression in an assert statement is set to true, the program continues normally. But when the expression is false, an error message is issued and the program is terminated.


2 Answers

In short, don't use it; use <assert.h>.

C++11 removed any formal guarantee of a "c...." header not polluting the global namespace.

It was never an in-practice guarantee, and now it's not even a formal guarantee.

Hence, with C++11 there is no longer any conceivable advantage in using the "c...." header variants, while there is the distinct and clear disadvantage that code that works well with one compiler and version of that compiler, may fail to compile with another compiler or version, due to e.g. name collisions or different overload selection in the global namespace.

SO, while cassert was pretty meaningless in C++03 (you can't put a macro in a namespace), it is totally meaningless -- even as a special case of a general scheme -- in C++11.


Addendum, Dec 22 2013:

The standard defines each C++ C header <X.h> header in terms of the <cX> header, which in turn is defined in terms of the corresponding C library header.

C++11 §D.5/2:

“Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope.”

C++11 §D.5/3 (non-normative example):

“The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std.”

Stack Overflow user C.R.’s comment made me aware that some versions of g++, such as MinGW g++ 4.7.2, are quite non-standard with respect to the <X.h> headers, lacking the overloads of e.g. sin that the C++ standard requires:

I already knew that MinGW g++ 4.7.2 also entirely lacks functions such as swprintf, and that it has ditto shortcomings in the pure C++ library such as lacking C++11 std::to_string. However, the information about it lacking the C function overloads was new to me.

In practice the lacking overloads with g++ means

  • ignoring the g++ issue, or

  • avoiding using the missing g++ overloads,
    e.g. using only double sin( double ), or

  • using the std namespace overloads
    (one then needs to include <cmath> to guarantee their presence with g++).

In order to use the g++ std namespace overloads unqualified, one practical approach is to define headers wrappers for this compiler. I've used that approach to address g++ shortcomings wrt. to the printf family. For as David Wheeler once remarked, “All problems in computer science can be solved by another level of indirection”…

Then things can be arranged so that standard code that uses g++'s missing overloads, also compiles with g++. This adjusts the compiler to the standard, with a fixed amount of code.

like image 159
Cheers and hth. - Alf Avatar answered Sep 21 '22 18:09

Cheers and hth. - Alf


Just like any other header file, you #include <cassert> when you use something declared in that header file, such as assert().

like image 36
Greg Hewgill Avatar answered Sep 17 '22 18:09

Greg Hewgill