title: Casting shared_ptr in initializer list
body: <p>Is it possible to up-cast a shared pointer object reference in an initializer list?</p>
  
  <p>I have two class hierarchies with matching derived classes.  Let's say I have a <code>Base</code> class which is derived to a <code>Foo</code> and a <code>Bar</code> class.  Then I have a separate hierarchy with a <code>ProxyBase</code> class and then (to match the other hierarchy) there's <code>FooProxy</code> and <code>BarProxy</code>.</p>
  
  <p>The <code>ProxyBase</code> class stores a <code>shared_ptr&lt;Base&gt;</code>.  When a <code>ProxyFoo</code> is created it requires a <code>std::shared_ptr&lt;Foo&gt;&amp;</code>, when a <code>ProxyBar</code> is created it requires a <code>std::shared_ptr&lt;Bar&gt;&amp;</code>.</p>
  
  <p>There are two ways I can make this work:</p>
  
  <ul>
  <li>Don't use references to the <code>std::shared_ptr&lt;*&gt;</code> objects.</li>
  <li>Use <code>std::shared_ptr&lt;Base&gt;&amp; obj</code> as input to the <code>ProxyFoo</code> and <code>ProxyBar</code> constructors.  This requires a down-cast (which I want to avoid) in the constructor (since I want the <code>Foo</code> and <code>Bar</code> specific properties), plus that it makes the contract/API more allowing than it should be.</li>
  </ul>
  
  <p>But I'm wondering if this can be done with references.</p>
  
  <p>I <em>think</em> I know what the problem is:  When I remove the references it works, and I think that's because "casting" a <code>shared_ptr&lt;X&gt;</code> is technically not allowed; it can only be "casted" by creating a new <code>shared_ptr</code> object of a derived/base type.  When using references one tries to actually cast one <code>shared_ptr</code> object to another which isn't allowed.  Is this correct?</p>
  
  <p>Originally I thought this was one of those cases where <code>std::static_pointer_cast</code> would help, but if my hypothesis is correct then that won't help either.</p>
  
  <p>Self-contained example:</p>
  
  <pre><code>#include &lt;memory&gt;
  
  class Base { };
  
  class Foo : public Base
  {
  private:
    int val_;
  
  public:
    Foo(int val) : val_(val) { }
  };
  
  class ProxyBase
  {
  protected:
    std::shared_ptr&lt;Base&gt; obj_;
  
    ProxyBase(std::shared_ptr&lt;Base&gt;&amp; obj) : obj_(obj) { }
  };
  
  class FooProxy : public ProxyBase
  {
  public:
    FooProxy(std::shared_ptr&lt;Foo&gt;&amp; fobj) : ProxyBase(fobj) { }
  };
  
  int main()
  {
    auto f = std::make_shared&lt;Foo&gt;(42);
    auto fp = std::make_shared&lt;FooProxy&gt;(f);
  }
  </code></pre>
  
  <p>So my question boils down to:  Is it possible to do what I'm trying to do in the (currently non-functional) code above without removing the references and yet keep the <code>Foo</code> and <code>Bar</code> specific constructors to <code>FooProxy</code> and <code>BarProxy</code>?</p>