title: Bizarre static_cast trick?
body: <p>While perusing the Qt source code I came across this gem:</p>
  
  <pre><code>template &lt;class T&gt; inline T qgraphicsitem_cast(const QGraphicsItem *item)
  {
      return int(static_cast&lt;T&gt;(0)-&gt;Type) == int(QGraphicsItem::Type)
          || (item &amp;&amp; int(static_cast&lt;T&gt;(0)-&gt;Type) == item-&gt;type()) ? static_cast&lt;T&gt;(item) : 0;
  }
  </code></pre>
  
  <p>Notice the <code>static_cast&lt;T&gt;(0)-&gt;Type</code>?  I've been using C++ for many years but have never seen 0 being used in a static_cast before.  What is this code doing and is it safe?</p>
  
  <p>Background: If you derive from <code>QGraphicsItem</code> you are meant to declare an unique enum value called <code>Type</code> that and implement a virtual function called <code>type</code> that returns it, e.g.:</p>
  
  <pre><code>class Item : public QGraphicsItem
  {
  public:
    enum { Type = MAGIC_NUMBER };
    int type() const { return Type; }
    ...
  };
  </code></pre>
  
  <p>You can then do this:</p>
  
  <pre><code>QGraphicsItem* item = new Item;
  ...
  Item* derivedItem = qgraphicsitem_cast&lt;Item*&gt;(item);
  </code></pre>
  
  <p>This will probably help explain what that static_cast is trying to do.</p>