且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

显式实例化 - 何时使用?

更新时间:2023-02-12 23:31:17

直接从 http://msdn.microsoft.com/en-us/library/by56e477%28VS.80%29.aspx


显式实例化可以创建模板类或函数的实例化,而不必在代码中实际使用。因为当您创建使用模板的库(.lib)进行分发时,这是非常有用的,未定义的模板定义不会放入对象(.obj)文件中。

Explicit instantiation lets you create an instantiation of a templated class or function without actually using it in your code. Because this is useful when you are creating library (.lib) files that use templates for distribution, uninstantiated template definitions are not put into object (.obj) files.

(例如,libstdc ++包含 std :: basic_string< char,char_traits< char>,allocator< char>因此每次使用 std :: string 的函数时,code>( std :: string ,相同的函数代码不需要复制到对象,编译器只需要引用(链接)到libstdc ++。)

(For instance, libstdc++ contains the explicit instantiation of std::basic_string<char,char_traits<char>,allocator<char> > (which is std::string) so every time you use functions of std::string, the same function code doesn't need to be copied to objects. The compiler only need to refer (link) those to libstdc++.)