PIMPL stands for Pointer to IMPLementation. The implementation stands for "implementation detail": something that the users of the class need not to be concerned with.
Qt's own class implementations cleanly separate out the interfaces from the implementations through the use of the PIMPL idiom. Yet, the mechanisms provided by Qt are undocumented. How to use them?
I'd like this to be the canonical question about "how do I PIMPL" in Qt. The answers are to be motivated by a simple coordinate entry dialog interface shown below.
The motivation for the use of PIMPL becomes apparent when we have anything with a semi-complex implementation. Further motivation is given in this question. Even a fairly simple class has to pull in a lot of other headers in its interface.
The PIMPL-based interface is fairly clean and readable.
// CoordinateDialog.h
#include <QDialog>
#include <QVector3D>
class CoordinateDialogPrivate;
class CoordinateDialog : public QDialog
{
Q_OBJECT
Q_DECLARE_PRIVATE(CoordinateDialog)
#if QT_VERSION <= QT_VERSION_CHECK(5,0,0)
Q_PRIVATE_SLOT(d_func(), void onAccepted())
#endif
QScopedPointer<CoordinateDialogPrivate> const d_ptr;
public:
CoordinateDialog(QWidget * parent = 0, Qt::WindowFlags flags = 0);
~CoordinateDialog();
QVector3D coordinates() const;
Q_SIGNAL void acceptedCoordinates(const QVector3D &);
};
Q_DECLARE_METATYPE(QVector3D)
A Qt 5, C++11 based interface doesn't need the Q_PRIVATE_SLOT
line.
Compare that to a non-PIMPL interface that tucks implementation details into the private section of the interface. Note how much other code has to be included.
// CoordinateDialog.h
#include <QDialog>
#include <QVector3D>
#include <QFormLayout>
#include <QDoubleSpinBox>
#include <QDialogButtonBox>
class CoordinateDialog : public QDialog
{
QFormLayout m_layout;
QDoubleSpinBox m_x, m_y, m_z;
QVector3D m_coordinates;
QDialogButtonBox m_buttons;
Q_SLOT void onAccepted();
public:
CoordinateDialog(QWidget * parent = 0, Qt::WindowFlags flags = 0);
QVector3D coordinates() const;
Q_SIGNAL void acceptedCoordinates(const QVector3D &);
};
Q_DECLARE_METATYPE(QVector3D)
Those two interfaces are exactly equivalent as far as their public interface is concerned. They have the same signals, slots and public methods.
Introduction
The PIMPL is a private class that contains all of the implementation-specific data of the parent class. Qt provides a PIMPL framework and a set of conventions that need to be followed when using that framework. Qt's PIMPLs can be used in all classes, even those not derived from
QObject
.The PIMPL needs to be allocated on the heap. In idiomatic C++, we must not manage such storage manually, but use a smart pointer. Either
QScopedPointer
orstd::unique_ptr
work for this purpose. Thus, a minimal pimpl-based interface, not derived fromQObject
, might look like:The destructor's declaration is necessary, since the scoped pointer's destructor needs to destruct an instance of the PIMPL. The destructor must be generated in the implementation file, where the
FooPrivate
class lives:See also:
The Interface
We'll now explain the PIMPL-based
CoordinateDialog
interface in the question.Qt provides several macros and implementation helpers that reduce the drudgery of PIMPLs. The implementation expects us to follow these rules:
Foo
is namedFooPrivate
.Foo
class in the interface (header) file.The Q_DECLARE_PRIVATE Macro
The
Q_DECLARE_PRIVATE
macro must be put in theprivate
section of the class's declaration. It takes the interface class's name as a parameter. It declares two inline implementations of thed_func()
helper method. That method returns the PIMPL pointer with proper constness. When used in const methods, it returns a pointer to a const PIMPL. In non-const methods, it returns a pointer to a non-const PIMPL. It also provides a pimpl of correct type in derived classes. It follows that all access to the pimpl from within the implementation is to be done usingd_func()
and **not throughd_ptr
. Usually we'd use theQ_D
macro, described in the Implementation section below.The macro comes in two flavors:
In our case,
Q_DECLARE_PRIAVATE(CoordinateDialog)
is equivalent toQ_DECLARE_PRIVATE_D(d_ptr, CoordinateDialog)
.The Q_PRIVATE_SLOT Macro
This macro is only needed for Qt 4 compatibility, or when targeting non-C++11 compilers. For Qt 5, C++11 code, it is unnecessary, as we can connect functors to signals and there's no need for explicit private slots.
We sometimes need for a
QObject
to have private slots for internal use. Such slots would pollute the interface's private section. Since the information about slots is only relevant to the moc code generator, we can, instead, use theQ_PRIVATE_SLOT
macro to tell moc that a given slot is to be invoked through thed_func()
pointer, instead of throughthis
.The syntax expected by moc in the
Q_PRIVATE_SLOT
is:In our case:
This effectively declares an
onAccepted
slot on theCoordinateDialog
class. The moc generates the following code to invoke the slot:The macro itself has an empty expansion - it only provides information to moc.
Our interface class is thus expanded as follows:
When using this macro, you must include the moc-generated code in a place where the private class is fully defined. In our case, this means that the
CoordinateDialog.cpp
file should end with:Gotchas
All of the
Q_
macros that are to be used in a class declaration already include a semicolon. No explicit semicolons are needed afterQ_
:The PIMPL must not be a private class within
Foo
itself:The first section after the opening brace in a class declaration is private by default. Thus the following are equivalent:
The
Q_DECLARE_PRIVATE
expects the interface class's name, not the PIMPL's name:The PIMPL pointer should be const for non-copyable/non-assignable classes such as
QObject
. It can be non-const when implementing copyable classes.Since the PIMPL is an internal implementation detail, its size is not available at the site where the interface is used. The temptation to use placement new and the Fast Pimpl idiom should be resisted as it provides no benefits for anything but a class that doesn't allocate memory at all.
The Implementation
The PIMPL has to be defined in the implementation file. If it is large, it can also be defined in a private header, customarily named
foo_p.h
for a class whose interface is infoo.h
.The PIMPL, at a minimum, is merely a carrier of the main class's data. It only needs a constructor and no other methods. In our case, it also needs to store the pointer to the main class, as we'll want to emit a signal from the main class. Thus:
The PIMPL is not copyable. Since we use non-copyable members, any attempt to copy or assign to the PIMPL would be caught by the compiler. Generally, it's best to explicitly disable the copy functionality by using
Q_DISABLE_COPY
.The
Q_DECLARE_PUBLIC
macro works similarly toQ_DECLARE_PRIVATE
. It is described later in this section.We pass the pointer to the dialog into the constructor, allowing us to initialize the layout on the dialog. We also connect the
QDialog
's accepted signal to the internalonAccepted
slot.The
onAccepted()
PIMPL method needs to be exposed as a slot in Qt 4/non-C++11 projects. For Qt 5 and C++11, this is no longer necessary.Upon the acceptance of the dialog, we capture the coordinates and emit the
acceptedCoordinates
signal. That's why we need the public pointer:The
Q_Q
macro declares a localCoordinateDialog * const q
variable. It is described later in this section.The public part of the implementation constructs the PIMPL and exposes its properties:
The
Q_D
macro declares a localCoordinateDialogPrivate * const d
variable. It is described below.The Q_D Macro
To access the PIMPL in an interface method, we can use the
Q_D
macro, passing it the name of the interface class.To access the PIMPL in a const interface method, we need to prepend the class name with the
const
keyword:The Q_Q Macro
To access the interface instance from a non-const PIMPL method, we can use the
Q_Q
macro, passing it the name of the interface class.To access the interface instance in a const PIMPL method, we prepend the class name with the
const
keyword, just as we did for theQ_D
macro:The Q_DECLARE_PUBLIC Macro
This macro is optional and is used to allow access to the interface from the PIMPL. It is typically used if the PIMPL's methods need to manipulate the interface's base class, or emit its signals. The equivalent
Q_DECLARE_PRIVATE
macro was used to allow access to the PIMPL from the interface.The macro takes the interface class's name as a parameter. It declares two inline implementations of the
q_func()
helper method. That method returns the interface pointer with proper constness. When used in const methods, it returns a pointer to a const interface. In non-const methods, it returns a pointer to a non-const interface. It also provides the interface of correct type in derived classes. It follows that all access to the interface from within the PIMPL is to be done usingq_func()
and **not throughq_ptr
. Usually we'd use theQ_Q
macro, described above.The macro expects the pointer to the interface to be named
q_ptr
. There is no two-argument variant of this macro that would allow to choose a different name for the interface pointer (as was the case forQ_DECLARE_PRIVATE
).The macro expands as follows:
The Q_DISABLE_COPY Macro
This macro deletes the copy constructor and the assignment operator. It must appear in the private section of the PIMPL.
Common Gotchas
The interface header for a given class must be the first header to be included in the implementation file. This forces the header to be self-contained and not dependent on declarations that happen to be included in the implementation. If it isn't so, the implementation will fail to compile, allowing you to fix the interface to make it self-sufficient.
The
Q_DISABLE_COPY
macro must appear in the private section of the PIMPLPIMPL And Non-QObject Copyable Classes
The PIMPL idiom allows one to implement copyable, copy- and move- constructible, assignable object. The assignment is done through the copy-and-swap idiom, preventing code duplication. The PIMPL pointer must not be const, of course.
Recall the in C++11, we need to heed the Rule of Four, and provide all of the following: the copy constructor, move constructor, assignment operator, and destructor. And the free-standing
swap
function to implement it all, of course†.We'll illustrate this using a rather useless, but nevertheless correct example.
Interface
For performance, the move constructor and the assignment operator should be defined in the interface (header) file. They don't need to access the PIMPL directly:
All of those use the
swap
freestanding function, which we must define in the interface as well. Note that it isImplementation
This is rather straightforward. We don't need access to the interface from the PIMPL, thus
Q_DECLARE_PUBLIC
andq_ptr
are absent.†Per this excellent answer: There are other claims that we should specialize
std::swap
for our type, provide an in-classswap
along-side a free-functionswap
, etc. But this is all unnecessary: any proper use ofswap
will be through an unqualified call, and our function will be found through ADL. One function will do.