重复的c ++模板实例化 [英] Duplicate c++ template instantiations
问题描述
编译器是否可以跨多个翻译单元复制同一模板的实例化?
Is it possible for the compiler to duplicate instantiations of a same template across several translation units?
例如,如果您有一个使用<$的a.cpp函数内部的c $ c> std:vector< int< int> 和b.cpp的情况相同,存在将 std :: vector< int>
在最终二进制文件中?
For instance, if you have a.cpp which use a std:vector<int>
inside a function, and same thing for b.cpp, is there a risk of having two times std::vector<int>
in the final binary?
推荐答案
正如Sharptooth所说,最终二进制文件将仅包含一个实例化。但是,每次在编译单元中遇到模板时,模板仍将被实例化。如果要提高编译速度,在C ++ 0x中,我们可以获取 extern模板
s。它的作用类似于普通的 extern
变量,因为它必须在至少一个编译单元中指定,但是编译器不会在该单元中实例化模板。请参见此处和此草案(14.7.2 [temp.explicit])以获取更多信息。
As sharptooth says, the final binary will only contain one instantiation. But the templates will still be instantiated everytime they are encountered in a compilation unit. If you want some compilation speed, in C++0x we get the extern template
s. It works like normal extern
variables, in that it has to be specified in at least one compilation unit, but the compiler wont instantiate the template in this unit. See here and this draft (14.7.2 [temp.explicit]) for more infos.
这篇关于重复的c ++模板实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!