Initialization of a static member of a template class fails

John (Eljay) Love-Jensen eljay@adobe.com
Thu Jul 26 18:42:00 GMT 2007


Hi Valery,

I suspect your code is running into the dreaded Order Of Construction problem.  Which is only turn-of-the-screw less painful than the even more dreaded Order Of Destruction problem.

I replaced this part of your code...

static instance_list s_instances;

...with a class function to access a function-wrapped static instance_list variable...

static instance_list& GetInstanceList()
{
  static instance_list s_instances;
  return s_instances;
}

...and the crasher (which I also saw) went away.

Of course, you won't want a class static accessor function wrapping an instance variable in a header file, since that may cause even harder to diagnose problems.  Such as:  every translation unit* getting one-or-more copies of an inlined GetInstanceList.  Doh!

The solution to that latter problem is rather heinous (in my opinion), which involves explicit instantiation for all known instances of Enum<T> in a designated translation unit.  Ugh.  Maybe there is a more elegant solution.

HTH,
--Eljay

* Or compilation unit - whatever your terminological preference.



More information about the Gcc-help mailing list