Templates what "phase" of compilation

Started by
1 comment, last by frob 9 years, 6 months ago

Three related questions

Is template "resolution" done by the preprocessor or the compiler or a third entity?

If the "resolution" is done by the compiler its still done in a separate pass before actual compilation right?

Is there a special name for the phase where templates get "resolved"?

Oh and when i say "resolved" is there a better term, "instantiated" maybe?

Thanks in advance for any help.

-potential energy is easily made kinetic-

Advertisement
Experimentally, it seems to me like this is the pipeline (at least for MSVC):

- Preprocessor runs
- Compiler begins running through each translation unit
- As things requiring a template instantiation are traversed, templates are instantiated, potentially generating errors


This is easy to confirm by putting a template declaration prior to a syntax error and compiling, then moving the syntax error to ahead of the template and recompiling. The order of emitted errors seems to support my theory.


This is purely speculation, of course.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

The phases of compilation are useful in compiler theory classes and logical ordering of standards documents.

The C++ language standard has 9 stages, with the footnote "Implementations must behave as if these separate phases occur, although in practice different phases might be folded together."

For instance, mapping of characters, including digraphs and trigraphs, logically happens before removing escaped symbol and unescaping unicode characters, which logically happens before tokenization, which logically happens before expanding macros, etc, which logically happen before the fourth stage of executing preprocessing directives. So those are the first four phases required by C++. In practice there is likely a single pass that simultaneously processes the characters and unescapes values and tokenizes the file, then begins executing preprocessing directives to include additional files. So logically 4 steps, but in practice probably just one.

As for the step that would involve template instantiation that would likely be this lovely paragraph, logically it is phase 8 of the 9 phases:

8. Translated translation units and instantiation units are combined as follows: [ Note: Some or all of these may be supplied from a library. —end note ] Each translated translation unit is examined to produce a list of required instantiations. [ Note: This may include instantiations which have been explicitly requested (14.7.2). —end note ] 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. [ Note: An implementation could encode sufficient information into the translated translation unit so as to ensure the source is not required here. —end note ] 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.

This topic is closed to new replies.

Advertisement