Detect derivation from e.g. addable

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.

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
participants (2)
-
Douglas Gregor
-
whastingsnj