4 ms·
The problem with headers (in C++) isn't #include. It's the bleed-through of macros (and pragmas, I guess) from headers into subsequent headers. Without that, pr
by Quekid5 8y ago
The problem with headers (in C++) isn't #include. It's the bleed-through of macros (and pragmas, I guess) from headers into subsequent headers. Without that, precompiled headers would be an obvious win always and every time.
Macros are side effects and there's really no sane way to constrain them (currently), but C++ modules are an attempt to do so.
- usefulcat 8y agoIn my experience, precompiled headers (as implemented by g++ at least) don't help as much as one might hope. I suspect that they only cache the parsed AST, as opposed to generated code. I wouldn't be surprised to find that generating optimized code is a lot more computationally intensive than parsing.
- oppositelock 8y agoFor complex projects, precompiled headers are massive, and sometimes slow you down! In several big C++ source bases that I've worked with, we reversed the process - at compile time, concatenate all the source files together, include everything you need just once. It's massively faster than precompiled headers, at the cost of sometimes running out of compiler heap, and inability to parallelize builds. The loss of parallelism was offset by the much faster compilation, though.
- Quekid5 8y agoI think that's a given. Generated code isn't even... generated by including a header. (I'm assuming you're thinking of templates.) You need use site "caching" to be able to reuse template parsing beyond the parsing step. I think linkers do a little bit of this, but that's after compile time. Even just caching the parse will probably be a little win in the case of complex C++ headers (esp. system headers with lots of platform defines, etc), but it's unlikely to be a panacea for compile times. I think the interesting part is what kind of tech can be built on top of the "headers are isolated" parts of the C++ modules proposal. The compiler are people are nothing if not inventive!
- malkia 8y ago... and forward declarations... https://google.github.io/styleguide/cppguide.html#Forward_Declarations https://google.github.io/styleguide/cppguide.html#Forward_De...
- einpoklum 8y agoBut this problem is gradually going away. That is, with every new language standard version, the use-cases for macros become more and more limited. Anyway, yeah, modules should do sidestep this challenge.