8 ms·
This reminds me of an article I once read here about writing code in such a way that headers were included once and only once, but it required you to manually w
by andrewmcwatters 3y ago
This reminds me of an article I once read here about writing code in such a way that headers were included once and only once, but it required you to manually write the code in such a fashion. The catch was that it was distinctly not #pragma once, nor did the style require the use of header guards.
I'm wondering if anyone here remembers that article.
- lelanthran 3y agoDunno which article it was, but if you're saying that header guards don't completely alleviate the compile times, you're correct (In order to hit the guard or #pragma, the file still has to be opened and read). The way to do it is to never have any `#include` directives in headers. Put them all in the implementation source files. You have a function in a `foo.h` which returns a `bar_t` type, that is defined in `bar.h`? Then each source (.cpp or .c) file that includes `foo.h` must first include `bar.h`. While this does not completely alleviate the problem of reading the same header 10x in a project with 10 source files, it does mean that at least you don't read the same same 50x in a project with 10 source files. Each header is read once, and only once, for each translation unit. When using header guards or #pragmas, a single header file will be read multiple times because it can (and usually will) include other headers.
- andrewmcwatters 3y agoThanks for mentioning this, I believe it was a part of the strategy they used for this article I’m thinking about.