The C++ standard library provides the following headers related to iostreams:
<ios>
<iosfwd>
<istream>
<ostream>
<streambuf>
<iostream>
<fstream>
<sstream>
<strstream> [deprecated]
<iomanip>
What is the simplest, most sensible rule for when to #include
which of these headers? (If the answer is different in different versions of C++, I'm most interested in C++17. And I'm most interested in what is guaranteed to work, not which headers happen to include other headers in libstdc++ or whatever.)
I would like to believe that I can always get by with <iostream>
, <fstream>
(only if I use fstreams), and/or <sstream>
(only if I use stringstreams). This seems to work for simple programs like
#include <iostream>
int main() {
std::cout << std::hex << 42 << std::endl << std::flush;
}
But if I add std::setw(42)
to that program, then it stops compiling; I need to include <iomanip>
as well in that case.
So the rule seems to be "include <iostream>
, <fstream>
, and/or <sstream>
; and additionally include <iomanip>
if you're using any of these manipulators."
If I follow this rule religiously, will I ever run into a case where I need to include <ios>
, <iosfwd>
, <istream>
, <ostream>
, and/or <streambuf>
in my application code?