Pass-by-value vs. pass-by-reference-to-const in C++
I disagree with this article.  There is a case where you might want to pass a const reference to a shared pointer.<p>Suppose you have a class<p><pre><code>  class Foo {
    shared_ptr&lt;Widget&gt; widget;
    bool has_widget();
    void set_widget(shared_ptr&lt;widget&gt; widget);
  }
</code></pre>
and you are writing a function which may call set_widget(), it can save an unnecessary shared_ptr copy if you pass it as a const reference:<p><pre><code>  void maybe_set_widget( Foo&amp; foo, const shared_ptr&lt;Widget&gt;&amp; widget) {
    if(!foo.has_widget()) {
       foo.set_widget(widget); 
    }
  }

</code></pre>
Doing a quick google search finds that Herb Sutter agrees with me (<a href="https:&#x2F;&#x2F;herbsutter.com&#x2F;2013&#x2F;06&#x2F;05&#x2F;gotw-91-solution-smart-pointer-parameters&#x2F;" rel="nofollow">https:&#x2F;&#x2F;herbsutter.com&#x2F;2013&#x2F;06&#x2F;05&#x2F;gotw-91-solution-smart-poi...</a>):<p>&gt;Use a const shared_ptr&amp; as a parameter only if you’re not sure whether or not you’ll take a copy and share ownership