First something that should work, then something that doesn’t. Why doesn’t it is the question.
I declare two classes:
class Base { ... };
class Derived : public Base { ... };
I then have the following function elsewhere:
void foo(shared_ptr<Base> base);
The following code should work right?
share_ptr<Derived> derived;
foo(derived);
Now, forget the above, I declare three classes:
class Foo { ... };
template <typename TYPE> class Base { ... };
class Derived : public Base<Foo> { ... };
Elsewhere, I declare a templated function:
template <typename TYPE> void foo(shared_ptr<Base<TYPE> > base);
The following code does not work:
shared_ptr<Derived> derived;
foo(derived);
It says that there is no matching function foo(…) found which accepts share_ptr<Derived>
First, should the original example work? And second, what do you think could be the issue in the second example where I have a shared_ptr to a class that is derived from a specialized base class.
I don’t think the compiler will go through a level of indirection in that way. Rather, you can explicitly instantiate foo with TYPE set to Foo. Eg, the following compiles via g++: