Static member variable in template, with multiple dlls(模板中的静态成员变量,具有多个 dll)
问题描述
我的代码是针对多个 .dll 文件构建的,并且我有一个包含静态成员变量的模板类.
My code is built to multiple .dll files, and I have a template class that has a static member variable.
我希望在所有 dll 中都可以使用此静态成员变量的相同实例,但它不起作用:我在每个 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"的建议解决方案,但我认为我不能使用它,因为我的代码应该适用于 Visual Studio 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.
谢谢.
澄清:我希望每个不同类型的模板实例都有一个不同的静态变量实例.但是,如果我在 2 个不同的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模板中的静态成员变量,具有多个 dll
基础教程推荐
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
