Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I cannot imagine any possible way how that typeid could be used to check that type is polymorphic. It cannot even be used to assert that it is, since typeid will work on any type. Boost has an implementation <a href="http://www.boost.org/doc/libs/1_39_0/boost/type_traits/is_polymorphic.hpp" rel="noreferrer">here</a>. As for why it might be necessary -- one case I know is the Boost.Serialization library. If you are saving non-polymorphic type, then you can just save it. If saving polymorphic one, you have to gets its dynamic type using typeid, and then invoke serialization method for that type (looking it up in some table).</p> <p><strong>Update</strong>: it appears I am actually wrong. Consider this variant:</p> <pre><code>template &lt;class T&gt; bool isPolymorphic() { bool answer=false; T *t = new T(); typeid(answer=true,*t); delete t; return answer; } </code></pre> <p>This actually does work as name suggests, exactly per comment in your original code snippet. The expression inside typeid is not evaluated if it "does not designate an lvalue of polymorphic class type" (std 3.2/2). So, in the case above, if T is not polymorphic, the typeid expression is not evaluated. If T is polymorphic, then *t is indeed lvalue of polymorphic type, so entire expression has to be evaluated.</p> <p>Now, your original example is still wrong :-). It used <code>T()</code>, not <code>*t</code>. And <code>T()</code> create <strong>rvalue</strong> (std 3.10/6). So, it still yields an expression that is not "lvalue of polymorphic class".</p> <p>That's fairly interesting trick. On the other hand, its practical value is somewhat limited -- because while boost::is_polymorphic gives you a compile-time constant, this one gives you a run-time value, so you cannot instantiate different code for polymorphic and non-polymorphic types.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload