In what cases we need to include <cassert>?

C++Header FilesAssert

C++ Problem Overview


In what cases should we include cassert?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

Solution 3 - C++

See an easily accessible reference

#include <iostream>
// uncomment to disable assert()
// #define NDEBUG
#include <cassert>
 
int main()
{
    assert(2+2==4);
    std::cout << "Execution continues past the first assert\n";
    assert(2+2==5);
    std::cout << "Execution continues past the second assert\n";
}

Solution 4 - C++

assert.h defines one macro function that can be used as a standard debugging tool.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJaneView Question on Stackoverflow
Solution 1 - C++Cheers and hth. - AlfView Answer on Stackoverflow
Solution 2 - C++Greg HewgillView Answer on Stackoverflow
Solution 3 - C++TemplateRexView Answer on Stackoverflow
Solution 4 - C++Praveen KishorView Answer on Stackoverflow