I do not understand why in the following code, the shared_ptr<Derived<int>>
isn't implicitly converted to a shared_ptr<Base<int>>
:
#include <memory>
template <typename T>
class Base {
};
template <typename T>
class Derived : public Base<T> {
};
template <typename T>
T foo(std::shared_ptr<Base<T>>) {
return T{};
}
void main() {
foo(std::make_shared<Base<int>>());
foo(std::make_shared<Derived<int>>());
}
I came across convert std::shared_ptr<Derived> to const shared_ptr<Base>&, which seems related to me. Am I getting an error because I made a template of the function?
The error I get is:
E0304 no instance of function template "foo" matches the argument list
C2664 'std::shared_ptr<Base<int>> foo<int>(std::shared_ptr<Base<int>>)': cannot convert argument 1 from 'std::shared_ptr<Derived<int>>' to 'std::shared_ptr<Base<int>>'
<
when quoting things that have<...>
pairs in them, so they are not confused with HTML markup. I have edited the error messages for you so the<
show correctly. – Remy Lebeau