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.
I have removed some of the notes to shorten the pasted text.
Why are templates locating and instantiating performed after the translation unit has already been translated in C++’s compilation phase 8? Shouldn’t these steps take place in phase 7 considering the two-phase name look-up?
This is a holdover from the now-deprecated export templates feature. Export templates allowed the definition of class and function templates to be located separately from their declaration (in an appropriate sense).
As an implementation detail, separating translation unit translation from template instantiation can make sense, as it can reduce redundant work instantiating templates. However, most current compilers instantiate all templates at translation unit translation and eliminate duplicate instantiations at link time.
It is hoped that concepts and modules will allow a similar feature to be reintroduced to the language in future.