How do modules from c++20 work?

I decided to see what changes were introduced in the 20th standard, and one of the first changes put me in a dead end: modules. I spent a long time trying to understand why they were implemented and how they should work (and I'm not talking about the syntax and usage examples).

As I understand it, modules can serve as a kind of alternative to dividing the code into header and source files, however! this division did not arise for nothing. We were forced to do so in order to do the compiler is single-pass. The compiler, when preprocessing the file, replaces all the include directives with the contents of these includes, which is a simple operation and requires only specifying where to look for these includes (if they are not in one of the standard locations). But how does the compiler handle modules? Here it meets the module import directive - its actions? This is not the name of the file, it does not know where to look for it if the file with this module has not yet been encountered. He must go through the sources in search of the place where this one is is the module being exported? Postpone the current file and start processing the others until the desired module is encountered? How do modules affect compiler performance?

 3
Author: Andrej Levkovitch, 2020-11-05

1 answers

Here in the article https://quuxplusone.github.io/blog/2019/11/07/modular-hello-world/ the answer to this question is given. The bottom line is that first you need to "precompile" the modules and the compiler generates special files. And then the classic compilation is done in the usual form.

Here it meets the module import directive - its actions?

Find the module file name in the specified paths (yes, now there will be another lib in addition to includs way)

This is not the name of the file, it does not know where to look for it if the file with this module has not yet been encountered.

This is just the name of the file (at least in the case of clang) and you have to specify where to look for it. Even if it is in the same directory (oh oh).

It should go through the sources in search of the place where this module is exported?

To do this, the programmer must first run the compiler in the special file generation mode the module.

Postpone the current file and start processing the others until the desired module is encountered?

No, it's just classic to say "I don't know what it is, I won't compile it"

How do modules affect compiler performance?

They should have a positive impact. After all, now you don't need to parse millions of lines of header code.

Here is a dock http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1441r1.pdf - where it is visible, what they got in somewhere in a couple of times to reduce the compilation time

And a little more on this topic https://cor3ntin.github.io/posts/compiletime/

 4
Author: KoVadim, 2020-11-05 13:17:13