"Delfin Rojas"
Not knowing what type of Foo it holds, how would you know what to do with the result of getP? Its type depends on the type of the Foo.
Yes, what I do with the result of getP would depend on the type of U. That is why I would need to get the right Foo<> back from FooBase *. Then I would pass this Foo<> through a template function that is specialized to a specific U and knows what to do with the result of getP().
In that case you're erasing the type information too early. You need
to instantiate the function template you mention above (call it f)
before you lose the type of Foo, e.g. make a vector of
shared_ptr<FooHolderBase>:
template <class T> void f(T*);
struct FooHolderBase
{
virtual ~FooHolderBase() {}
virtual void do_something_with_getP() = 0;
};
template <class U>
struct FooHolder
{
FooHolder(Foo<U> const& x) : held(x) {}
void do_something_with_getP()
{
f(held.getP()); // <== f gets instantiated
}
Foo<U> held;
};
template <class U>
void add_foo(
std::vector