title: Polymorphism and shared_ptr passed by reference
body: <p>I try to send to function a shared_ptr with polymorphic class.
  My objective is to find a best way to send my shared_ptr 
  without increase ref_count.</p>
  
  <p><strong>EDIT:</strong> I don't search solution where my shared_ptr is replaced because I want to call shared_ptr.reset() for example.</p>
  
  <p>Currently, <code>void doGenericTemplate(std::shared_ptr&lt;CLASS&gt;&amp; ptr)</code> is what I want in result BUT I prefer a single function in program.</p>
  
  <ol>
  <li>Do you have another solution ?</li>
  </ol>
  
  <p>Moreover, I don't understand why the function <code>void doGeneric(std::shared_ptr&lt;Base&gt;&amp; ptr)</code> doesn't compile (equivalent without shared_ptr work fine: please check <code>doClassic</code> in complete code).</p>
  
  <ol start="2">
  <li>Do you have an explain ?</li>
  </ol>
  
  <p>Thanks you !</p>
  
  <h3>Partial code</h3>
  
  
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  class Base
  {
      public:
          Base() = default;
          virtual ~Base() = default;
          virtual void run() = 0;
  };
  
  class Derived1: public Base
  {
      public:
          Derived1() = default;
          virtual ~Derived1() = default;
          void run()
          {
              std::cout &lt;&lt; "  Derived1";
          }
  };
  
  class Derived2: public Base
  {
      public:
          Derived2() = default;
          virtual ~Derived2() = default;
          void run()
          {
              std::cout &lt;&lt; "  Derived2";
          }
  };
  
  // This function works but increase count
  void doGenericCopy(std::shared_ptr&lt;Base&gt; ptr)
  {
      ptr-&gt;run();
      std::cout &lt;&lt; "  Ref count: " &lt;&lt; ptr.use_count() &lt;&lt; std::endl;
  }
  
  // This function works without increase count = OK !
  void doSpecificD1(std::shared_ptr&lt;Derived1&gt;&amp; ptr)
  {
      ptr-&gt;run();
      std::cout &lt;&lt; "  Ref count: " &lt;&lt; ptr.use_count() &lt;&lt; std::endl;
  }
  
  // Compilation error = FAILED !
  void doGeneric(std::shared_ptr&lt;Base&gt;&amp; ptr)
  {
      ptr-&gt;run();
      std::cout &lt;&lt; "  Ref count: " &lt;&lt; ptr.use_count() &lt;&lt; std::endl;
  }
  
  // Working fine for all Derivate = OK !
  template&lt;typename CLASS&gt;
  void doGenericTemplate(std::shared_ptr&lt;CLASS&gt;&amp; ptr)
  {
      ptr-&gt;run();
      std::cout &lt;&lt; "  Ref count: " &lt;&lt; ptr.use_count() &lt;&lt; std::endl;
  }
  
  int main()
  {
      auto d1 = std::make_shared&lt;Derived1&gt;();
      auto d2 = std::make_shared&lt;Derived2&gt;();
  
      std::cout &lt;&lt; "With copy: " &lt;&lt; std::endl;
      doGenericCopy(d1);
      doGenericCopy(d2);
  
      std::cout &lt;&lt; "Specific: " &lt;&lt; std::endl;
      doSpecificD1(d1);
  
      std::cout &lt;&lt; "Template: " &lt;&lt; std::endl;
      doGenericTemplate(d1);
      doGenericTemplate(d2);
  
      // Compilation issue
      //doGeneric(d1);
  }
  </code></pre>
  
  <h3>Complete code</h3>
  
  <p><a href="https://ideone.com/ZL0v7z" rel="nofollow noreferrer">https://ideone.com/ZL0v7z</a></p>
  
  <h3>Conclusion</h3>
  
  <p>Currently in c++, shared_ptr has not in language a specific tools to use polymorphism of class inside template.</p>
  
  <p>The best way is to refactor my code and avoids to manage shared_ptr (ref_count, reset).</p>
  
  <p>Thanks guys !</p>