title: C++: type_info to distinguish types
body: <p>I know that compilers have much freedom in implementing <code>std::type_info</code> functions' behavior.</p>
  
  <p>I'm thinking about using it to compare object types, so I'd like to be sure that:</p>
  
  <ol>
  <li><p><code>std::type_info::name</code> must return two different strings for two different types.</p></li>
  <li><p><code>std::type_info::before</code> must say that <code>Type1</code> is <em>before</em> <code>Type2</code> <strong>exclusive-or</strong> <code>Type2</code> is <em>before</em> <code>Type1</code>.</p>
  
  <pre><code>// like this:
  typeid(T1).before( typeid(T2) ) != typeid(T2).before( typeid(T1) )
  </code></pre></li>
  <li><p>Two different specialization of the same template class are considered different types.</p></li>
  <li><p>Two different <code>typedef</code>-initions of the same type are the same type.</p></li>
  </ol>
  
  <p>And finally:</p>
  
  <ul>
  <li><p>Since <code>std::type_info</code> is not copyable, how could I store <code>type_info</code>s somewhere (eg: in a <code>std::map</code>)? The only way it to have a <code>std::type_info</code> always allocated somewhere (eg: on the stack or on a static/global variable) and use a pointer to it?</p></li>
  <li><p>How fast are <code>operator==</code>, <code>operator!=</code> and <code>before</code> on most common compilers? I guess they should only compare a value. And how fast is <code>typeid</code>?</p></li>
  <li><p>I've got a class <code>A</code> with a <code>virtual bool operator==( const A&amp; ) const</code>. Since <code>A</code> has got many subclasses (some of which are unknown at compile time), I'd overload that virtual operator in any subclass <code>B</code> this way:</p>
  
  <pre><code>virtual bool operator==( const A &amp;other ) const {
    if( typeid(*this) != typeid(other) ) return false;
    // bool B::operator==( const B &amp;other ) const // is defined for any class B
    return operator==( static_cast&lt;B&amp;&gt;( other ) );
  }
  </code></pre>
  
  <p>Is this an acceptable (and standard) way to implement such operator?</p></li>
  </ul>