Boost logo

Boost Users :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-04-09 22:48:16


On Thursday 28 March 2002 04:44 pm, you wrote:
> Is there a way to detect that a class has been derived from
> boost::addable<T>, for example? I would like to be able to
> write something like this:
>
> template <typename T>
> void foo() {
> //if T derived from boost::addable<T,...>
> // . . .
> }
>
> The problem, of course, is the base class chaining feature.
> I do not see how to use boost::is_base_and_derived in this case.

If you still need to solve this problem after two weeks, the solution is to
use the 'sizeof' trick that is used by the boost::is_base_and_derived class:

typedef char yes_type;
typedef double no_type;

template<typename T, typename U, typename Base, typename Chained>
yes_type is_addable_helper(addable<T, U, Base, Chained>*);

no_type is_addable_helper(void*);

template<typename T>
struct is_addable {
  BOOST_STATIC_CONSTANT(bool,
    value = (sizeof(is_addable_helper((T*)0)) == sizeof(yes_type)));
};

        Doug


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