简体   繁体   English

C++20 模块是如何编译的?

[英]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.一些消息来源说编译器解析模块并创建抽象语法树(AST),然后在解析导入模块的所有代码文件时使用它。 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.与 #include 头文件相比,这将减少编译器必须执行的解析量,但是对于导入模块的每个代码文件,所有内容仍然必须编译一次。

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.在构建模块接口时,编译器拥有它需要的 100% 的信息,以使包括该模块接口高效。

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.模块编译与“内联”只有一个特殊的交互:在 class 定义中定义的 class 的成员函数没有隐式地给出inline声明。 That's the only effect that modules have on "inlining".这是模块对“内联”的唯一影响。

And of course, the inline keyword is not strictly about "inlining".当然, inline关键字并不是严格意义上的“内联”。 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.如果您将事物的定义放在模块的接口文件中,那么导入这些接口的人可以使用这些定义进行内联,无论是否使用了inline关键字(显式或隐式)。 This was true pre-modules, and it is still true in module builds.这是真正的前置模块,在模块构建中仍然如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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