C++ 如何在没有明确规范的情况下从 dll 导出模板化类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/362822/
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 14:56:19  来源:igfitidea点击:

How do I export templated classes from a dll without explicit specification?

c++dlltemplates

提问by Boyan

I have a dll that contains a templated class. Is there a way to export it without explicit specification?

我有一个包含模板化类的 dll。有没有办法在没有明确规范的情况下导出它?

回答by James Hopkin

Since the code for templates is usually in headers, you don't need to export the functions at all. That is, the library that is using the dll can instantiate the template.

由于模板的代码通常位于标题中,因此您根本不需要导出函数。也就是说,使用 dll 的库可以实例化模板。

This is the only way to give users the freedom to use any type with the template, but in a sense it's working against the way dlls are supposed to work.

这是让用户可以自由地使用模板的任何类型的唯一方法,但从某种意义上说,它与 dll 应该工作的方式背道而驰。

回答by Laserallan

Are you looking into exporting an instantiation of a template class through a dll? A class along the lines:

您是否正在考虑通过 dll 导出模板类的实例?沿线的类:

typedef std::vector<int> IntVec;

There is some discussion how to do this on: http://support.microsoft.com/kb/168958

有一些讨论如何做到这一点:http: //support.microsoft.com/kb/168958

Another approach is to explicitly exporting each function you are interested in through a wrapper class working against this template instance. Then you won't clutter the dll with more symbols than you are actually interested in using.

另一种方法是通过针对此模板实例工作的包装类显式导出您感兴趣的每个函数。这样你就不会用比你实际感兴趣的更多的符号来混淆 dll。

回答by Paolo Tedesco

When the compiler finds an instantiation of a template class, like MyTemplate<int>, then it generates the code for the template specialization.
For this reason, all the template code must be placed in an header file and included where you want to use it.
If you want to 'export' your template class, just place your code in an header file and include it where it's needed.

当编译器找到模板类的实例化时,例如 MyTemplate<int>,它会生成模板特化的代码。
出于这个原因,所有模板代码都必须放在一个头文件中,并包含在您想要使用它的地方。
如果您想“导出”您的模板类,只需将您的代码放在一个头文件中并将其包含在需要的地方。

回答by Peter Driscoll

In your export control file.

在您的出口控制文件中。

#ifdef XXXX_BUILD
    #define XXXX_EXPORT __declspec(dllexport)
    #define XXXX_EXTERN
#else
    #define XXXX_EXPORT __declspec(dllimport)
    #define XXXX_EXTERN extern
#endif

where XXXX_BUILD is a symbol defined in your project.

其中 XXXX_BUILD 是项目中定义的符号。

To get your class exported.

导出您的课程。

XXXX_EXTERN template class XXXX_EXPORT YourClass<double>;

Where double is the type you want to instantiate the class with.

其中 double 是您要用于实例化类的类型。

https://support.microsoft.com/en-us/help/168958/how-to-export-an-instantiation-of-a-standard-template-library-stl-clas

https://support.microsoft.com/en-us/help/168958/how-to-export-an-instantiation-of-a-standard-template-library-stl-clas