|
Boost : |
From: Aljaz Noe (aljaz.noe_at_[hidden])
Date: 2006-07-26 15:10:49
Hi!
Recently there was a proposal to add a class template that simplifies the
implementation of a PIMPL idiom to Boost. Unfortunately I missed the review
deadline so I could not submit a proper review. But I do have some
experience with PIMPL class template that I would like to share with you.
I wrote similar class template a couple of years ago (see attached file).
The basic idea is the same as in a proposed PIMPL class. I call it
ImplWrapper, because it is used to wrap class implementation details.
Besides automating the standard PIMPL functionality, it also addresses the
following issues:
1. The Impl object can be allocated in a buffer that is provided by
ImplWrapper class. The reason for this is to avoid unnecessary allocation of
small objects on the heap and to increase performance. In fact, I believe
that with proper compiler optimizations, there should be no difference
between using ImplWrapper class and storing data members in class directly
(additional pointer indirection normally associated with a PIMPL idiom is
eliminated).
The drawback is that the size of the Impl object must be known (guessed) in
advance. Sometimes this is not possible, so there is also alternative
implementation that allocates Impl object on the heap.
2. Arbitrary parameters can be passed to Impl constructor by wrapping them
in a temporary struct. This struct Par, is forward declared within
ImplWrapper class and can be specialized for each type of Impl class. There
is no need for policies or other design complications.
3. Simplicity. The overall design seems simple, yet functional. But I do
admit that this opinion may by biased. ;-)
Since the above issues were not resolved within the proposed PIMPL class (I
only quickly glanced over the implementation and a PIMPL thread in this list
at the end of May, so I might have skipped something), I decided to post my
implementation here.
I believe that PIMPL idiom is so often used in C++, that it should have some
support in Boost (maybe even in future versions of standard C++ library).
Hopefully this post will contribute to that goal.
Any comments are greatly appreciated.
Regards,
Aljaz
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk