c++ - template class with static data member used across DLL/SO -
c++ - template class with static data member used across DLL/SO -
suppose have such template class:
template <class t> class queue { public: static int size; }; template <class t> int queue<t>::size = 0; and export function in d.dll utilize queue parameter:
void changequeuesize(queue<int>& q) { q.size = 100; } and utilize exported function in a.exe:
queue<int> q; q.size = 10; changequeuesize(q); int updatedsize = q.size; since queue class generated class template in 2 projects, there 2 copies of code, static info member.
so calling changequeuesize won't alter queue size here, update class's static member, happens have same class name.
what can solve problem? is weak symbol in gcc capable of address this? much.
you cannot set templates in library in way might think. can set actual, instantiated class definitions in library.
templates code generation tool, , can set generated code library.
you might want utilize explicit template instantiation create compiler generate code, , take static fellow member definition out of header:
// header, shipped clients template <class t> class queue { public: static int size; }; // library source code: template <typename t> int queue<t>::size = 0; template class queue<int>; now compile source file library; contain instance of static fellow member variable queue<int>::size.
note consumers able utilize instances of class t = int since don't have access static fellow member otherwise (i.e. they'd have provide own).
c++ templates static export
Comments
Post a Comment