Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Making Pimpl Easy


Introduction

The Pimpl idiom is a simple yet robust technique to minimize coupling via the separation of interface and implementation and then implementation hiding. Successfully popularized by Sutter ([1,2]) under the cheeky Pimpl name (as for "pointer to implementation") the technique has been known long before under various names (the Cheshire Cat in [3], the Handle/Body in [4], the Bridge in [5]) and conceptually deployed in C and C++ as far back as at least early eighties ([6]).

In the domain of industrial software development where portability, implementation hiding and minimization of compilation dependencies come to the fore, the Pimpl technique can help great deal achieving those goals. Still, it does not seem to be very widely deployed. Sometimes that is due to efficiency considerations (valid or misguided) and probably more often due to additional implementation scaffolding and coding overhead associated with the technique.

I have been using the technique quite extensively lately and I found myself increasingly frustrated having to waste time cutting and pasting the same Pimpl-enabling set. I Googled around to see if there were easier deployment alternatives available. Not surprisingly, I found numerous Pimpl-related discussions and quite a few attempts to generalize the Pimpl idiom (see [7,8]). However, I was not able to find anything that I thought would fit my objective.

The conventional Pimpl deployment packs quite a punch by tackling two issues at once. The first is the separation of interface and implementation. The second is implementation hiding. The former is the obvious prerequisite for the latter. However, it is the latter that brings in all the benefits like portability and minimization of compilation dependencies.

All the implementations that I looked at managed the separation of interface and implementation quite well. However, the implementation hiding — the main quality and attraction of the Pimpl idiom — was routinely sacrificed in the name of generalization.

In addition, I was looking for a simple and convenient yet flexible and generic deployment technique. The conventional Pimpl deployment technique is uninspiring but already fairly straightforward. The complexity of any alternative technique is to be measured against that original and simplifying something so straightforward might be not as simple as it might seem.

In the end I rolled up my sleeves and came up with my own Pimpl generalization technique which (not surprisingly) I find quite satisfying. It's seemingly complete and broadly applicable, yet minimal, simple and pleasant to use. But I am admittedly biased. You decide for yourself. Read on. Try the code (located here). Let me know if there is anything that I missed or got wrong that could be improved.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.