3 ms·
What’s the problem with header files? Also header files exists not just because of resource constraints.
by up2isomorphism 3y ago
What’s the problem with header files? Also header files exists not just because of resource constraints.
- trealira 3y agoOne good thing that comes from it is that templated code can now go into a module and be compiled only once. Before, you had to put template classes into headers, so you effectively had to recompile, for example, the class std::vector<T>, for each file you included the header <vector> in, because a new compiler process compiles each .cpp file from scratch, knowing nothing. If two separate files instantiated std::vector<int>, then it would be instantiated and defined separately at compile time, and at link time, one of the definitions would be discarded. The only way to speed it up before modules was to use pre-compiled headers, which are compiler-specific. Modules let the compiler share state across files, so it doesn't have to recompile template instantiations unnecessarily. I think this guy's blog explains it well: https://blog.ecosta.dev/en/tech/explaining-cpp20-modules https://blog.ecosta.dev/en/tech/explaining-cpp20-modules
- maccard 3y agoExtern templates have existed since c++11 for that. > The only way to speed it up before modules was to use pre-compiled headers, which are compiler-specific. Modules are compiler specific too, unfortunately.
- trealira 3y ago>Extern templates have existed since c++11 for that. True, but ar least you don't need to write extern template class SomeTemplateClass<SomeType> in the header, and then explicitly instantiate it in the source. You can just write "export template class SomeTemplateClass<typename T> { ... };" (i.e., "export" followed by the definition) in the module file. > Modules are compiler specific too, unfortunately. Ah, I didn't realize, my bad.
- maccard 3y agoWe have a handful of expensive, widely used templates in my work project. We put the extern definition in a precompiled header and we have an externals.cpp file. It works a treat. > Ah, I didn't realize, my bad. Yeah, a huge beat was missed here. I don't envy the compiler writers, but surely we could have spent 6 months out of the last 19 years defining a common format, even if it defined <implementatiom defined blob> for the meat of the work.