title: Test whether a class is polymorphic
body: <p>We have a sub-project 'commonUtils' that has many generic code-snippets used across the parent project.
  One such interesting stuff i saw was :-</p>
  
  <pre><code>/*********************************************************************
  If T is polymorphic, the compiler is required to evaluate the typeid 
  stuff at runtime, and answer will be true.  If T is non-polymorphic, 
  the compiler is required to evaluate the typeid stuff at compile time, 
  whence answer will remain false
  *********************************************************************/
  template &lt;class T&gt; 
  bool isPolymorphic() { 
     bool answer=false; 
     typeid(answer=true,T()); 
     return answer; 
  }
  </code></pre>
  
  <p>I believed the comment and thought that it is quite an interesting template though it is not
  used across the project. I tried using it like this just for curiosity ...</p>
  
  <pre><code>class PolyBase {
  public:
     virtual ~PolyBase(){}
  };
  
  class NPolyBase {
  public:
     ~NPolyBase(){}
  };
  
  
  if (isPolymorphic&lt;PolyBase&gt;())
    std::cout&lt;&lt;"PolyBase = Polymorphic\n";
  if (isPolymorphic&lt;NPolyBase&gt;())
    std::cout&lt;&lt;"NPolyBase = Also Polymorphic\n";
  </code></pre>
  
  <p>But none of those ever returns true. MSVC 2005 gives no warnings but Comeau warns typeid expression has no effect. Section 5.2.8 in the C++ standard does not say anything like what the comment says i.e. typeid is is evaluated at compile time for non-polymorphic types and at runtime for polymorphic types.</p>
  
  <p>1) So i guess the comment is misleading/plain-wrong or since the author of this code is quite a senior C++ programmer, am i missing something?</p>
  
  <p>2) OTOH, I am wondering if we can test whether a class is polymorphic(has at least one virtual function) using some technique?</p>
  
  <p>3) When would one want to know if a class is polymorphic? Wild guess; to get the start-address of a class by using <code>dynamic_cast&lt;void*&gt;(T)</code> (as <code>dynamic_cast</code> works only on polymorphic classes).</p>
  
  <p>Awaiting your opinions.</p>
  
  <p>Thanks in advance,</p>