One of the evilnesses of the C++ Standard Library is that implementations — be they GCC's libstdc++ or the Dimkumware implementation used by MSVC — are free to have their headers include other standard headers as much as they like. This can lead to confusion. For example, the oft-given:
int main() { std::cout << "Hello world\n"; }
This is technically incorrect!
Although the most popular standard library implementations currently just so happen to have an iostream
implementation that itself includes ostream
(for any internal reason), you should include the header ostream
yourself to gain access to ostream
's operator<<
that you use above. If one day your toolchain's code gets restructured and iostream no longer includes ostream
, your build will break because you've included the wrong header.
Unlikey though it is to be a problem in this specific case, it's important in general to include precisely the right header for the job according to the documentation, and not one that just seems to work for you at any given time. Even if you don't care about immediate portability or proper correctness, then at least think about future maintainability.
The corrected snippet reads:
include // for std::ostream::operator<<
int main() { std::cout << "Hello world\n"; }