Suppose I have some template class A
with a static member A::i
:
template<typename T>
class A {
public:
static int i;
}
template<typename T>
int A<T>::i = 0;
Now suppose I want to share A<T>::i
across DLL boundaries for arbitrary T
. I found various solutions to related problems (e.g., here, and here). However, as far as I can tell, these solutions require tight constraints on the entire build chain (e.g., the main executable and DLL must be built with the same version of the same compiler). Otherwise, name mangling could result in the main executable and the DLL having two different symbols for A<T>::i
(among other problems).
The canonical, "portable" (compiler-independent) solution to sharing C++ objects / data across DLL boundaries is don't. Instead, the C++ data is usually unpacked into C-friendly primitives, passed to the DLL, and then re-packed. But in this case, I'm trying to share static data for arbitrary template classes, some of which may not have even been template-instantiated in the main executable. The data itself is templated, so I see no simple way of unpacking it into C-friendly primitives.
Basically, I want a mapping between types and data that's consistent (at a symbol level for linking) across compilers. I don't think I could use C++'s RTTI to build that mapping because it's implementation-dependent.
Is there any solution to this problem, besides a) rolling my own portable introspection system, or b) imposing tight constraints on the build chain?
EDIT: The goal is to pass class-wide data belonging to 0+ arbitrary classes across DLL boundaries. Static variables of generic classes are the intuitive implementation of this kind of data, but if I have to discard compile-time parametric polymorphism to achieve this broader goal, that's perfectly acceptable.