Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2005-05-02 01:40:50

> 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>:
<snip code>
> There are various other ways to write this basic idiom (e.g. using
> function pointers instead of building a class with virtual functions),
> but the essential thing is that f<U> gets instantiated when you still
> know U at compile-time.
> If you try to erase the type first, you will end up with what is
> essentially a massive type-switch:
> type_info dynamic_type = typeid(*some_foo_base_ptr);
> if (dynamic_type == typeid(Foo<A>))
> f(*dynamic_cast<Foo<A>*>(some_foo_base_ptr));
> else if (dynamic_type == typeid(Foo<B>))
> f(*dynamic_cast<Foo<B>*>(some_foo_base_ptr));
> ...
> which really sucks because it has to be maintained every time you
> write Foo<U> for some new U.

Thanks for the advice. Yes, that dynamic_cast switch was precisely what I as
trying to avoid and I think I can avoid it using what you suggested
(instantiating template function when the type of the object is still

> HTH,
> --
> Dave Abrahams
> Boost Consulting
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at