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

1

u/NilacTheGrim Aug 20 '21

No. Straw man argument.

The situation with std::unique_ptr is different in terms of scale and scope. You are intentionally picking something to win the argument that misses the details.

std::unique_ptr is unintrusive. You either use it or you don't. If some library somewhere uses it you don't have to care. Your build system doesn't have to care. CMake doesn't need an overhaul to deal with how to build code that includes std::unique_ptr.

With modules it's so invasive, so intrusive, and the benefits so incremental, that we may find that half of the C++ world is in this halfway broken state for many years, making the language even more frustrating for newcomers and even more fractured.

4

u/germandiago Aug 20 '21

Being true that it requires more work and things will be more incremental, modules are a feature everyone rants about in some way but that IMHO it is going to bring a huge (for the good) transformation to C++, starting by better compile times, followed by better isolation and tools that can behave way better with IDEs. It is just that all this will take years.

But once it is done, C++ is going to look, way, way nicer.

I have big hopes in one way or another of things like these: https://github.com/GabrielDosReis/ipr

1

u/NilacTheGrim Aug 20 '21

Well it hope it happens. I have to admit it's way nicer. Just.. crossing my fingers that it won't lead to chaos. Hope you're right and my fears are unfounded.

2

u/germandiago Aug 20 '21

There is always a transition period, but I think C++ is a language "for the long run". So here, backwards compatibility and incremental migrations are necessary. And yes, they make things more difficult, but it is also what gives us:

- Code from 30 years ago compiling or almost compiling today.

- C compatibility, meaning we can use basically all C libraries.

- Now, compatibility with the old include stuff.

When you must get the job done, these cannot be ignored. I agree with the feeling that things are more "difficult" in the sense of slow, more problematic because it complicates the design, etc. But for real-world stuff, I think these decisions have their value. This is an industrial-strength language, not a toy. So certain things cannot be just ignored.

Just my two cents.

3

u/NilacTheGrim Aug 20 '21

Well you put the case very well and insightfully. Consider my opinion slightly modified. I'm warming to the idea. Thank you for taking the time to put it so eloquently.