C++11 §2.2 翻译阶段,第 8 个短语。翻译后的翻译单元和实例化单元组合如下。“实例化单元”的确切含义是什么?
2 回答
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
实例化单元是模板实例化(隐式和显式)。
例如,对于这个模板:
template < typename T >
struct A
{
};
这个 :
template class A<int>;
加上上面的模板声明和定义,是一个实例化单元。