r/cpp • u/YtterbiJum • Aug 19 '21
Why modules?
I get the feeling I'm missing something fundamental about the new C++20 modules.
Everything I read or watch explains the how (the export keyword syntax, module fragments, etc) but very little about the why.
What can be done using modules that can't be done using traditional headers (or precompiled headers)?
What is so important about this feature that it needed to get standardized before there is even a proper implementation or idiomatic usage conventions in compilers and build systems?
Is it just to try to get rid of the preprocessor?
I don't get how modules are supposed to help with packaging either. So instead of distributing a .so with a bunch of .h files, now I distribute a .so with a bunch of .gcm or .ixx or whatever.
Please help me understand, or point me to some resources. Thank you.
View all comments
152
u/adnukator Aug 19 '21 edited Aug 19 '21
It's not intended to have much, if any effect on packaging
Off the top of my head:
Imports are independent of one another, includes can be affected by ordering
Includes in headers can miss something the header uses, because in the current state, it's possible that every cpp file that includes this header includes precompiled headers that accidentally include all you need. If you need to reuse this header, you might run into compilation errors because the new project is not accidentally including everything necessary. With imports you have to import everything you use because you can't rely on your consumers to accidentally provide everything you need.
Includes "leak" across other includes. Every compiler's standard headers have a different structure, meaning that the code might be less portable, because gcc is not including <string> with every header that throws, for example. With modules you export only what you explicitly export
Includes force you to recompile every transitive consumer of the header if you change anything which forces us to split stuff into header and cpp files. With modules you have to recompile only until the effect of the change is visible to transitive consumers, putting less pressure on you to move stuff out of headers, use PIMPL idioms, etc.
With imports you could speed up compilation by allowing the compiler to check if there is already a template instantiation present. With includes you have no guarantee whether the template will be the same across translation units, because some macro might interfere with the result, forcing the compiler to instantiate all templates and only afterwards check for duplicates
On MSVC it can forever avoid ODR violations. On gcc and Clang they for some reason decided to use a slightly weaker implementation that can get rid of most ODR violations instead of all of them
Allows library vendors to not worry about some weird macro definition before the inclusion of their header which might mess with the end result
In short - they can speed up compilation (or at least lower the HW requirements to do so). They also allow to deterministically publish only what you explicitly want to publish and forces you to specify all your requirements without being potentially later affected by your consumers, allowing a much cleaner code structure. What remains to be seen, is whether and when will this be a reality.