title: Compile time generated constant type ID
body: <p>I'm writing an event system as a part of a hobby project, a 2D game engine. As part of the event system design, I need to map objects in base of what templated derived class they represent. To better illustrate the problem, consider the following simplified code:</p>
  
  <pre><code>class Base
  {
  public:
      virtual ~Base(){};
      int getTypeId() {return typeId_;}
      static bool compareIfSameType(Base *a, Base *b)
          {return a-&gt;getTypeId() == b-&gt;getTypeId();}
  protected:
      int typeId_;
  };
  
  template&lt;typename T&gt;
  class Derived : public Base
  {
  public:
      Derived(int typeId) {typeId_ = typeId;}
  };
  
  int main()
  {
      Derived&lt;int&gt; obj1(1);
      Derived&lt;float&gt; obj2(2);
      Derived&lt;float&gt; obj3(2);
  
      if(Base::compareIfSameType(&amp;obj1, &amp;obj2))
           cout &lt;&lt; "obj1 and obj2 are of equal type\n";
      else cout &lt;&lt; "obj1 and obj2 are not of equal type\n";
      if(Base::compareIfSameType(&amp;obj2, &amp;obj3))
           cout &lt;&lt; "obj2 and obj3 are of equal type\n";
      else cout &lt;&lt; "obj2 and obj3 are not of equal type\n";
  }
  /*output:
  obj1 and obj2 are not of equal type
  obj2 and obj3 are of equal type*/
  </code></pre>
  
  <p>There is no actual problem with this code, but the requirement of manually passing a number identifying the type of each derived class instance is very cumbersome and quite error prone.
  What I want is to automatically generate the typeId from the type of T at compile time:</p>
  
  <pre><code>Derived&lt;int&gt; obj1;
  Derived&lt;float&gt; obj2;
  Derived&lt;float&gt; obj3;
  
  if(Base::compareIfSameType(&amp;obj1, &amp;obj2))
      //do something...
  </code></pre>