Next: , Previous: Template Argument Deduction, Up: Templates


9.10 Template Definition Bindings

Specialization introduces a whole new aspect of complication to the language. When a definition is used to instantiate an object, should it be instantiated with the best-fit definition seen? In C++, the notion of point-of-instantiation is used to select the definition. [cite] Roughly, it says that only definitions that are available (complete) before the point in the translation unit are considered for instantiation. This introduces potential headaches when different translation units see different available definitions at different points of instantiation, i.e. C++ has no mechanism for enforcing consistent use of specializations across translation units. The benefit of compile-time binding of definitions is that type-checking of template definitions and uses may be done entirely at compile-time per translation unit.

HAC uses the unroll-phase as the point-of-instantiation for all instances, when all instances are bound to their proper definitions. The consequence of such a choice is that compile-time type checking is very limited with respect to template definitions. [Discuss implementation issues.]

RESOLVE: Should the following example be accepted or rejected (as a compilation unit)?

     template <pbool B>
     defproc foo() { }
     
     foo<true> bar;
     bool b = bar.x;

What if another compilation unit provides a specialization for foo<true> with a bool member x?

Likewise, consider the following similar example:

     template <pbool B>
     defproc foo(bool b) { }
     
     foo<true> bar;
     bool b = bar.x;

It is conceivable for a specialization to be defined later without b as a bool port. Should the last connection statement be accepted at compile-time? If so, does that constrain the specializations that may be introduced later?

PROPOSAL: A separate bind-phase to allow full type-checking of a compilation unit (perhaps with references to available definition families).

IDEA: Allow introduction of new template specializations that do not interfere with pre-determined bindings. Rationale: many full-specializations are introduced for one-time use and do not interfere with other instantiations' bindings.

Binding an instantiation to a definition is recursive: i.e. all members and sub-instances of a bound instantiation must already be bound. Implementation issue: bind-if-possible to automatically bind dependent instantiations.

Implementation option: eager or early binding to force definition binding and thus allow type-checking of a compilation unit that uses template definitions.

IMPLEMENTATION: Is there a need to track the instantiation statements that used particular specializations? Need to somehow catch inconsistent views of specializations...

BOTTOM LINE: Type-references to template definitions MUST have a consistent view of definitions at instantiation (unroll) time.