本文介绍了模板的显式实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此线程确实是很有帮助,但我对这个过程仍然有疑问,似乎无法解决.

This thread has been really helpful, but I still have questions about this process that don't appear to be answered.

我必须在多大程度上显式实例化模板?例如,如果在我的定义文件中我在每个函数,好友类,运算符重载等上使用模板,是否必须在模板实例化文件(我正在使用的当前方法)中实例化每个模板?

To what extent must I explicitly instantiate a template? For example, if in my definitions file I am using templates on every function, friend class, operator overload, etc, must I instantiate each in a template instantiation file (the current method that I am using)?

基于我的反复试验,答案似乎是否",而且很简单

Based off of my trial and error, the answer appears to be no, and that a simple

template class Class<type>;

将适用于该班的所有成员.但是,我已经阅读了暗示其他建议的代码,将非常感谢您提供具体的答案.

will work for all of the class's members. However, I have read code that suggests otherwise, and a concrete answer would be greatly appreciated.

推荐答案

通常,您无需显式实例化模板,只需在头文件中定义它并包含该头文件即可..但是,显式模板实例化的常见应用是您要隐藏"模板的定义.想象一下以下情况,为简单起见,我们隐藏了模板函数的实现:

In general you don't need to explicitly instantiate a template, but just define it in a header file and include that header file. However, a common application of explicit template instantiation is when you want to "hide" the definition of a template. Imagine the following situation, in which for simplicity we hide the implementation of a template function:

header.h

header.h

template<class X> void f(); // declaration

header.cpp

header.cpp

#include "header.h"

template<class X> void f(){ /* definition */ }
template void f<int>();    // explicit instantiation for int, force the compiler to generate code
template void f<double>(); // explicit instantiation for double, same

main.cpp

main.cpp

#include "header.h"

int main()
{
    f<int>(); // OK
    f<double>(); // also OK
    f<char>(); // linker error
}

如您所见,函数 f header.cpp 文件中定义(在 header.h中的 not 中定义)),因此对用户隐藏了该实现.由于 int double 的显式实例化,编译器将能够找到 f< int>() f< double>(); ,当编译 main.cpp 时.但是,当在编译 main.cpp 时尝试查找 f< char>(); 的代码时,会出现链接器错误.那是因为编译是独立完成的,并且当编译器编译 header.cpp 时,编译器仅生成 f< int> f< double< ,并且不知道我们将调用 f< char> ,因此它不会为 f< char> 生成代码.

As you can see, the function f is defined in the header.cpp file (and not in header.h), and therefore the implementation is hidden from the user. Because of the explicit instantiations for int and double, the compiler will be able to find the code of f<int>() and f<double>(); when compiling main.cpp. However, when trying to find the code for f<char>(); when compiling main.cpp, we get a linker error. That's because the compilation is done independently, and when the compiler compiles header.cpp, the compiler only generates code for f<int> and f<double>, and doesn't know we will invoke f<char> so it does not generate code for f<char>.

唯一的不足是,为了利用这种代码隐藏,我们必须显式实例化要使用它的所有类型的函数,否则会出现链接器错误.

The only catch is that in order to make use of such a code hiding, we must explicitly instantiate the function for all the types for which we want to use it, otherwise we get linker errors.

这篇关于模板的显式实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:48