0

There are several ways to detect this at run-time but I cannot find a way to determine if a pointer to a class will be offsetted at compile-time.

class MyA
{
public:
    int m_memberI;
};

class MyB
{
public:
    double m_memberD;
};

class MyC : public MyA, public MyB
{
};

void main()
{
    MyC myC;
    void* pVoidB = dynamic_cast< MyB* >( &myC );

    if( pVoidB != &myC )
    {
        std::cout << "Offset needed!" << std::endl;
    }
}

// **********************************************************************************
// Ideally, I would prefer something like this
//
// static_assert( std::cast_needs_offset< MyB*, MyC* >::value, "Offset detected!!!" );
// **********************************************************************************

The compilers obviously has that information, but I can't find a type_trait that could help me.

Any trick up your sleeves?

1 Answers1

0

I have found several questions about the same problem. It's not what I want and I cannot use those solutions but after a lot of research, I think it's impossible to accomplish what I am trying to do because first members of each class are unknown at a template library level.

See:

How to do a static assert that a pointer cast is trivial?

and

C++, statically detect base classes with differing addresses?

static_assert(offsetof(MyC, m_memberI) == offsetof(MyA, m_memberI));
static_assert(offsetof(MyC, m_memberD) != offsetof(MyB, m_memberD));

Unfortunately this is useless for my particular problem.

Community
  • 1
  • 1