C++ template: The static member in a global object is not initialized : <p>I have a piece of simple C++ code, in which I defined a template and a global object by specializing the template. The object constructor accesses a static member in the specialized template. But it turns out the static member is not initialized at that point. But for a local object (defined in the body of a function), it works. I'm confused...</p>

<p>My c++ compiler is: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609</p>

<pre><code>/////////////////////////
template&lt;typename T&gt;
class TB{
public:
  const char *_name;
  TB(const char * str):_name(str){
    cout &lt;&lt; "constructor is called:" &lt;&lt; _name &lt;&lt; endl;
  };

  virtual ~TB(){
    cout &lt;&lt; "destructor is called:" &lt;&lt; _name &lt;&lt; endl;
  };
};

template&lt;typename T&gt;
class TA{
public:
  const char *_name;
  TA(const char * str):_name(str){
    cout &lt;&lt; "constructor is called:" &lt;&lt; _name &lt;&lt; endl;
    cout &lt;&lt; tb._name &lt;&lt;endl;
  };

  virtual ~TA(){
    cout &lt;&lt; "destructor is called:" &lt;&lt; _name &lt;&lt; endl;
  };

  static TB&lt;T&gt; tb;
};

template&lt;typename T&gt;
  TB&lt;T&gt; TA&lt;T&gt;::tb("static-tb");
TA&lt;int&gt; ta("global-ta");

int main(int argc,char ** argv){
  cout &lt;&lt; "program started." &lt;&lt; endl;
  cout &lt;&lt; "program stopped." &lt;&lt; endl;
  return 0;
}

/////////////////////////
//  OUTPUT:
constructor is called:global-ta
// yes, only such a single line.
</code></pre>

<p>If I put the definition of ta in main() like the following, it works.</p>

<pre><code>int main(int argc,char ** argv){
  cout &lt;&lt; "program started." &lt;&lt; endl;
  TA&lt;int&gt; ta("local-ta");
  cout &lt;&lt; "program stopped." &lt;&lt; endl;
  return 0;
}

/////////////////////
//  OUTPUT:
constructor is called:static-tb
program started.
constructor is called:local-ta
static-tb
program stopped.
destructor is called:local-ta
destructor is called:static-tb
// end of output
</code></pre>
