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<regis terfunc> & GetRegisterList ()
{
static std::list<regis terfunc> registerlist;
return registerlist;
}
static bool Register(regist erfunc func)
{
GetRegisterList ().push_back(fu nc);
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::Re gister(T::Regis ter);
class CTest : public THelper<CTest>
{
public:
static void Register()
{
printf("Registe ring CTest\n");
}
};
int main(const char * args[], int iNumArgs)
{
printf("There are %d functions registered.\n",
CMgr::GetRegist erList().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