Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2004-06-28 08:24:21


> I am writing a template class that processes several types. However my
> problem is that when the type of the template is boost::shared_ptr<P> (and
> only in that case) I need to do a boost::dynamic_pointer_cast<P> inside a
> method.
>
> So, I searched boost::type_traits library but could not find anything to
> detect a shared_ptr type at compile time. I would appreciate any ideas of
> how this could be implemented.

Easy: not a traits class, but a function overload:

template <class To, class From>
To* my_cast(From* p)
{ return dynamic_cast<To*>(p); }

template <class To, class From>
shared_ptr<To> my_cast(shared_ptr<From> p)
{ return dynamic_pointer_cast<From>(p); }

Then call

my_cast<Target*>(p);

rather than:

dynamic_cast<Target*>(p);

John.


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