title: compiler error: cannot convert from object_type<T> to object_type<T> in c++
body: <p>I have a method in a shared pointer class I am writing.</p>
  
  <pre><code>template&lt;class T&gt;
  template&lt;class Y&gt;
  shared_ptr&lt;T&gt; &amp;shared_ptr&lt;T&gt;::operator=(/*const*/ shared_ptr&lt;Y&gt; &amp;r)
  {
    shared_ptr&lt;T&gt;(r).swap(*this);
    return *this;
  }
  </code></pre>
  
  <p>Which when used in this manner</p>
  
  <pre><code>class Foo
  {
  };
  
  class Bar : public Foo
  {
  };
  
  int main(int /*argc*/, char * /*argv*/[]) {
    shared_ptr&lt;Foo&gt; giPtr(new Foo(1));
    shared_ptr&lt;Bar&gt; giPtr2;
    giPtr2 = glext::dynamic_pointer_cast&lt;Foo&gt;(giPtr);
  } 
  </code></pre>
  
  <p>Generates the following error in MSVC:</p>
  
  <pre><code>1&gt;c:\users\mehoggan\documents\github\x86-applications\glextensions\smart_ptrs\shared_ptr\shared_ptr.inl(53): error C2440: '&lt;function-style-cast&gt;' : cannot convert from 'glext::shared_ptr&lt;T&gt;' to 'glext::shared_ptr&lt;T&gt;'
  1&gt;          with
  1&gt;          [
  1&gt;              T=Foo
  1&gt;          ]
  1&gt;          and
  1&gt;          [
  1&gt;              T=Bar
  1&gt;          ]
  1&gt;          No constructor could take the source type, or constructor overload resolution was ambiguous
  1&gt;          main.cpp(28) : see reference to function template instantiation 'glext::shared_ptr&lt;T&gt; &amp;glext::shared_ptr&lt;T&gt;::operator =&lt;Foo&gt;(glext::shared_ptr&lt;Foo&gt; &amp;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              T=Bar
  1&gt;          ]
  </code></pre>
  
  <hr>
  
  <p><em><strong>Constructors</em></strong></p>
  
  <pre><code>template&lt;class T&gt;
    shared_ptr&lt;T&gt;::shared_ptr() : 
      _px(0), 
      _pn()
    {}
  
    template&lt;class T&gt;
    template&lt;class Y&gt;
    shared_ptr&lt;T&gt;::shared_ptr(Y * p) : 
      _px(p), 
      _pn()
    {
      internal::shared_pointer_construct(this, p, _pn);
    }
  
    // TODO: Create shared ptr from weak pointer
  
    template&lt;class T&gt;
    shared_ptr&lt;T&gt;::shared_ptr(const shared_ptr &amp;r) :
      _px(r._px),
      _pn(r._pn)
    {}
  
    template&lt;class T&gt;
    template&lt;class Y&gt;
    shared_ptr&lt;T&gt;::shared_ptr(const shared_ptr&lt;Y&gt; &amp;r, element_type *p) :
      _px(p), 
      _pn(r._pn)
    {}
  
    template&lt;class T&gt;
    shared_ptr&lt;T&gt; &amp;shared_ptr&lt;T&gt;::operator=(const shared_ptr&lt;T&gt; &amp;r)
    {
      shared_ptr&lt;T&gt;(r).swap(*this);
      return *this;
    }
  </code></pre>
  
  <p><em><strong>Swap</em></strong></p>
  
  <pre><code>template&lt;class T&gt;
  void shared_ptr&lt;T&gt;::swap(shared_ptr&lt;T&gt; &amp;other)
  {
    std::swap(_px, other._px);
    _pn.swap(other._pn);
  }
  </code></pre>