显式模板实例化 - 何时使用?

Explicit template instantiation - when is it used?(显式模板实例化 - 何时使用?)
本文介绍了显式模板实例化 - 何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

休息几周后,我试图通过模板–一书来扩展和扩展我的模板知识.David Vandevoorde 和 Nicolai M. Josuttis 的完整指南,我现在想了解的是模板的显式实例化.

After few weeks break, I'm trying to expand and extend my knowlege of templates with the book Templates – The Complete Guide by David Vandevoorde and Nicolai M. Josuttis, and what I'm trying to understand at this moment is explicit instantiation of templates.

我实际上对这种机制没有任何问题,但我无法想象我想要或想要使用此功能的情况.如果有人能向我解释这一点,我将不胜感激.

I don't actually have a problem with the mechanism as such, but I can't imagine a situation in which I would like or want to use this feature. If anyone can explain that to me I will be more than grateful.

推荐答案

直接复制自 https://docs.microsoft.com/en-us/cpp/cpp/explicit-instantiation:

您可以使用显式实例化来创建模板化类或函数的实例化,而无需在代码中实际使用它.由于这在您创建使用模板进行分发的库 (.lib) 文件时很有用,因此未实例化的模板定义不会放入对象 (.obj) 文件中.

You can use explicit instantiation to 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::stringcode>) 所以每次使用 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++.)

这篇关于显式模板实例化 - 何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)