Phase 7: White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.
Phase 8: Translated translation units and instantiation units are combined as follows: Each translated translation unit is examined to produce a list of required instantiations. The definitions of the required templates are located. It is implementation-defined whether the source of the translation units containing these definitions is required to be available. All the required instantiations are performed to produce instantiation units.[ Note: These are similar to translated translation units, but contain no references to uninstantiated templates and no template definitions. —end note ] The program is ill-formed if any instantiation fails.
我删除了一些注释以缩短粘贴的文本。
为什么在C++的编译阶段8中翻译单元已经被翻译后,模板定位和实例化才被执行?考虑到两阶段名称查找,这些步骤不应该在阶段 7 中进行吗?
最佳答案
这是 now-deprecated 的遗留物导出模板 功能。导出模板允许类和函数模板的定义与其声明分开(在适当的意义上)。
作为一个实现细节,将翻译单元翻译与模板实例化分开是有意义的,因为它可以减少实例化模板的冗余工作。然而,大多数当前的编译器在翻译单元翻译时实例化所有模板,并在链接时消除重复实例化。
希望概念和模块能够在未来将类似的功能重新引入该语言。
关于C++11 翻译阶段 8 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12665302/