Jump to content

  • Log In with Google      Sign In   
  • Create Account


I put on my robe and...

Member Since 29 Nov 2011
Offline Last Active Sep 02 2013 12:01 PM

#4888833 C++ One header to rule them all

Posted by I put on my robe and... on 29 November 2011 - 01:17 PM

Thank you very much for all of your responses! I knew this was the place to go to. I really appreciate it, and I will avoid using the "include everything" header from now on.

There was one guy who used to hang around here, I forget his handle, who actually advocated this, as long as the master header was a pre-compiled header. He said that it actually improved compile times for large projects even if the pch had to be rebuilt every time anything was modified. I don't agree with him, but there are people out there who think otherwise.

Going back to disadvantages, keep in mind that a master header's effects may extend beyond just compile times. Every compilation unit will generate it's own copy of any global variables with internal linkage or inside anonymous namespaces as well as inline function definitions and potentially language support structures such as virtual function tables. Some linkers can merge/discard duplicate copies of some of these, but some cannot and other variables cannot be discard because the language prevents it. One particular case is global variables with creation side-effects designed to work around the static initialization order problems like the Zerob counter.

Then there are the inevitable problems with identifier pollution, such as the ever so fun preprocessor definition issues that can arise if you force every single file in your project to include windows.h. Some of the preprocessor changes are well known and easy to avoid, like the regular all-caps identifiers. Some regularly bite even people who know about them like min and max. Others are just obscure out of the blue PITAs like rad0.


I especially appreciate this response. I was looking for a concrete reason as to why this would be a bad habit, but have been unable to find one. Thank you. I will go and eat crow now!

It is an awful habit, and it's something you should unlearn as soon as possible. The good practice rules are this:

For header files:
* Minimise the number of includes to the absolute bare minimum (i.e. for base classes and member variable types only).
* Forward declare classes as much as possible to reduce the number of includes.
* To solve extreme build time problems, consider using the pimpl idiom.
For source files:
* Include the class header
* Only include additional headers as and when they are needed.


Thank you for providing me with appropriate practice rules as well. Everybody's experience is much appreciated.


PARTNERS