模板中的静态成员变量,具有多个dll [英] Static member variable in template, with multiple dlls
问题描述
我的代码是建立到多个.dll文件,我有一个模板类,有一个静态成员变量。
My code is built to multiple .dll files, and I have a template class that has a static member variable.
我想要这个静态成员的同一个实例变量在所有dll中可用,但它不工作:我看到不同的实例(不同的值)在每个。
I want the same instance of this static member variable to be available in all dlls, but it doesn't work: I see different instance (different value) in each of them.
当我不使用模板,没有问题:初始化其中一个源文件中的静态成员,并在类上使用__declspec(dllexport)和__declspec(dllimport)指令。但它不适用于模板。有没有办法使它工作?
When I don't use templates, there is no problem: initialize the static member in one of the source files, and use __declspec(dllexport) and __declspec(dllimport) directives on the class. But it doesn't work with templates. Is there any way to make it work?
我看到一些提出的解决方案使用extern,但我想我不能使用,因为我的代码应该与视觉工作室2002和2005合作。
I saw some proposed solutions that use "extern", but I think I can't use it because my code is supposed to work with visual studio 2002 and 2005.
谢谢。
澄清:我想要一个不同的实例的每个不同类型的模板实例化的静态变量。但是如果我在两个不同的dll中实例化相同类型的模板,我想在两者中有相同的变量。
Clarification: I want to have a different instance of static variable per each different type of template instantiation. But if I instantiate the template with the same type in 2 different dlls, I want to have the same variable in the both of them.
推荐答案
创建模板专用化,然后导出专业化的静态成员。
Create template specialization and then export the static members of the specialization.
这篇关于模板中的静态成员变量,具有多个dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!