模板中的静态成员变量,具有多个 dll

Static member variable in template, with multiple dlls(模板中的静态成员变量,具有多个 dll)

本文介绍了模板中的静态成员变量,具有多个 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

基础教程推荐