Suppose I have two unrelated classes A
and B
. I also have a class Bla
that uses boost::shared_ptr
like this:
class Bla {
public:
void foo(boost::shared_ptr<const A>);
void foo(boost::shared_ptr<const B>);
}
Notice the const. That's the important part which the original version of this question lacked. This compiles, and the following code works:
Bla bla;
boost::shared_ptr<A> a;
bla.foo(a);
However, if I switch from using boost::shared_ptr
to using std::shared_ptr
in the above examples, I get a compilation error that says:
"error: call of overloaded 'foo(std::shared_ptr<A>)' is ambiguous
note: candidates are: void foo(std::shared_ptr<const A>)
void foo(std::shared_ptr<const B>)
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.
Here is the full code that you can try compiling:
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
// #include <memory>
// using std::shared_ptr;
class A {};
class B {};
class Bla {
public:
void foo(shared_ptr<const A>) {}
void foo(shared_ptr<const B>) {}
};
int main() {
Bla bla;
shared_ptr<A> a;
bla.foo(a);
return 0;
}
By the way, this issue motivated me to ask this question about whether I should be using std::shared_ptr
at all yet ;-)