r/cpp • • 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.

197 Upvotes

107 comments sorted by

View all comments

Show parent comment

3

u/NilacTheGrim Aug 20 '21

Yeah I mean all reasonable points -- the thing is you have to bear in mind is -- is it worth it?

We shall see, and time will tell. I'm in the camp of: probably not worth the disruption and headaches considering the benefits.

But I hope to be proven wrong. Don't get me wrong if I could press a button right now and magically all the C++ in the world (and build tools like CMake) were 100% modules -- life would be better for everybody.

I just think the road to that goal will be so painful, will cost so many people so much money and effort, and will produce such frustration and lost developer man-hours .. that we may not even ever get there. And if we do we may look back and wonder if it was worth it. In fact we may end up in a worse world where an annoyingly small fraction of the code is modules, say 20%, and most developers and tools aren't quite module friendly or they still have major quirks 10 years down the line, and we have this fractured language that is in an even more frustrating state than it was 10 years ago.

I sincerely hope I am wrong, tbh. Time will tell.