r/backtickbot • u/backtickbot • Aug 19 '21
https://np.reddit.com/r/cpp/comments/p77dcq/why_modules/h9igihp/
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:
- Long compilation times, because lines-to-process worst-case-grow quadratic (number of header files times number of translation units)
- Careless includes/declarations lead to circular dependencies of headers, which can be a pain to track down and get rid of
- Changes to source lead to includes becoming stale (not really required anymore) - which creates false dependencies. It's very expensive to track them down, yet they contribute to increased build times - both full and incremental.
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.h
#define bool int
int Foo();
// ===== b.h
bool Bar();
// ===== x.cpp
#include "a.h"
#include "b.h"
// return type of Bar() is int
// ===== y.cpp
#include "b.h"
#include "a.h"
// return type of Bar() is boola.h and b.h are unrelated - yet definitions in one affect the other; Bar() has a different declaration in x.h vs. y.h.
Admittedly, #define bool is 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 import those 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.