Why not include all the standard headers always?

Besides

  • namespace pollution
  • compilation time (although reducable by precompiled headers, it will hurt those compiling a large project once because they actually want to use it, and not develop - also you want to think about rebuilds which are necessary once in a while)

you named "Less figuring out which headers I need and in which header a certain function is" as a benefit. I agree so far as this can be true for well designed libraries and headers.

In practice however I experienced (at least with MFC/ATL) some errors which could be solved by figuring out the correct order of includes. On the other hand one day you want to resolve an issue which makes you travel across the included headers - now imagine yourself looking at tons of headerfiles actually having nothing to do with your code file.

My conclusion is: The time you save by including a bunch of unnecessary headers do not pay off if you have to maintain a large project later on. The more time you invest before starting including any headers, the more time you will safe afterwards - but mostly without actually recognizing it.


On your system, it might not cause much of a slowdown, but someone else might have a different experience.

In the long run, computers will continue to get faster, and compilers will continue to get more efficient. The time saved obsessing over header files is certainly less than the incremental time spent waiting for the compiler, in most small projects.

But (for an implementation that doesn't precompile or cache them) the cost will be multiplied across all the source files. This affects the speed of non-incremental builds.

So for a library used over many sources or distributed across different platforms, it might still be a good idea to cut things out every so often, and before making a public release.


Oh! I know a good one.

I have one proprietary library for making nice zip archive files out of memory data. It was designed to be multiplatform, but apparently not tested well enough on every platform including Windows.

It works great on Linux and other POSIX systems but as I tried to adopt it in my project, I've stubled upon this: How to suppress #define locally?

Both the library and winbase.h (included via the most standart windows.h) has a CreateFile entity. And, as in winbase it's just a macros, compiler don't see any problem, unless you actually try to use CreateFile in your code.

So yes, keeping your namespace clean might be a good idea.