stdint for C++98 (gcc/clang)

199 views Asked by At

Why do GCC/clang complain about including cstdint but not about stdint.h when compiling for C++98?

Compiling with -std=c++98 -x c++ -Wall -Wextra -pedantic outputs for #include <cstdint>:

This file requires compiler and library support for the ISO C++ 2011 standard.

According to the C++98 standard none of the headers are defined.

2

There are 2 answers

1
user5534993 On BEST ANSWER

GCC

The #include directive is executed by the GNU C preprocessor (CPP) for C and C++ sources. The standard system directories for header files, searched at by default by CPP, contain the headers for the C Standard Library. For C++ additional directories are used for the search first. Thus even for C++ source the C Standard Library headers are available by default. The header stdint.h found is thus not the compatibility header available since C++11. But instead the C Standard Library header available since C99.

I deduced this from an old version of the GNU CPP documentation:

GCC looks in several different places for headers. [...] For C++ programs, it will also look in [...], first.

See also the GNU CPP options -nostdinc and -nostdinc++.

⚠ I assume this behavior is not backed by the C++ Standard.

8
Andrey Semashev On

The C++ standard only documents headers that are part of C++ standard library. One of such headers, since C++11, is cstdint. If you include this header in C++03 or C++98 mode, the compiler generates a warning that this header is not part of the C++ standard version you selected.

The stdint.h header is part of the C standard library and is not covered by C++ standard (other than as a reference to the C standard). This header is also described in a number of other standards, such as POSIX. C++ compilers allow including C standard library headers, as well as system headers, which is why including stdint.h works, although the types defined by that header are not placed in namespace std. In fact, it is often (but not necessarily) the case that C++ standard headers are implemented using C standard headers internally. Nevertheless, the C++ standard does not cover those headers, and indeed those headers don't depend on the C++ version you choose. Hence the compiler does not complain when you include those headers, even if in an older C++ version.