title: Static variable inside template function
body: <p>In C++, if you define this function in header.hpp</p>
  
  <pre><code>void incAndShow()
  {
    static int myStaticVar = 0;
    std::cout &lt;&lt; ++myStaticVar &lt;&lt; " " &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>and you include header.hpp in at least two .cpp files. Then you will have <code>multiple definition of incAndShow()</code>. Which is expected. However, if you add a template to the function</p>
  
  <pre><code>template &lt;class T&gt;
  void incAndShow()
  {
    static int myStaticVar = 0;
    std::cout &lt;&lt; ++myStaticVar &lt;&lt; " " &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>then you won't have any <code>multiple definition of</code> error. Likewise, two different .cpp calling the function with the same template (e.g. <code>incAndShow&lt;int&gt;()</code>), will share <code>myStaticVar</code>. Is this normal? I'm asking this question, because I do rely on this "feature" (sharing the static variable) and I want to be sure that it is not only my implementation that is doing this.</p>