Вы можете вручную управлять экземплярами ваших статических объектов и находить и устранять дубликаты следующим образом:
myfile.h:
// Base class which will make linked list of all static instances
class Base {
protected:
// make linked list overall template instances
static Base *first, *last;
Base *prev, *next;
Base();
virtual void set_alias(Base *alias) = 0;
virtual ~Base();
static void initialize();
}
// Your template class with template static members
template<typename T>
class MyClass: public Base {
protected:
T own_data;
T *aliased_data;
virtual void set_alias(Base *alias) {
aliased_data = alias == NULL ? &own_data : ((MyClass<T>*)alias)->aliased_data;
//if (aliased_data != &own_data) {
// .... here you can merge data from two clones of template, if need
//}
}
public:
// data accessors
inline T& data() { return *aliased_data; }
inline const T& data() const { return *aliased_data; }
// single instance of class by this you can access staic data field
static MyClass instance;
}
myfile.cpp:
#include <typeinfo>
#include <string>
Base *Base::first = NULL;
Base *Base::last = NULL;
// connect each created instance to static fields
Base::Base(): prev(last), next(NULL) {
last = (first == NULL ? first : last->next) = this;
}
Base::~Base() {
(prev == NULL ? first : prev->next) = next;
(next == NULL ? last : next->prev) = prev;
}
// find all duplicates and connect it togather
// compare instances by mangled typename
// Note: Base should contain virtual methods so typeid will works proper
Base::initialize() {
for(Base *i = first; i != NULL; i = i->next)
for(Base *j = i->next; j != NULL; j = j->next)
if (std::string( typeid(*i).name() ) == typeid(*j).name())
j->set_alias(*i);
}
Как использовать:
...
// call initialize when program started and DLL
// with implementation of class Base was loaded
Base::initialize();
...
...
// call initialize again when any other dll which uses MyClass loaded
Base::initialize();
...
...
// now we can use MyClass from any function (from DLL and/or from main program)
MyClass<float>::instance.data() = 10.f;
...
...
std::cout << MyClass<float>::instance.data() << std::endl;
...
Примечание: В любом случае вам нужно выполнить dllexport и любые другие действия для экспорта и импорта функций:
Base::Base();
virtual void Base::set_alias(Base *alias) = 0;
virtual Base::~Base();
static void Base::initialize();