所有模板实例化是否在编译时创建? [英] Are all template instantiations created at compile time?

查看:159
本文介绍了所有模板实例化是否在编译时创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习使用递归的可变函数模板之后,我想知道:



所有模板实例化都可以程序的执行在编译时创建?

解决方案

模板在将每个翻译的翻译单元转换为



翻译单位实际上是一个源文件。例如,三倍快)是没有模板实例化的编译输出。



实例化单元本质上是实例化模板的翻译翻译单元。



实例化是否发生在编译时取决于实现的架构。



在传统的编译对象和链接对象架构(大多数开发人员在windows或linux下工作的开发人员都会熟悉),翻译翻译单元的生成和实例化单元的生成都是编译器的阶段(可能是组合阶段)。因此,在这个模型中,实例化是一个编译时活动。



但是,有一些实现使用智能链接器,编译器输出翻译的翻译单元,描述每个翻译的翻译单元需要什么模板实例化的一些辅助信息。然后将这些转换为实例化单元的过程由链接器处理。使用这样的实现,模板实例化因此是链接时活动而不是编译时活动。这个构建模型的目的是为链接时间优化提供机会(并且链接时间模板实例化比一个目标更为副作用)。



我遇到的第一个使用智能链接器的实现是作为一个附加成本选项从Sun Microsystems在SunOS和更高版本的Solaris(默认情况下运行的工具链包含更典型的哑的链接器)。我遇到了一些其他这样的工具链,因为,但不能记得他们的供应商手头。



我不知道模板实例化在运行时发生的任何实现时间。然而,可以想象,C ++解释器可能会这样工作。


After learning about variadic function templates that use recursion, I am wondering:

Are all template instantiations that can possibly be needed during the program's execution created at compile time? Is there such thing as instantiation on-the-fly?

解决方案

Templates are instantiated in the process of converting each translated translation unit into an instantiation unit.

A translation unit is essentially a source file.

A translated translation unit (try to say that three times fast) is the output from compilation without templates instantiated.

An instantiation unit is essentially the translated translation unit with templates instantiated.

Whether the instantiation occurs at "compile time" depends on the architecture of the implementation.

In a traditional "compile to objects and link the objects" architecture (which most developers working under windows or linux will be familiar with) the generation of translated translation units and generation of instantiation units are both phases (possibly combined phases) of the compiler. So, in this model, instantiation is a compile time activity.

However, there are implementations that use a "smart linker", and the compiler outputs translated translation units, with some auxiliary information that describes what template instantiations are needed by each translated translation unit. The process of converting those into an instantiation unit is then handled by the linker. With such implementations, template instantation is therefore a link-time activity rather than a compile time activity. The intent of this build model is that it provides opportunities for link-time optimisation (and the link-time template instantiation is more a side-effect than a goal).

The first implementation with a smart linker I encountered was available as an additional-cost option from Sun Microsystems on SunOS and later Solaris (those operating systems shipped by default with a toolchain that included a more typical dumb linker). I've encountered a couple of other such toolchains since, but can't recall their vendors offhand.

I'm not aware of any implementations where template instantiation occurs at run time. Conceivably, however, a C++ interpreter might work this way.

这篇关于所有模板实例化是否在编译时创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆