|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - static object constructors not being invoked in a DLL
Here's the simplified code example that demonstrates that static template
objects do not get constructed when its DLL get's loaded. Unfortunately, I
have a lot of these in my code and the problem affects me critically.
---------------------------------------------------------------------
#include <iostream>
template<typename T> struct dllmain_foo
{
dllmain_foo();
};
template<typename T>
dllmain_foo::dllmain_foo()
{
std::cout << "dllmain_foo constructed" << std::endl;
}
typedef dllmain_foo<int> dllmain_foo_int;
static dllmain_foo_int dllmain_foo_int_wombat;
----------------------------------------------------------------------
I'd expect "dllmain_foo constructed" to show up when tested in a
console-mode app, but it doesn't. If I make dllmain_foo just a regular
structure (non-template), the expected output shows up.
-scooter
Jan 27 2004
Slightly better console mode program that demonstrates that the static
template object's ctor doesn't get invoked. Oddly enough, I could work
around this problem with an anonymous namespace:
#include <iostream>
//---------------------------------------------------------------------------
template<typename T>
struct problem_1
{
problem_1();
~problem_1();
};
template<typename T>
problem_1<T>::problem_1()
{
std::cout << "problem_1 ctor invoked." << std::endl;
}
template<typename T>
problem_1<T>::~problem_1()
{
std::cout << "problem_1 dtor invoked." << std::endl;
}
// Should be initialized but isnt (no output from ctor):
static problem_1<int> static_problem_1;
//---------------------------------------------------------------------------
namespace {
template<typename T>
struct problem_2
{
problem_2();
~problem_2();
};
template<typename T>
problem_2<T>::problem_2()
{
std::cout << "problem_2 ctor invoked." << std::endl;
}
template<typename T>
problem_2<T>::~problem_2()
{
std::cout << "problem_2 dtor invoked." << std::endl;
}
};
// But the anonymous namespace DTRT:
problem_2<int> anon_problem_2;
//---------------------------------------------------------------------------
int main(void)
{
std::cout << "hello from main." << std::endl;
}
Feb 02 2004
Scott Michel wrote:Slightly better console mode program that demonstrates that the static template object's ctor doesn't get invoked. Oddly enough, I could work around this problem with an anonymous namespace: Feb 02 2004
"Scott Michel" <scottm cs.ucla.edu> wrote in message news:bvmqak$2tvn$1 digitaldaemon.com...Scott Michel wrote:Slightly better console mode program that demonstrates that the static template object's ctor doesn't get invoked. Oddly enough, I could work around this problem with an anonymous namespace: Feb 06 2004
|