My code is built to multiple .dll files, and I have a template class that has a static member variable.
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.
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?
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.
Thank you.
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.
There exists the following solution, as well:
The desciption in detail how you can do this:
Anteru's blog Explicit template instantiation
Prior to
extern template
instantiations being accepted into the draft standard it appears Microsoft implemented an extension for the VC++ compiler.The VC++ compiler will generate a warning if the non-standard extension is used; VS.NET (2003) and above refer to this warning description for details. This warning is also listed against VS 6.0.
I personally have never attempted to use this extension so I'm unable to vouch for this suggestion. Obviously I'm restricting this answer to Microsoft Visual Studio (I saw a comment from you regarding Unix) but I post in the hope that it may prove useful.
There appears to be a way to do this with fewer limitations for the code that uses the template class.
Make the static member a pointer. Create a global map which has fixed known type and can be exported from the DLL. The map uses the typeid() of the class as key and the address of the "global variable per class" as value. Initialise the static member through a function that tests whether the class already exists in the map and if so forces the second version of the class (in the second DLL) to point to the static variable of the first version of the class.
In this way every DLL has a distinct static object, but every DLL also has a pointer and all the pointers point to the same one of the static objects.
Here's some pseudo-code, assuming the static's type is the same as the template parameter (but should be easily adapted for other cases).
You already try this usage :
Note that variable need beam initialized.
More info : http://msdn.microsoft.com/en-us/library/ms997537.aspx http://www.flounder.com/hooks.htm
Good luck.
The problem is that each different template instantiation is a different type with its own static variable that is not shared with other instances that have different template parameters. You could provide a non-template base class that contains the static variable.