我应该如何显式实例化这个模板函数以供 DLL 使用?

发布于 2024-10-01 03:24:52 字数 877 浏览 11 评论 0原文

我有以下类和模板函数:

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& 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-10-08 03:24:52

如果您希望能够实例化任意参数的函数模板,则不要将其放入源文件中。而是将其放入头文件中。

强制链接:

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:

等往事风中吹 2024-10-08 03:24:52

它是什么,从一开始就是一个错误的设计,我所做的根本不是一个显式实例化,因为即使对于“size_t num”的不同值,也会生成与我最初的想法相反的函数的不同实例,所以对于这种设计,我想要的显式实例化是不可能的。对于正确的设计,函数的第一个参数应该是一个数组,以使“typename T”的不同类型可以显式实例化。正确的设计如下:

template <typename T>
void getString(char string[],T number){
 cout <<string<<' '<<number<<'\n';
}
template __declspec(dllexport) void getString(char string[],unsigned char number);

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:

template <typename T>
void getString(char string[],T number){
 cout <<string<<' '<<number<<'\n';
}
template __declspec(dllexport) void getString(char string[],unsigned char number);
第七度阳光i 2024-10-08 03:24:52

在您的标头中声明:

template <size_t num,typename T>
void getString(String<num> & string,T number);

在您的 cpp 文件中定义:

template <>
void getString<42,int>(String<42,int> & string, int number){
  cout <<string.charArray<<' '<<number<<'\n';
}

In your header declare:

template <size_t num,typename T>
void getString(String<num> & string,T number);

In you cpp file define:

template <>
void getString<42,int>(String<42,int> & string, int number){
  cout <<string.charArray<<' '<<number<<'\n';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文