Detecting virtual inheritance

I

Imre

Hi

I'd like to create a special cast template function that can be used to
cast a base pointer to a derived one, and uses dynamic_cast if virtual
inheritance is involved, and static_cast otherwise. Unfortunately I
don't know how to properly detect VI.
I've seen a trick for detecting VI, that goes something like:

template <class B, class D>
struct IsVirtualBaseAndDerived
{
class Helper1: public D, public virtual B {};
class Helper2: public D {};
public:
enum { value = (sizeof(Helper1) == sizeof(Helper2)) };
};

This works well, but can't be used to detect if there are additional
inheritance levels between B and D, and VI is used somewhere in the
middle. For example:

class A {};
class B: public A {}; // not virtual
class C: public virtual B {}; // virtual

A is not really a VB of C here, but still, if we are to do an A* -> C*
cast, we should use dynamic_cast due to the VI between B and C.

So, my question is: how could I create some
IsVirtualInheritanceInvolved<B, D> template metafunction?

Thanks,

Imre
 
G

Gernot Frisch

So, my question is: how could I create some
IsVirtualInheritanceInvolved<B, D> template metafunction?

If I'm not totally wrong, you must provide a function like:
std::vector<std::string> InheritanceInformation();

to all of your classes.
Maybe someone's got a brilliant idea. This one propably sux a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top