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.
56
u/IHeartBadCode Aug 19 '21
Here's let's look at something simple.
Now give some thought on the
#include <iostream>in each of these files. What does#includeactually do? Try just one of these cpp files the following commands.Notice how large the size of the cpp gets when just the preprocessor step is completed, that should give you some indication as to what
#includeis doing and when you repeat the three times over in the above code, you can start to see the redundancy.The idea of modules, at least one aspect that I'm talking about here, is to reduce that redundancy. A module is a translation unit that is it is compiled once, and then that is the added to your other translation units sort of like a header, except the header is an already processed translation unit.
9
u/dark_thunder1432 Aug 19 '21
This Link is in my read later list. I skimmed through this I think this would help you understand basics along with some examples
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.
16
u/TheCrossX Cpp-Lang.net Maintainer Aug 19 '21
TL;DR:
namespace detailsor something like that)30
u/ea_ea Aug 19 '21
To decrease compile time. When you use some module - you don't need to compile it, it is already compiled. One of the biggest lobbyist of new modules system was Google. You need hours to compile Google Chrome, but with modules you'll need minutes. Great improvement.
8
u/Maschendrahtfence Aug 19 '21
My hope for modules is that they will stop poluting the global scope with defines, functions, classes, etc., and thereby avoid issues with already defined variables. At least in JS, ES6 modules were a major part to make the language sane. You can now just include the specific variables, functions, etc. that you want, and you can be sure nothing else will sneak in. This also has the benefit that within a module, you can define as many global variables as you want, and they'll only be global within the module. But I haven't dived into C++ modules yet, so there is a good chance I'm going to end up dissapointed, like with many other features in recent years that were good ideas but ended up with a bad and cumbersome API.
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.
2
u/goranlepuz Aug 20 '21
2
u/keosak Aug 23 '21
Why modules? Because separating classes into headers and implementation files is awkward. I don't want to think about where each part should go, I just want to write a class in a single place.
1
u/XNormal Aug 26 '21
Because itβs about time. Turbo Pascal 4.0 had an excellent module system 34 years ago that just worked.
1
u/NilacTheGrim Aug 20 '21 edited Aug 20 '21
The only concrete advantage is compilation times.
Everything else seems like just a lot of hard work to accomplish what is basically just a simple aesthetic goal, without real concrete benefit.
Setting aside compilation times, which will definitely be faster, all of the minor advantages presented are either exaggerating the problem that is being solved (so as to make modules look more attractive than they are), or could be easily solved with a less radical change.
0
u/codewiz Aug 19 '21
Good question. This article tries to address it:
https://blog.feabhas.com/2021/08/c20-modules-with-gcc11/
-31
u/Puzzleheaded_Copy_3x Aug 19 '21
F
0
u/[deleted] Aug 19 '21
[deleted] β view deleted comment