Re: Why don't we use the pimpl idiom?

On Sun, 2014-04-27 at 19:39 +0200, Kjell Ahlstedt wrote:
Over and over again we find ourselves in situations where we want to
add data members to a class without breaking ABI, That can be, and has
been, done in an ugly way. See e.g.

I think it would be particularly useful in code that has lots of
implementation code, yes.
and the discussion at the end of

For the *mm wrapper libraries, we have traditionally tried to keep the
objects as small as possible. I can imagine there being some objection
to an extra pointer that is not used. I don't know if it would be a
significant increase in object size. I'm not generally against it. I
guess it's something to consider for the next big ABI break.

It wouldn't deal with all the ABI issues. For instance, it would not
allow us to add new virtual base classes when an underlying GObject
starts to implement a new "interface".

The pimpl (pointer to implementation) idiom, described at, offers a better solution. In a new
class, add

    struct Impl;
    Impl* pimpl;

struct Impl is defined in the .cc file. It's not part of the ABI. It
can be changed without breaking ABI.

The drawback of using the pimpl idiom consistently is of course that
it requires an extra pointer in each object instance. In many classes,
those pointers will never be needed.

Murray Cumming
murrayc murrayc com

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]