title: Downcasting shared_ptr<Base> to shared_ptr<Derived>?
body: <p><i><b>Update:</b> the shared_ptr in this example is like the one in Boost, but it doesn't support shared_polymorphic_downcast (or dynamic_pointer_cast or static_pointer_cast for that matter)!</i></p>
  
  <p>I'm trying to initialize a shared pointer to a derived class without losing the reference count:</p>
  
  <pre><code>struct Base { };
  struct Derived : public Base { };
  shared_ptr&lt;Base&gt; base(new Base());
  shared_ptr&lt;Derived&gt; derived;
  
  // error: invalid conversion from 'Base* const' to 'Derived*'
  derived = base;  
  </code></pre>
  
  <p>So far, so good.  I didn't expect C++ to implicitly convert Base* to Derived*.  However, I do want the functionality expressed by the code (that is, maintaining the reference count while downcasting the base pointer). My first thought was to provide a cast operator in Base so that an implicit conversion to Derived could take place (for pedants: I would check that the down cast is valid, don't worry):</p>
  
  <pre><code>struct Base {
    operator Derived* ();
  }
  // ...
  Base::operator Derived* () {
    return down_cast&lt;Derived*&gt;(this);
  }
  </code></pre>
  
  <p>Well, it didn't help. It seems the compiler completely ignored my typecast operator. Any ideas how I could make the shared_ptr assignment work? For extra points: what kind of type <code>Base* const</code> is?  <code>const Base*</code> I understand, but <code>Base* const</code>?  What does <code>const</code> refer to in this case?</p>