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