Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with std::shared_ptr, inheritance, and template argument deduction
    primarykey
    data
    text
    <p>I'm trying to use template argument deduction with inheritance and <code>std::shared_ptr</code>. As you can see in the sample code below, I'm passing a <code>shared_ptr&lt;Derived&gt;</code> to a templated non-member function, which should do template argument deduction. If I manually name the type everything works, and if I let it do template argument deduction it doesn't. It would <em>seem</em> as though the compiler couldn't figure out the type, however the error message shows that it did. I'm not sure what's going on here, and I would appreciate any input. (Visual Studio 2010)</p> <pre><code>#include &lt;memory&gt; template &lt;typename T&gt; class Base {}; class Derived : public Base&lt;int&gt; {}; template &lt;typename T&gt; void func(std::shared_ptr&lt;Base&lt;T&gt; &gt; ptr) {}; int main(int argc, char* argv[]) { std::shared_ptr&lt;Base&lt;int&gt;&gt; myfoo(std::shared_ptr&lt;Derived&gt;(new Derived)); // Compiles func(myfoo); // Compiles func&lt;int&gt;(std::shared_ptr&lt;Derived&gt;(new Derived)); // Compiles func(std::shared_ptr&lt;Derived&gt;(new Derived)); // Doesn't compile. The error message suggests it did deduce the template argument. return 0; } </code></pre> <p>The error message:</p> <pre><code>5&gt; error C2664: 'func' : cannot convert parameter 1 from 'std::tr1::shared_ptr&lt;_Ty&gt;' to 'std::tr1::shared_ptr&lt;_Ty&gt;' 5&gt; with 5&gt; [ 5&gt; _Ty=Derived 5&gt; ] 5&gt; and 5&gt; [ 5&gt; _Ty=Base&lt;int&gt; 5&gt; ] 5&gt; Binding to reference 5&gt; followed by 5&gt; Call to constructor 'std::tr1::shared_ptr&lt;_Ty&gt;::shared_ptr&lt;Derived&gt;(std::tr1::shared_ptr&lt;Derived&gt; &amp;&amp;,void **)' 5&gt; with 5&gt; [ 5&gt; _Ty=Base&lt;int&gt; 5&gt; ] 5&gt; c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(1532) : see declaration of 'std::tr1::shared_ptr&lt;_Ty&gt;::shared_ptr' 5&gt; with 5&gt; [ 5&gt; _Ty=Base&lt;int&gt; 5&gt; ] 5&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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