Checking non-virtual inheritance during compile time

119 Views Asked by At

I'm trying to determine during compile time if A is a non-virtual ancestor of B, without using any third party tools or 'is_base_of'. after quite a bit of search I learned a little about the SFINAE mechanism and this is the code I came up with:

template <typename P>
struct helper : public P {

};
template <typename CHILD, typename PARENT>
struct appendix : public CHILD, public helper<PARENT> {
    static constexpr appendix<CHILD,PARENT>* getClassType();

    static bool val(PARENT* p);
    static int val(...);

    enum {
        value = sizeof(val(getClassType())) == sizeof(bool)
    };
};

My thought was that if PARENT is an ancestor of CHILD the ambiguous conversion error will cause the compiler to use the seconds definition of 'val', but instead it just gives compile error.

1) Can someone suggest a working solution?

2) Why doesn't the code snippet above works? I'm no expert at all in anything related to templates (it looks amazingly hard to be an expert on this subject) but I don't see why it doesn't work

Edit: usage of static_cast is also forbidden

0

There are 0 best solutions below