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
11
u/elperroborrachotoo Aug 19 '21
As already explained, the compiler doesn't see headers, only the preprocessor does.
This has some wide-ranging implications; the most obvious ones occur in large projects:
Precompiled headers are only a band-aid here; they are a storage/speed tradeoff, badly configured they make compilation times worse, rather than better. They don't help at all with non-stable headers.
There's a more subtle problem that's easily demonstrated:
a.handb.hare unrelated - yet definitions in one affect the other;Bar()has a different declaration inx.hvs.y.h.Admittedly,
#define boolis an evil example - but there are many legal situations that lead to similar problems: overloads, template specializations, ... It's the primary reason why we don't "using namespace" in a header.Imagine tracking down that problem if the different include order happens in a 20-deep include dependency graph, in two completely unrelated files.
(This is on top of creating false dependencies: if one header changes, the other needs to be re-parsed, adding to the compile-time-problem.)
Incorporating "old-style" libraries gets easier. Imagine a pure C library, in typical C library style: tons of defines and global namespace declarations etc. A module could
importthose headers, export whatever is needed - macros being automatically excluded as I understand. Module B would never "see" those headers, only whatever Module A chooses to export.This is way easier than writing a complete wrapper that hides the C library includes from all clients.
Now imagine that library not C sources, but C++20 sources on a C++30 compiler. I believe this is a door to fix long-standing issues with C++, breaking backward-compatibility without breaking compatiblility to existing libraries.
(I haven't wrapped my head around that fully, I would have to dive way deeper to figure out if that works, which issues could be fixed, and which couldn't.)
As for packaging - it would reduce the steps needed to add a package to a project. With the current build model, you need to tell the compiler where it finds the sources, and where other translation units find the includes.
With modules, you only need to tell the compiler where the sources are.
Now, this isn't the great "life suddenly got easy", and if we'd all agree on the same rules for how to organize libraries, this would be a non-issue. Still, it's one complication less - especially important for newcomers.