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.
https://git.gnome.org/browse/libxml++/tree/libxml++/parsers/parser.cc

I think it would be particularly useful in code that has lots of
implementation code, yes.

https://git.gnome.org/browse/glibmm/tree/glib/glibmm/main.cc
https://git.gnome.org/browse/glibmm/tree/glib/glibmm/objectbase.h
and the discussion at the end of
https://bugzilla.gnome.org/show_bug.cgi?id=727822#c3.

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
http://en.wikipedia.org/wiki/Pimpl, offers a better solution. In a new
class, add

  private:
    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
www.murrayc.com




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