Hello,
this is a test case of something I just can't explain myself:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <list>
typedef void (*registerfunc)();
class CMgr
{
public:
static std::list<registerfunc> & GetRegisterList()
{
static std::list<registerfunc> registerlist;
return registerlist;
}
static bool Register(registerfunc func)
{
GetRegisterList().push_back(func);
return true;
}
};
template< class T> class THelper
{
public:
//virtual bool GetDummy() { return ms_bDummy; }
private:
static bool ms_bDummy;
};
template <class T>
bool THelper<T>::ms_bDummy=CMgr::Register(T::Register);
class CTest : public THelper<CTest>
{
public:
static void Register()
{
printf("Registering CTest\n");
}
};
int main(const char * args[], int iNumArgs)
{
printf("There are %d functions registered.\n",
CMgr::GetRegisterList().size());
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////7
I just want a list of functions be generated when the program starts
up. The output of the program is the following:
Optimizations turned off:
"There are 0 functions registered."
Optimizations turned off and "GetDummy()" uncommented in THelper:
"There are 1 functions registered."
Leaving "GetDummy()" uncommented and turing optimizations on:
"There are 0 functions registered."
Instanciating CTest and calling GetDummy() from main():
"There are 1 functions registered."
I cannot explain this to me. When is the compiler optimizing away the
static member and not performing initialization?
Thanks a lot.
Markus