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
known).

> HTH,
> --
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net