title: static template variable initialized by the constructor of a global variable is empty when main() was reached
body: <p>Please have a look at the following example code</p>
  
  <pre><code>template &lt; typename TYPE &gt;
  struct foo
  {
      static TYPE bar;
  };
  
  template &lt; typename TYPE &gt;
  TYPE foo &lt; TYPE &gt;::bar;
  
  template &lt; typename RET, typename... ARGS &gt;
  struct changer
  {
      typedef std::function &lt; RET ( ARGS... ) &gt; type;
  
      static void set ( type v ) { foo &lt; type &gt;::bar = v; }
  
      static type get () { return foo &lt; type &gt;::bar; }
  };
  
  void custom_func ( int i )
  {
      std::cout &lt;&lt; "called with " &lt;&lt; i &lt;&lt; std::endl;
  }
  
  struct initializer
  {
      typedef changer &lt; void, int &gt; changer_t;
  
      initializer ()
      {
          changer_t::set ( std::bind ( &amp; custom_func, std::placeholders::_1 ) );
          call ( 1 );
      }
  
      void call ( int v )
      {
          auto myfunc = changer_t::get ();
  
          if ( myfunc ) myfunc ( v );
          else std::cout &lt;&lt; "myfunc is empty" &lt;&lt; std::endl;
      }
  };
  
  initializer x;
  
  int main()
  {
      x.call ( 2 );
  
      return 0;
  }
  </code></pre>
  
  <p>The output of that program is</p>
  
  <pre><code>called with 1
  myfunc is empty
  </code></pre>
  
  <p>And when I change <code>foo</code> and <code>changer</code> to not-template code</p>
  
  <pre><code>struct foo
  {
      static std::function &lt; void ( int ) &gt; bar;
  };
  
  std::function &lt; void ( int ) &gt; foo::bar;
  
  struct changer
  {
      typedef std::function &lt; void ( int ) &gt; type;
  
      static void set ( type v ) { foo::bar = v; }
  
      static type get () { return foo::bar; }
  };
  </code></pre>
  
  <p>The output is what I would've also expected for the first version:</p>
  
  <pre><code>called with 1
  called with 2
  </code></pre>
  
  <p>What is happening here ? And how can I make it work with the templated version ?</p>
  
  <p>Tested with MSVC2013 and g++ <a href="http://coliru.stacked-crooked.com/a/b2a56df35098889f" rel="nofollow">templated</a> | <a href="http://coliru.stacked-crooked.com/a/f491169788abb044" rel="nofollow">not-templated</a></p>
  
  <p><strong>Update</strong>: I just accidently built in release mode in VS, in which the templated version outputs </p>
  
  <pre><code>called with 1
  called with 2
  </code></pre>
  
  <p>... Am I tickling UB ?</p>