Note that there are some explanatory texts on larger screens.

plurals
  1. POC++0x Error: overloading a function with std::shared_ptr to const argument is ambiguous
    text
    copied!<p>Suppose I have two <strong>unrelated</strong> classes <code>A</code> and <code>B</code>. I also have a class <code>Bla</code> that uses <code>boost::shared_ptr</code> like this:</p> <pre><code>class Bla { public: void foo(boost::shared_ptr&lt;const A&gt;); void foo(boost::shared_ptr&lt;const B&gt;); } </code></pre> <p>Notice the <strong>const</strong>. That's the important part which the original version of this question lacked. This compiles, and the following code works:</p> <pre><code>Bla bla; boost::shared_ptr&lt;A&gt; a; bla.foo(a); </code></pre> <p>However, if I switch from using <code>boost::shared_ptr</code> to using <code>std::shared_ptr</code> in the above examples, I get a compilation error that says:</p> <pre><code>"error: call of overloaded 'foo(std::shared_ptr&lt;A&gt;)' is ambiguous note: candidates are: void foo(std::shared_ptr&lt;const A&gt;) void foo(std::shared_ptr&lt;const B&gt;) </code></pre> <p>Can you help me figure out why the compiler can't figure out which function to use in the std::shared_ptr case, and can in the boost::shared_ptr case? I'm using the default GCC and Boost versions from the Ubuntu 11.04 package repository which are currently GCC 4.5.2 and Boost 1.42.0.</p> <p>Here is the full code that you can try compiling:</p> <pre><code>#include &lt;boost/shared_ptr.hpp&gt; using boost::shared_ptr; // #include &lt;memory&gt; // using std::shared_ptr; class A {}; class B {}; class Bla { public: void foo(shared_ptr&lt;const A&gt;) {} void foo(shared_ptr&lt;const B&gt;) {} }; int main() { Bla bla; shared_ptr&lt;A&gt; a; bla.foo(a); return 0; } </code></pre> <p>By the way, this issue motivated me to ask <a href="https://stackoverflow.com/questions/6322245/should-i-switch-from-using-boostshared-ptr-to-stdshared-ptr">this question</a> about whether I should be using <code>std::shared_ptr</code> at all yet ;-)</p>
 

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