c++11 - How in c++ are handled the template classes' static attribute in different OSs? -
a quite technical question because i've problem on windows static attribute of template class...
say exemple :
a.h
template <class t> class { private: static t * obj; public: template <typename... args> static inline void ini(args... args) { if (!obj) obj = new t(std::forward<args>(args)...); } static inline t * get() { assert(obj); homecoming obj; } // [...] other methods. } template <class t> t* a<t>::obj = nullptr; // yeah, = c++11 style singleton class
b.h
class b: a<b> { //[...] }
main.cpp (executed first)
b::ini();
c0.cpp
b * b = b::get();
c1.cpp
b * b = b::get();
ok, quite simple now, compile modulary : linker automatically consider b::obj exactely same in main.o, c1.o , c0.o ? (*.o object file, not gcc) if not, how create ?
then, consider first compile c1.cpp , c0.cpp in extern dll (libc.so or libc.a/libc.lib + c.dll depending compiler/os) , main, executable, linked c.dll/so. b::obj ? should exist in both lib , exe... how create designating same thing ? or if case, how scheme handle ? can suppose there many libs using symbol (as static or dynamic pluggin).
actually, i'm asking these questions because on linux, when compiled such app, symbols correctly resolved , there 1 b::obj instance. i'm compiling same mingw , after init different lib doesn't pass assert, concluded there 1 instance per lib...
it not templates. time have static variable used in dll, uses re-create of static variable within dll. when utilize "same" variable in exe, utilize re-create within exe. changes re-create in dll not reflected in re-create used exe, , vice versa.
with difficulty (or appropriate third-party library) can create singleton pattern both dll , exe utilize same instance of singleton. can set variables have been static in singleton. template class a
can utilize singleton class store obj
, , can utilize other singleton classes elsewhere if needed.
alternatively, perhaps can redesign template obj
not static.
templates c++11 static linker
No comments:
Post a Comment