vendredi 15 octobre 2021

Derived pointer to member to base pointer to member error

To support some compile time magic I would like to use pointers to members like:

struct BaseT
{
};

struct DerivedT: public BaseT
{
};

struct TestT 
{
    DerivedT testMem;

    typedef BaseT (TestT::* TestTMemPtr);

    constexpr TestT() = default;

    static constexpr TestTMemPtr testMemOffset()
    {
        return &TestT::testMem;
    }
};

int main()
{
    constexpr TestT test;
}

I cannot return a pointer-to-derived-member as a pointer-to-base-member, I get this with clang:

cannot initialize return object of type 'TestT::TestTMemPtr' (aka 'BaseT (TestT::*)') with an rvalue of type 'DerivedT TestT::*'

I checked it with gcc:

error: invalid conversion from 'DerivedT TestT::*' to 'TestT::TestTMemPtr' {aka 'BaseT TestT::*'}

Is this the normal behavior? I thought I can always use a derived pointer as a base pointer.

Aucun commentaire:

Enregistrer un commentaire