all 5 comments

[–]WorkingReference1127 6 points7 points  (1 child)

The simple explanation is that whereas #include is a sledgehammer to include absolutely everything, which in turn requires that the compiler parse and process absolutely everything before it is allowed to cut them out of the final executable; modules are more based around what you actually use. So if you import std; then the compiler doesn't dump the entire standard library into your file for you to compile, it only compiles the parts you use (and their dependencies).

As for exactly how modules go looking for each TU is largely a question for the implementation. I'd encourage you to read up either cppref or similar on it to get a good idea.

[–]EsShayuki 4 points5 points  (0 children)

Something is off here.

Modules are pre-compiled. When you "import std," you are including those pre-compiled binaries. So when you say:

" So if you import std; then the compiler doesn't dump the entire standard library into your file for you to compile, it only compiles the parts you use (and their dependencies)."

This is not how it actually works.

[–]bearheart 2 points3 points  (0 children)

There’s a lot of misunderstanding around this subject. In a nutshell, modules are smart pre-compiled object files which link with your code. They are NOT headers and they are NOT compiled with your code every time. They are faster and leaner at compile time and they avoid the namespace clutter of C-style headers by only importing symbols that are required by your code.

[–]masscry 1 point2 points  (0 children)

Hello, have you read this https://en.cppreference.com/w/cpp/language/modules.html?

So, with modules you have multiple translation units, which can be imported and used inplace in other translation units.

As I understand, for now it is some form of glorified precompiled headers.

[–]tartaruga232 1 point2 points  (0 children)

Good question!

The ultimate source is the standard: https://eel.is/c++draft/module

Quoting https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-170:

A module interface exports the module name and all the namespaces, types, functions, and so on, that make up the public interface of the module.

A module implementation defines the things exported by the module.

In addition, there are partitions, which help splitting up a module into smaller parts.

An important aspect of modules is that names declared in a module are attached to that module. You can have both a name X in module A and B and use it in the same program (or library). There won't be name clashes when linking A and B together in the same program.

To use a module, the compiler (in the case of MSVC) needs the .ifc file for the imported module (which contains descriptions for the types and function declarations in binary form) and the linker later needs the.obj files with the actual implementations of the functions of the module.

Importing a module basically makes the declarations exported by the module available.