Are all templates instantiated at compile time?

After exploring variational function templates that use recursion, I'm wondering:

Are all instances of templates necessary that are possible during the execution of a compile-time program? Is there such a thing as on-the-fly instantiation?

+3


source to share


2 answers


Templates are created by converting each translated translation unit into an instantiation unit.

The translation unit is mainly a source file.

The translated unit of translation (try to say three times faster) is the result of compiling without generating templates.

An Instance Block is essentially a translation unit with templates created.

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



In the traditional "compile objects and link objects" architecture (which will be used by most developers working under windows or linux), translation unit generation and instance unit generation are both phases (possibly with merged 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 post the translated translation units as well as some supporting information that describes which template instances are needed for each translated translation unit. The linker then handles the process of converting this data into an instance unit. Thus, with such implementations, template creation is a link-time activity rather than a compile-time activity. The purpose of this build model is that it provides opportunities to optimize connection times (and instantiating the link-time template is a side effect rather than a goal).

The first smart linker implementation I came across was available as an add-on from Sun Microsystems on SunOS and then Solaris (these operating systems ship by default with a toolchain that includes a more typical dumb linker). I have encountered several other instrumental targets since then, but cannot remember my suppliers.

I am not aware of any implementations where the template is instantiated at runtime. Of course, however, the C ++ interpreter can work this way.

+3


source


All template instances are created at compile time. Quoting from the standard:

N4296 2.2 / 1/8 [lex.phases]



The translation units and copy units are combined as follows: [Note: some or all of them may be provided from the library. - end note] Each translated translation unit is examined to create a list of required copies. [Note: this may include which have been explicitly requested (14.7.2). - end note] The required templates are defined. It is determined whether the source of translation units containing these definitions should be available. [Note: an implementation may encode enough information into a translated one to provide a source is not required here. - end note]All required instances are performed to create instances. [Note: they are similar to translated translation units, but do not contain links to unreasonable templates and template descriptions. - end of note]. A program is poorly formed if any instance fails.

+1


source







All Articles