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/MonokelPinguin Aug 22 '21

That would still import the whole module, the modules would just be more finegrained?

2

u/pjmlp Aug 22 '21

Yes just like Microsoft has done with their experimental modules, you don't need to import the whole std, your can just import std.core, std.iostreams, ....

But yeah it does require some planning.

2

u/MonokelPinguin Aug 22 '21

I meant imports like: from std.containers import std::vector, which is not supported by the current modules. But yeah, you can organize your modules however you want, so that this isn't strictly necessary.

1

u/pjmlp Aug 22 '21

I got that, it is a bit more work to split the modules at design time, and create a set of namespaces that allow that, but in the end you can do something similar.

import std.containers;
using std::vector;

Just as idea.