title: How to implement a class counter in DLL?
body: <p>So far I have:</p>
  
  <pre><code>// TypeCounter.h
  
  template &lt;typename Base&gt;
  class Counter : public Base
  {
  protected:
      static int typeIndexCounter;
  };
  
  template &lt;typename T, typename Base&gt;
  class Bridge : public Counter&lt;Base&gt;
  {
  public:
      virtual ~Bridge() {}
  
      virtual int GetTypeIndex() const
      {
          return TypeIndex();
      }
  
      static int TypeIndex()
      {
          static int typeIndex = typeIndexCounter++;
          return typeIndex;
      }
  };
  
  // static variable definition
  template &lt;typename Base&gt;
  int Counter&lt;Base&gt;::typeIndexCounter;
  </code></pre>
  
  <p>Use case is like:</p>
  
  <pre><code>class SBase
  { ... }
  class S0 : public Bridge&lt;S0, SBase&gt;
  { ... }
  class S1 : public Bridge&lt;S1, SBase&gt;
  { ... }
  class S2 : public Bridge&lt;S2, SBase&gt;
  { ... }
  </code></pre>
  
  <p>So for each kind of <code>Base</code> class, I can count how many derives it has (if it has ever been instantiated, of course).</p>
  
  <p>The problem is that, this <code>Counter</code> class is in a DLL.</p>
  
  <p>If I have multiple users of this DLL, then each user would instantiate its own <code>Bridge</code> and <code>Counter</code>, then the <code>TypeIndex()</code> can be different among these users for the same <code>T</code>.</p>
  
  <p>I tried to put <code>__declspec(dllexport)</code> before <code>Counter</code> and <code>Bridge</code>, but there are compile errors on the user side (the user side would change <code>export</code> to <code>import</code>, by defining a macro).<br>
  Despite the errors, it is also a wrong approach, since one should not export class template.</p>
  
  <p><strong>So I want to make sure that the <code>TypeIndex()</code> keep the same for certain <code>T</code>, even if there are many instantiations for the same <code>T</code>, how can I achieve this?</strong></p>
  
  <p>Thanks!</p>