Is changing the pointer type of a private member variable in an interface class binary compatible?

Viewed 97
class Type1;
class Type2;
class __declspec(dllexport) Foo
{
  public:
    Foo();

  private:
  Type1 * m_p1;
  Type2 * m_p2;
};

Can I replace Type1 with Type3 without breaking binary compatibility?

Background: Unfortunately, this class does not use the pimpl idiom. To remedy this, I want to replace the pointer m_p1 with a pimpl-pointer.

Using Visual Studio 2010, Windows 7 and 10.

1 Answers

There's no way m_p2 can be accessed on the caller side from the code example that's provided.

Even then, this is very much breaking ABI technical speaking.

But if the assumption holds (100% sure that m_p2 isn't somehow exposed) then ABI would only break if changing that type pointed to could change the layout of the class.

That would seem a little strange though - even when it's impossible to give a guarentee at the c++ language level.

Therefore it is a matter of checking wrether the layout changes between the two versions for the specific setup. That can be checked with something like:

Foo* p = 0;
&p.m_p1;//offset to m_p1 (make a static member function for the check itself)

Since there's no virtual functions on Foo we luckily do not need to worry about that for this class - otherwise that should be tested for as well to be absolutely sure.

The only thing thats left is any possible names that could be left - that would of course still be impossible to change - but in itself shouldn't break ABI.

Related