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
c ++ templates metaprogramming sfinae


source to share


No one has answered this question yet

Check out similar questions:

26
SFINAE for checking inherited member functions
22
C ++: Can virtual inheritance be detected at compile time?
12
Template function dependency on identical signatures, why does it work?
6
Using a child class as a base class template parameter and as a nested name pointer
3
Checking C ++ inheritance at compile time
3
MSVC pointer to overloaded function in template parameter
3
Template variable in sfinae context
1
Virtual / non-virtual inheritance
0
Create a type trait with lookup tables
-1
Return structure pointer from templated function



All Articles
Loading...
X
Show
Funny
Dev
Pics