简体   繁体   中英

How are C++20 modules compiled?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM