C++11 翻译阶段 8 困惑

标签 c++ templates compiler-construction c++11

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/

相关文章:

c++ - 我们可以强制 shell 扩展显式调用 IShellIconOverlayIdentifier::GetOverlayInfo 吗?

c++ - 如何通过串行发送 float

c++ - 将 int(C::*)(int, char) 类型转换为 int(int, char) 类型

c - C中的链接问题

c++ - read was not declared 错误信息

c++ - #define var 在另一个#define in C

c++ - 为什么 cv::gpu::GaussianBlur 比 cv::GaussianBlur 慢?

c++ - 在这种情况下应该使用 C++ 模板吗?

c++ - 将类型序列与 iostream 混合的最简单方法?

compiler-construction - 有趣的编译器项目