3 ms·
IME the main problem of allowing to include headers in headers is that this quickly grows out of control, and after a while headers will include stuff that's no
by flohofwoe 5y ago
IME the main problem of allowing to include headers in headers is that this quickly grows out of control, and after a while headers will include stuff that's not actually needed (usually, no longer needed).
Then you get quickly into a situation where a header includes another header that's no longer needed by itself, but is used by another header in the same 'include tree', which in turn doesn't include the required header itself. Gets very messy very quickly.
It's much easier to notice and fix such situations in the top-level implementation file, and another advantage is that the "dependency complexity" of an implementation file is visible at a glance by looking at the include list at the top of the file.
TL;DR: It's not primarily for compilation speed, but for 'header hygiene' (but of course this will also eventually help with compilation speed as the project grows).
Another well known (at least among game devs) proponent of the the same idea is Our Machinery (granted, the whole idea makes a lot more sense in C than in C++, because in C declaration and implementation is usually much stricter separated into header and source files than in C++)
https://ourmachinery.com/post/physical-design/ https://ourmachinery.com/post/physical-design/
- account42 5y agoThe tool for deal with this is https://include-what-you-use.org/ https://include-what-you-use.org/ The idea is that each file (source or header) should include exactly those headers from which it uses things. In practice, it gets a bit more complicated has you don't want to include internal implementation headers and sometimes the same thing does not even have a canonical public header but IWUY does allow you to configure all that to your liking.
- marco_craveiro 5y agoGreat point. And this will soon be available directly from Clang tools such as clangd, which will make life much easier.
- trinovantes 5y agoHopefully explicit exports with modules in c++20 will solve the include tree problem