shaohu
shaohu

Reputation: 49

What's the meaning of instantiation unit in C++11 standard?

C++11 §2.2 Phases of translation, 8th phrase. Translated translation units and instantiation units are combined as follows. What's the exact meaning of "instantiation unit"?

Upvotes: 5

Views: 631

Answers (2)

Cubbi
Cubbi

Reputation: 47428

The meaning hasn't changed since the C++98 standard, since this was the original C++ compilation model - instantiation units are separate files where template instantiations encountered by the compiler in a TU are stored, so that each template instantiation is compiled only once per program.

To quote IBM compiler documentation on their -qtemplateregistry option,

When a compilation unit instantiates a new instance for the first time, the compiler creates this instance and keeps the record in a registry file. [...] When another compilation unit references the same instantiation and uses the same registry file as in the previous compilation unit, this instance will not be instantiated again. Thus, only one copy is generated for the entire program.

Oracle has a more extensive documentation on the C++ template compilation model.

GCC doesn't have an automatic repository, but the docs seem to imply that similar results can be obtained by compiling with -frepo and running collect2

Upvotes: 2

BЈовић
BЈовић

Reputation: 64223

Instantiation units are template instantiations (implicit and explicit).

For example, for this template :

template < typename T >
struct A
{
};

this :

template class A<int>;

with addition of the above template declaration and definition, is one instantiation unit.

Upvotes: 2

Related Questions