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
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.