In the following code, why is the call to bar(std::shared_ptr<B>)
ambiguous, but bar(std::shared_ptr<A>)
is not?
The intent of foo
is to treat const and non-const pointers differently. Can I disambiguate this in any other way than changing the call site to bar(std::dynamic_pointer_cast<A>(b))
?
#include <memory>
struct A {};
struct B : A {};
void foo(std::shared_ptr<const A>) { }
void foo(std::shared_ptr<A>) { }
void bar(const A &) { }
void bar(A &) { }
int main() {
A a;
bar(a); // ok
B b;
bar(b); // ok
auto ap = std::make_shared<A>();
foo(ap); // ok
auto bp = std::make_shared<B>();
foo(bp); // ambiguous
return 0;
}