Murray's Blog

ABI Stability of C++ Libraries

ABI Stability Is Difficult

It’s easy to break the ABI of a C++ class. Just add a new member variable to a public class or add a member variable to an implementation class that’s used a a member instance in a public class. C has the same problem, though it’s not quite as bad because they tend to use pointers more rather than member instances.

I have mostly had the luxury of working in these two situations:

But what if it’s a toolkit whose first public version will be released as ABI-stable?

Pimpl?

Pimpl is the only real solution that I know of. You use an opaque pointer as a member variable. The class to which the pointer points is not fully defined in the header. For instance:

class PrivateThing;

class Something
{
   ...

private:
  PrivateThing* m_priv;
};

The instance of PrivateThing is then created in the Something constructor.

This has some disadvantages:

I am fairly confident that this is the best solution, particularly as it’s equivalent to what GTK+ does in C. But please do comment if you have opinions or alternatives.

Exit mobile version