Is there a way to find out whether a class is a direct base of another class?

Posted by user176168 on Stack Overflow See other posts from Stack Overflow or by user176168
Published on 2010-02-14T17:36:43Z Indexed on 2010/05/15 20:04 UTC
Read the original article Hit count: 207

Filed under:
|
|
|

Hi I'm wondering whether there is a way to find out whether a class is a direct base of another class i.e. in boost type trait terms a is_direct_base_of function. As far as I can see boost doesn't see to support this kind of functionality which leads me to think that its impossible with the current C++ standard.

The reason I want it is to do some validation checking on two macro's that are used for a reflection system to specify that one class is derived from another e.g.

header.h:

#define BASE     A
#define DERIVED  B

class A {};
class B : public A 
{
   #include <rtti.h>
};

rtti.h:

// I want to check that the two macro's are correct with a compile time assert
Rtti<BASE, DERIVED> m_rtti;

Although the macro's seem unnecessary in this simple example in my real world scenario rtti.h is a lot more complex.

One possible avenue would be to compare the size of the this pointer with the size of a this pointer cast to the base type and some how trying to figure out whether its the size of the base class itself away or something (yeah your right I don't know how that would work either! lol)

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost