我应该如何显式实例化这个模板函数以供 DLL 使用?
我有以下类和模板函数:
template <size_t num>
class String{
public:
char charArray[num];
};
template <size_t num,typename T>
void getString(String<num> & string,T number){
cout <<string.charArray<<' '<<number<<'\n';
}
然后我尝试执行如下显式实例化,将该实例化导出到 DLL,但最后发现它根本没有实例化,因为我遇到了未解决的外部链接器错误在我要导入和使用该函数的地方按链接器符号(确切的链接器错误:“未解析的外部符号”__declspec(dllimport) void _cdecl getString<5>(class String<5> &,unsigned char) (_imp_??$getString@$04@@YAXAAV?$String@$04@@E@Z) strong>") 因为在我打算实例化时未指定“num”;首先我在想,也许是因为 String
将被实现为指针下面的语法本来是一个实例化,但似乎我错了。
template<size_t num>
__declspec(dllexport) void getString(String<num> & string,unsigned char number);
现在你建议我应该如何进行实例化,因为我肯定不会对地球上找到的每个整数都这样做!
I've got following class and template function:
template <size_t num>
class String{
public:
char charArray[num];
};
template <size_t num,typename T>
void getString(String<num> & string,T number){
cout <<string.charArray<<' '<<number<<'\n';
}
then I tried to do an explicit instantiation as following to export that instantiation to a DLL but found out at last that it didn't get instantiated at all since I got a linker error of unresolved external symbol by linker at the place I was about to import and use that function (exact linker error:"unresolved external symbol "__declspec(dllimport)
void _cdecl getString<5>(class String<5> &,unsigned char) (_imp_??$getString@$04@@YAXAAV?$String@$04@@E@Z)") because "num" was not specified at the point I was intending to instantiate; at the first place I was thinking that maybe because String<num> & string
would be implemented as a pointer the following syntax would've been an instantiation but seems I was wrong.
template<size_t num>
__declspec(dllexport) void getString(String<num> & string,unsigned char number);
Now how do you suggest I should do the instantiation because I'm not certainly going to do it for every single integer number found on earth!!!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望能够实例化任意参数的函数模板,则不要将其放入源文件中。而是将其放入头文件中。
强制链接:
If it's a function template that you'd like to be able to instantiate for arbitrary parameters, then don't put it in your source files. Put it in the header file instead.
Obligatory links:
它是什么,从一开始就是一个错误的设计,我所做的根本不是一个显式实例化,因为即使对于“size_t num”的不同值,也会生成与我最初的想法相反的函数的不同实例,所以对于这种设计,我想要的显式实例化是不可能的。对于正确的设计,函数的第一个参数应该是一个数组,以使“typename T”的不同类型可以显式实例化。正确的设计如下:
What it is, is a wrong design from the beginning, What I've done is not an explicit instantiation at all because even for different values of "size_t num" different instances of the function are generated opposite to what I was thinking at first so my kind of intended explicit instantiation is impossible with this design. for a right design the first parameter of the function should be an array to make the explicit instantiation possible for different types for "typename T". The right design would be as following:
在您的标头中声明:
在您的 cpp 文件中定义:
In your header declare:
In you cpp file define: