I consider this a purely stylistic concern, since headers should have as few dependencies as possible, and consequently forward-declare as much as possible, relying on source files to supply dependencies for what they actually use non-referentially. Basically the only time I ever include something in a header is when the needed class cannot be forward-declared, which is the case pretty much exclusively for system headers and inherited classes. You can get around the whole issue of inclusion order by designing your headers correctly in the first place.
So, for the sake of having some arbitrary consistent order, I write #includes
top-down:
- System headers.
- Boost headers.
- Library headers.
- Project headers.
Each section is sorted lexicographically, case-sensitively: files beginning with a
come after those beginning with Z
. Since my class headers are in CamelCase
and utility headers are in lowercase
, these naturally fall into groups within the project headers. The header for the class that is implemented in the including source file has no special significance and is sorted along with other project headers.
You can introduce implicit dependencies this way. It is not foolproof. But defensive programming is really fearful programming, and the only way to quit being a fool when programming is to program without fear, accept the consequences for doing it wrong, and learn from your mistakes.