Boost logo

Boost Users :

From: Martin Apel (martin.apel_at_[hidden])
Date: 2007-08-21 07:17:48


Hi James,
>> the easiest way is something like the following:
>>
>> #include <boost/type_traits.hpp>
>> #include <boost/static_assert.hpp>
>>
>> BOOST_STATIC_ASSERT (boost::is_base_of<Base, Derived>::value);
>>
>> Hope this helps,
>>
>> Martin
>>
>
> Thanks Martin - I knew I had seen it somewhere.
>
> That covers the first point, but I've just realised I need to be a bit
> cleverer. The incoming type can either be the type itself or a pointer
> to the type, or a boost::shared_ptr to the type, and the type must be
> derived as the original question. Depending on which it is I need to do
> slightly different things, so I need to, effectively, conditionally
> compile a bit of code depending on the results of the tests, or assert
> if the type (after getting rid of the pointer/shared point bit) isn't
> derived from my base class.
>
> Basically I'm iterating through a vector of the incoming type and
> calling a function in the type stored. That function comes from the base
> class, but in order to be really generic I need to handle the shared_ptr
> and pointer cases.
>
> I think I need mpl for that, but I only get to chapter three of the book
> before my head starts to hurt. Any ideas/hints/places I can look?
>
>
I'm not sure if you really need mpl to do this. To me it looks as if you
need one template
and multiple partial specialization, something like the following:

template<typename T>
class Handler
{
 public:
   void doIt (T val)
   {
      .... // default implementation
   }
};

template<typename T>
class Handler<boost::shared_ptr<T> >
{
public:
   void doIt (boost::shared_ptr<T> val)
   {
     ... // Impl for shared_ptr
   }

template <typename T>
class Handler<T*>
....

Hope this helps,

Martin


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