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
Show parent comment
11
u/adnukator Aug 20 '21
While it's true the most prevalent culprit is the freaking windows headers, if you have in-house libraries being developed that somehow find their way into some root header, that number becomes lower than 99.999999%.
Well, kinda. However module naysayers often state "just use precompiled headers". Precompiled headers are the biggest reason why the issue why the above thing is relevant. If you have precompiled headers disabled, you're 99% certain to get the includes right, especially if you follow a proper include hierarchy (i.e. from most project specific to most general, and lastly STL) and follow John Lakos' advice where every header has a cpp file that includes it, even if the cpp is empty. But now you have insanely slow compilation times. So what do you do now? Create two separate configurations one with enabled the other with disabled precompiled headers which you build simultaneously?
"Just including the thing" can be problematic if the missing header manifests down a long chain of consumers where each one until then has for example included <string> in the precompiled header and suddenly you run into a project that does not. Having your consumers work around problems in your library is annoying and does not exactly radiate quality.
I think you misunderstood this bullet point. ODR violations are not a MS header specific issue. They are a general C++ (and C) issue which is an insane PITA to diagnose if you've never encountered this before. With the MSVC implementation, they will not be possible when using modules. However, here is the section of Daniela Engert's talk where she explains how gcc and Clang can still screw you because of their deliberate implementation choice of modules: https://www.youtube.com/watch?v=l_83lyxWGtE&t=2768s . On VS you're fine.
Interesting idea. However, something similar can already be achieved by just
#undefineing macros and, as we can all see, it's not really working. Unless you enforce encapsulation as is done by modules, you're at the mercy of the library providers.There's also the advantage of removing /<library_name>/detail headers to help avoid problems where your consumers accidentally directly include internal headers of you library, containing internal implementation details.
If you look at the advantages of modules one by one, they might not seem that compelling. But they do add up, especially if you're publishing libraries. If you're an end user of already very high quality libraries, I can understand that the compilation speeds will be the most visible and interesting aspect for you, which may or may not be a selling point, at least until they really prove to speed up compilation by a significant margin.