title: Static Construction when the object is a template
body: <p>i have a factory class that i've used for quite some time, which i have tried to do a little template magic to, and it simply will not work. I don't think you'll need to see that class, as it's quite large, but if you really think it's related then i will edit it in. Anyway, i have a setup like this which i've done forever.</p>
  
  <pre><code>class Base :
      public Factory&lt;Base&gt;
  {
  };
  
  class Derived :
      public Base
  {
      static Factory&lt;Base&gt;::DerivedRegister&lt;Derived&gt; m_Reg;
  };
  
  Factory&lt;Base&gt;::DerivedRegister&lt;Derived&gt; Derived::m_Reg;
  </code></pre>
  
  <p>the factory gives it the static method createInstance(const std::string&amp; name) which will return an instance of the registered object. The DerivedRegister object derives from the factory object and when declared statically, must be constructed during static initialization. This means i have access to the factories static internals within it's constructor, and so i utilize it's constructor to have all registered object available to the factory during static initialization. This works and has for awhile, but then i tried something like this so that derived classes could avoid needing to explicitly make that static derivedregister object:</p>
  
  <pre><code>class Base :
      public Factory&lt;Base&gt;
  {
  };
  
  template&lt;class TDerived&gt;
  class BaseReg :
      public Base
  {
      static Factory&lt;Base&gt;::DerivedRegister&lt;TDerived&gt; m_Reg;
  };
  
  template&lt;class TDerived&gt;
  Factory&lt;Base&gt;::DerivedRegister&lt;TDerived&gt; BaseReg&lt;TDerived&gt;::m_Reg;
  
  class Derived :
      public BaseReg&lt;Derived&gt;
  {
  };
  </code></pre>
  
  <p>When i run the program and try to create an instance of the Derived class, it doesn't exist in the factory, and after a little doodling, i find that the static DerivedRegister is never being constructed in derived, as in the constructor is never called. I find this odd. How could it not be forced to statically initialize if the Derived class isn't a template? When i make a dummy static object as seen below, and use a method from m_Reg in it's static construction, suddenly the DerivedRegister constructs correctly and the factory can instantiate it, no sweat.</p>
  
  <pre><code>class Base :
      public Factory&lt;Base&gt;
  {
  };
  
  template&lt;class TDerived&gt;
  class BaseReg :
      public Base
  {
  protected:
      static Factory&lt;Base&gt;::DerivedRegister&lt;TDerived&gt; m_Reg;
  };
  
  template&lt;class TDerived&gt;
  Factory&lt;Base&gt;::DerivedRegister&lt;TDerived&gt; BaseReg&lt;TDerived&gt;::m_Reg;
  
  class Derived :
      public BaseReg&lt;Derived&gt;
  {
      class RandomClass
      {
      public:
           RandomClass(std::string meh) {}
      };
  
  private:
      static RandomClass m_Obj;
  };
  
  Derived::RandomClass Derived::m_Obj(m_Reg.func());
  </code></pre>
  
  <p>So what am i not getting about this template class's static member initialization here? doesn't it have to statically initialize the object like any other non-template object?</p>
  
  <p>EDIT: mkay, to give a little insight, i'll post the Factory object. Just beware the WALLS of text. ignore the extra includes, and the superfluous GetNames, and func functions, they are just voodoo code.</p>
  
  <pre><code>#ifndef FACTORY_H
  #define FACTORY_H
  
  // library tools
  #include &lt;map&gt;
  #include &lt;string&gt;
  #include &lt;typeinfo&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;cxxabi.h&gt;
  
  const std::string demangle(const char* name);
  
  template&lt;class base&gt;
  class Factory
  {
  protected:
      template&lt;class T&gt;
      static base * createT() { return new T;}
  
      typedef std::map&lt;std::string, base*(*)()&gt; map_type;
  
      static map_type&amp; GetMap()
      {
          static map_type map;
          return map;
      }
  
  public:
      virtual ~Factory(){}
  
      static base * createInstance(const std::string &amp; s)
      {
          if(!GetMap().count(s))
              return nullptr;
          typename map_type::iterator it = GetMap().find(s);
          return it-&gt;second();
      }
  
      template &lt;class TDerived&gt;
      struct DerivedRegister :
          public Factory&lt;base&gt;
      {
          DerivedRegister()
          {
              std::string name = demangle(typeid(TDerived).name());
              GetMap().insert(std::pair&lt;std::string, base*(*)()&gt;(name, &amp;createT&lt;TDerived&gt;));
          }
  
          DerivedRegister(const std::string&amp; name)
          {
              GetMap().insert(std::pair&lt;std::string, base*(*)()&gt;(name, &amp;createT&lt;TDerived&gt;));
          }
  
          std::string func() {return "meh";}
      };
  
      static void GetNames(std::vector&lt;std::string&gt;&amp; names)
      {
          names.clear();
  
          for(auto it = GetMap().begin(); it != GetMap().end(); ++it)
              names.push_back(it-&gt;first);
  
      }
  };
  
  
  #endif
  </code></pre>