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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2351148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:10:23  来源:igfitidea点击:

Explicit template instantiation - when is it used?

c++templates

提问by There is nothing we can do

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

几周休息后,我正在尝试通过 David Vandevoorde 和 Nicolai M. Josuttis的书Templates – The Complete Guide来扩展和扩展我对模板的了解,而此时我试图理解的是模板的显式实例化.

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.

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

采纳答案by kennytm

Directly copied from https://docs.microsoft.com/en-us/cpp/cpp/explicit-instantiation:

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

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 templatesfor distribution, uninstantiated template definitions are not put into object (.obj) files.

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

(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++.)

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

回答by Martin York

If you define a template class that you only want to work for a couple of explicit types.

如果您定义了一个模板类,您只想为几个显式类型工作。

Put the template declaration in the header file just like a normal class.

将模板声明像普通类一样放在头文件中。

Put the template definition in a source file just like a normal class.

将模板定义放在源文件中,就像普通类一样。

Then, at the end of the source file, explicitly instantiate only the version you want to be available.

然后,在源文件的末尾,仅显式实例化您希望可用的版本。

Silly example:

愚蠢的例子:

// StringAdapter.h
template<typename T>
class StringAdapter
{
     public:
         StringAdapter(T* data);
         void doAdapterStuff();
     private:
         std::basic_string<T> m_data;
};
typedef StringAdapter<char>    StrAdapter;
typedef StringAdapter<wchar_t> WStrAdapter;

Source:

来源:

// StringAdapter.cpp
#include "StringAdapter.h"

template<typename T>
StringAdapter<T>::StringAdapter(T* data)
    :m_data(data)
{}

template<typename T>
void StringAdapter<T>::doAdapterStuff()
{
    /* Manipulate a string */
}

// Explicitly instantiate only the classes you want to be defined.
// In this case I only want the template to work with characters but
// I want to support both char and wchar_t with the same code.
template class StringAdapter<char>;
template class StringAdapter<wchar_t>;

Main

主要的

#include "StringAdapter.h"

// Note: Main can not see the definition of the template from here (just the declaration)
//       So it relies on the explicit instantiation to make sure it links.
int main()
{
  StrAdapter  x("hi There");
  x.doAdapterStuff();
}

回答by DAmann

It depends on the compiler model - apparently there is the Borland model and the CFront model. And then it depends also on your intention - if your are writing a library, you might (as alluded above) explicitly instantiate the specializations you want.

这取决于编译器模型——显然有 Borland 模型和 CFront 模型。然后这也取决于您的意图 - 如果您正在编写一个库,您可能(如上所述)显式实例化您想要的专业化。

The GNU c++ page discusses the models here https://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Template-Instantiation.html.

GNU c++ 页面在这里讨论了模型https://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Template-Instantiation.html