title: How to use static variable in a C++ class template
body: <p>This is an example taken from geeksforgeeks. I don't understand the following code.</p>
  
  <pre><code>template&lt;class T&gt; int Test&lt;T&gt;::count = 0;
  </code></pre>
  
  <p>Is count an external variable? Why not just let static int count = 0?
  The description and code in the geeksforgeeks is listed below.</p>
  
  <blockquote>
    <p>Class templates and static variables: The rule for class templates is
    same as function templates Each instantiation of class template has
    its own copy of member static variables. For example, in the following
    program there are two instances Test and Test. So two copies of static
    variable count exist.</p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  template &lt;class T&gt; class Test
  {  
  private:
    T val; 
  public:
    static int count;
    Test()
    {
      count++;
    }
    // some other stuff in class
  };
  
  template&lt;class T&gt;
  int Test&lt;T&gt;::count = 0;
  
  int main()
  {
    Test&lt;int&gt; a;  // value of count for Test&lt;int&gt; is 1 now
    Test&lt;int&gt; b;  // value of count for Test&lt;int&gt; is 2 now
    Test&lt;double&gt; c;  // value of count for Test&lt;double&gt; is 1 now
    cout &lt;&lt; Test&lt;int&gt;::count   &lt;&lt; endl;  // prints 2  
    cout &lt;&lt; Test&lt;double&gt;::count &lt;&lt; endl; //prints 1
  
    getchar();
    return 0;
  }
  </code></pre>