Detecting Inheritence during compile time

Posted by Jagannath on Stack Overflow See other posts from Stack Overflow or by Jagannath
Published on 2010-12-25T02:31:17Z Indexed on 2010/12/25 2:54 UTC
Read the original article Hit count: 251

Filed under:

I am unable to figure out why this code is returning false. I had the first version of partial specialization. It did not work, I tried with the second version. It did not work either.

UPDATE: I wanted to check if "Derived" is publicly derived from "Base".

template<class TBase, class TDerived>
struct IsDerived
{
    public:
    enum { isDerived = false };
};


template<class TBase>
struct IsDerived<TBase, TBase>
{
    public:
    enum {  isDerived = true };
};


template<class TBase>
struct IsDerived<TBase&, TBase&>
{
    public:
    enum {  isDerived = true };
};

int main()
{
    cout << ((IsDerived<Base&, Derived&>::isDerived) ? "true" : "false")
         << endl;
    cout << ((IsDerived<const Derived*, const Base*>::isDerived) ?
            "true" : "false") << endl;

} 

© Stack Overflow or respective owner

Related posts about c++