I am working on a large project that uses the STL and have a question about your preferred way to organise your STL #includes.
- Do you prefer to #include each header in the source file it is used. For example, if both
foo.cppandbar.cpprequirestd::string, then both will#include <string>. - Do you prefer to have a single header file that includes all the STL headers your project uses (i.e. add them to the MS ‘stdafx.h’ pre-compiled header).
The advantage of the first method is that the .cpp file is an independent unit and can be used in a different project without having to worry that you’re missing a #include. The advantages of the second method is that you can take use your compilers pre-compiled header support plus you can wrap STL #includes in pragmas that disable some warnings (for example, some Boost headers will cause warnings when compiling at level 4).
Which do you prefer to use?
I only include the header files that are really needed in every source, and not ‘catch all’ headers, to keep dependencies (and hence compile times) as low as possible.
Precompiled headers can work irrespective of this (i.e. I rely on precompiled headers to speed up the compiling process, not to get declarations). So even if something gets declared via the included precompiled headers, I still include the ‘regular’ header, which will get skipped by the include guard mechanism and won’t add anything significant to the compile times.
As precompiled headers are a compiler specific thing. Optimizing / changing precompiled headers should have no effect on the correct functioning of the code in my opinion.
The main advantage of having dependencies as low as possible is that refactoring gets easier (or rather: feasible)
Great book on all this is Large Scale C++ Design from Lakos