title: Template static variable
body: <p>I can't understand, why if we define static variable of usual (non-template) class in header, we have linker error, but in case of templates all works fine and moreover we will have single instance of static variable among all translation units:</p>
  
  <p>It's template header (template.h):</p>
  
  <pre><code>// template.h
  template&lt;typename T&gt;
  class Templ {
  public:
    static int templStatic;
  };
  
  template&lt;typename T&gt; Templ&lt;T&gt;::templStatic = 0;
  </code></pre>
  
  <p>It's first unit using template (unit1.cpp)</p>
  
  <pre><code>// unit1.cpp
  #include "template.h"
  
  int method1() {
    return Templ&lt;void&gt;::templStatic++;
  }
  </code></pre>
  
  <p>Second unit here (unit2.cpp):</p>
  
  <pre><code>// unit2.cpp
  #include "template.h"
  int method2() {
    return Templ&lt;void&gt;::templStatic++;
  }
  </code></pre>
  
  <p>And, finally, main.cpp:</p>
  
  <pre><code>// main.cpp
  #include &lt;iostream&gt;
  int method1();
  int method2();
  
  int main(int argc, char** argv) {
    std::cout &lt;&lt; method1() &lt;&lt; std::endl;
    std::cout &lt;&lt; method2() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>After compilling, linking and executing this code, we will have following output:</p>
  
  <pre><code>0
  1
  </code></pre>
  
  <p>So, why in case of templates all works fine (and as expected) ? How compiler or linker handle this (we can compile each .cpp file in separated calling of compiler, and then link them with caling to linker, so compiler and linker don't "see" all .cpp files at same time) ?</p>
  
  <p>PS: My compiler: msvcpp 9 (but checked on mingw too)</p>