Home > front end >  How are C 20 modules compiled?
How are C 20 modules compiled?

Time:09-28

Some sources say that compilers parse modules and create an abstract syntax tree (AST), which is then used when parsing all code files that import the module. This would reduce the amount of parsing the compiler has to do as opposed to when #including headers, but everything would still have to be compiled once for every code file that imports a module.

Other sources say that modules are only compiled once.

How and when are modules compiled, and how does this affect inlining at compile time?

CodePudding user response:

The products of module compilation are implementation dependent. But broadly speaking, they are whatever the compiler needs them to be to make module inclusion efficient. That is, after all, the whole point of modules. When building a module interface, the compiler has 100% of the information it needs to have to make including that module interface efficient.

Module compilation has only one special interaction with "inlining": member functions of a class defined within the class definition are not implicitly given an inline declaration. That's the only effect that modules have on "inlining".

And of course, the inline keyword is not strictly about "inlining". If you put definitions of things in a module's interface files, those definitions can be available for inlining by those who import those interfaces, whether the inline keyword is used (explicitly or implicitly) or not. This was true pre-modules, and it is still true in module builds.

  • Related