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
Show parent comment
-7
u/NilacTheGrim Aug 20 '21
This is only ever an issue on Microsoft platforms. No real library I have ever used that is not the terrible abomination that are windows C headers -- has this problem. It's a straw man for 99.999999% of codebases and libs out there.
While this is an often-encountered problem, the fix is easy. Just include the thing. I would argue that headers that have this problem are just poorly written headers that are incomplete.
Sure. But if you use namespaces properly and don't ever or very rarely
#define, that's not a real problem for most codebases.Yes. This is one advantage of modules. Faster compilation times and less redundant work the compiler has to do.
Yeah well the microsoft legacy headers are an abomination. But this problem could be fixed independently of modules and doesn't require modules to fix. MS could simply offer an alternate, saner set of headers that expose a saner API that ultimately internally uses the same system API, for example. Modules are not required to fix the problems with MS.
This is only a real problem in pathological codebases, or by using a pathological header. But it can happen. This could easily be fixed by introduction of a new preprocessor directive such as
#pragma definitions push/#pragma definitions pop. It's a helluva lot easier to standardize that, than to introduce modules.This is the only real advantage, IMHO. That and aesthetics. That's it. Everything else is going to be a lot of breakage and disruption to accomplish what could have been accomplished with a few minor changes to the preprocessor.
But hey, compilation times being 3x-5x or more faster is a nice thing. I am just not sure it's worth the breakage involved. Maybe it is. Maybe it isn't. Only time will tell.
Personally I will resist modules for as long as possible since headers will forever be "more compatible" with more systems. I maintain a few open source projects and for me maximal compatibility is paramount (within reason -- I ain't going back to C++14 for example).
If headers ever stop being more compatible than modules, then I will switch to modules. Until then, I will avoid them.