title: Comma operator with typeid
body: <p>I was studying <em><a href="http://www.justsoftwaresolutions.co.uk/articles/genericptr.pdf" rel="noreferrer">A Generic Non-intrusive Smart Pointer Implementation</a></em>. I have some confusion in <em>section 4</em>. One statement is</p>
  
  <blockquote>
    <p>the expression supplied as the argument to the typeid operator is only evaluated if the result type is an lvalue of polymorphic class type.</p>
  </blockquote>
  
  <p>And associated example code is:</p>
  
  <pre><code>template&lt;typename T&gt;
    void* startOfObject(T* p) {
    void* q=static_cast&lt;void*&gt;(p);
    typeid(q=dynamic_cast&lt;void*&gt;(p),*p); // This line
    return q;
  }
  </code></pre>
  
  <p>AFAIU, it means <code>q=dynamic_cast&lt;void*&gt;(p)</code> will be evaluated if the <em>result type</em> is an <em>lvalue of polymorphic class type</em>. The <em>result</em> means the result of evaluating <code>dynamic_cast&lt;void*&gt;(p)</code> (I guess), so the <code>dynamic_cast</code> has to be applied in any case. The articles states (as I understand) that if <code>p</code> is not <em>polymorphic</em> then <code>dynamic_cast</code> will not be applied, but why? Before applying it, how can it be known whether the <em>result</em> is polymorphic or not? It will be helpful if someone describes in details how the full statement will be executed.</p>
  
  <p>Another statement is</p>
  
  <blockquote>
    <p>There is also a problem if p is NULL – the typeid will throw a std::bad cast.</p>
  </blockquote>
  
  <p>The problem I see is with de-referencing if <code>p</code> is <code>NULL</code>, not with <code>typeid</code> (although it may throw bad_typeid, but that is not because of casting). <code>dynamic_cast</code> will return a <code>NULL</code> pointer of type <code>void*</code> if <code>p</code> is <code>NULL</code>, and <code>typeid</code> should be able to deduce the type information. Is that a typo, or am I missing something?</p>