且构网

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

C ++从DLL中实例化模板类

更新时间:2023-11-27 18:49:34

模板仅在编译时解决!而在两个不同的编译单元中它们将是不同的类型。 (这就是为什么使用std :: string作为参数导出函数非常危险的原因)。

Templates are resolved at compile time only ! And they are going to be different types in two different compile units. (This is the reason why it's really dangerous to export functions with std::string as parameters).

作为一个共同点,你应该明确地将模板化为你所使用的类型将要使用/允许使用。

As a consquence you should explicitely instanciate the templates to the types that you are going to use / allow to be used.

在exportimport.h文件中,应该会在您的dll中显示所有类型的模板实例。即 MatrixInterface< int>

In you exportimport.h file, there should be template instanciation of all the types you are going to expose in your dll. Namely MatrixInterface<int>.

你应该写类模板MatrixInterface< int&gt ;; 公开一种类型。

you should write class template MatrixInterface<int>; to expose one and only one type.

查看显式模板实例