Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 718
shaohu Avatar asked Oct 02 '22 10:10

shaohu


2 Answers

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.

like image 158
BЈовић Avatar answered Oct 12 '22 12:10

BЈовић


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

like image 32
Cubbi Avatar answered Oct 12 '22 11:10

Cubbi