Here is my code:
#include <iostream>
int main()
{
int32_t i = 5;
std::cout << "i: " << i << '\n';
}
Here is the output:
$ clang++ -std=c++11 -pedantic -Wall -Wextra foo.cpp && ./a.out
i: 5
Here is my question:
The C++ standard appears to define int32_t
in cstdint
within the std
namespace.
In my code, I have neither included cstdint
nor do I use the std
namespace. Why does the compiler not complain then?
The name int32_t
also appears in the global scope of the C library header stdint.h
. This might make it globally visible also in C++.
The section [Headers] says:
... the contents of each header cname is the same as that of the corresponding header name.h as specified in the C standard library. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope of the namespace std. It is unspecified whether these names (including any overloads added in [language.support] through [thread] and [depr]) are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations.
The standard also has a blanket statement:
A C++ header may include other C++ headers.
So by including <iostream>
you are guaranteed to see the stream objects, but might also happen to get access to some other library features as well.
As these indirect includes are unspecified, the result varies between implementations. So the program should always include all the headers it needs, to be portable to a different compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With