2

I find long sequences of standard includes annoying:

#include <vector>
#include <string>
#include <sstream>
#include <iostream>

Considering these header files change only very rarely, is there a reason why I should not make a "std.h" file #including all std headers and just use that everywhere?

6 Answers 6

18

Including unnecessary header files will increase compile times.

6

You might like to add these to your project's standard, precompiled header file: if your project has a standard, precompiled header file, and if your files aren't supposed to be reusuable in other projects which might have a different standard header file.

4
  • If other projects aren't using the same standard header files, they really do need to be kept separate. Jan 19, 2009 at 19:10
  • You can (and should) have a separate precompiled header file per project. Jan 19, 2009 at 22:46
  • Because different projects may have different precompiled headers, I wouldn't rely on anything in particular being included into the project's precompiled header, if the source file is supposed to be used in more than one project.
    – ChrisW
    Jan 19, 2009 at 23:51
  • Instead, if a source file will be used in more than one project, then it should explicitly include whichever header files (including 'standard' header files) it needs.
    – ChrisW
    Jan 19, 2009 at 23:52
4

I hate to create a new answer for this, but I basically want to put both Brian Ensink and ChrisW's answer together:

First off, as Brian mentioned, your compile times will skyrocket as it's including tonnes of files every time it compiles. Luckily it looks like it's standard includes, so they shouldn't be changing all that often, but it'll still suck.

You can mitigate this by buildnig a precompiled header that'll only be re-compiled when you do a full rebuild, toast the PCH file, or change something included in it - but that's why PCH files normally only have system wide includes, or things that don't change that often.

1

If you're not actually using it, it should not be included.

0
0

It reduces your code readability (oddly enough :)). By including them all in one header file, you can no longer tell just by looking at the top of a file what translation units are including iostreams (and therefore doing IO), fstream (and doing file access) etc.

I'd suggest using an IDE that allows folding of include statements, like eclipse, and have it automatically fold the includes. That removes them from your view while still making the dependencies clear.

1
  • Hmm that's not a problem :) If not from the class name, then at least from the documentation the class's purpose must be apparent. Thanks though!
    – Iraimbilanja
    Jan 19, 2009 at 20:32
-2

This would also increase the size of your object files, which probably isn't a big deal these days.

Edited per comments that the binary would not actually be larger

4
  • Doesn't the linker eliminate unused code when linking into a binary?
    – Iraimbilanja
    Jan 19, 2009 at 19:53
  • But wouldn't code from headers that is never used be included?
    – JohnMcG
    Jan 19, 2009 at 21:07
  • Yes the preprocessor will include it, the compiler will compile it, and the linker will strip it off I believe ;)
    – Iraimbilanja
    Jan 19, 2009 at 21:19
  • Also - <vector> is a header for std::vector<T>. Templates generate code when instantiated. If vector is not used, there's no code generated, so the linker won't even need to strip it.
    – MSalters
    Jan 20, 2009 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.