Checking for non-virtual inheritance at compile time

I am trying to determine at compile time if A is not a virtual ancestor of B, without using any third party tools or 'is_base_of'. after doing a little searching, 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)
    };
};

      

I thought that if PARENT is an ancestor of CHILD, an ambiguous conversion error would force the compiler to use the val seconds definition, but instead it just gives a compilation error.

1) Can anyone suggest a working solution?

2) Why doesn't the code snippet above work? I'm not an expert in anything related to templates at all (it looks surprisingly difficult to be an expert on the subject), but I don't understand why it doesn't work.

Edit: using static_cast is also prohibited

+3


source to share





All Articles