Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While the compiler can perform derived-to-base conversions when doing type deduction, <code>std::shared_ptr&lt;Derived&gt;</code> does <em>not</em> itself derive from <code>std::shared_ptr&lt;Base&lt;int&gt;&gt;</code>.</p> <p>There is a user-defined conversion between the two that allows <code>shared_ptr</code> to behave like regular pointers with respect to polymorphism, but the compiler won't take into consideration user-defined conversions when performing type deduction.</p> <p>Without considering user-defined conversiosn, the compiler can't deduce a <code>T</code> that would make <code>shared_ptr&lt;Base&lt;T&gt;&gt;</code> either identical to <code>shared_ptr&lt;Derived&gt;</code> or a base class of <code>shared_ptr&lt;Derived&gt;</code> (once again, <code>shared_ptr&lt;Base&lt;int&gt;&gt;</code> is <em>not</em> a base class of <code>shared_ptr&lt;Derived&gt;</code>).</p> <p>Therefore, type deduction fails.</p> <p>To work around this problem, you could let the parameter of your function be a simple <code>shared_ptr&lt;T&gt;</code> <em>and</em> add a SFINAE-constraint that would make sure your overload is picked only when the type of the argument is derived from (or is) an instance of the <code>Base</code> class template:</p> <pre><code>#include &lt;type_traits&gt; namespace detail { template&lt;typename T&gt; void deducer(Base&lt;T&gt;); bool deducer(...); } template &lt;typename T, typename std::enable_if&lt; std::is_same&lt; decltype(detail::deducer(std::declval&lt;T&gt;())), void &gt;::value&gt;::type* = nullptr&gt; void func(std::shared_ptr&lt;T&gt; ptr) { // ... } </code></pre> <p>Here is a <a href="http://coliru.stacked-crooked.com/view?id=f4988955d3b682b6e1185a37194b20e2-e54ee7a04e4b807da0930236d4cc94dc"><strong>live example</strong></a>.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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