You are actually requesting two different things, although both are related to creating a template.
Why is the first part of the code compiling?
The standard states that the actual creation of the template is performed after processing the entire translation unit, which means that the actual creation of the template will be after all types defined in this translation unit are completed, even if the point instance is different and much earlier in the translation unit . More on this in this other question.
Why doesn't the second example compile?
The problem with the second example is that the standard requires that the template specialization be declared before the first use of this specialization.
Β§14.7.3p6 (C ++ 03)
If a template, a member template, or a member of a class template is explicitly specialized, then this specialization must be declared before the first use of this specialization, which will lead to an implicit implementation, in each translation unit into which such use occurs; no diagnostics required.
Please note that there are two different concepts. The instantiation point refers to where the instance is created in the code, and not when it is created. In your example, the instantiation point is the expression C<float> gc; , whereas, as in all other cases, after processing the entire transfer unit.
source share