PIMPL as a pattern
PIMPL as a design pattern is the choice to hide a class's implementation behind an opaque pointer so the public header stays stable. It is the same mechanism as the pimpl OOP entry, described here as an architectural habit rather than a language feature.
Detailed Explanation
Headers in C++ leak layout. Every private unordered_map include becomes your caller's include. Every extra field is an ABI break. The PIMPL pattern is the decision, at the boundary of a library or a widely included type, that the public class is a handle and the real state lives in a private Impl defined in the .cpp.
That decision has a shape you repeat:
- Forward-declare
struct Implin the header. - Store
unique_ptr<Impl>. - Declare the special members in the header; define them in the
.cppafterImplis complete. - Forward every public method into
impl_->.
The pattern's intent is compile firewall and ABI stability, not polymorphism. Callers never see Impl. There is no virtual dispatch unless you add it. Contrast with a strategy or a factory: those hide a type. PIMPL hides members.
When the pattern is justified: a public library class, a type included from fifty translation units, a class whose private includes are expensive (windows.h, JSON, SQL). When it is not: a .cpp-local helper, a 16-byte value type, a hot loop object. The extra allocation and the extra indirection are real.
Variants: a shared_ptr<Impl> if handles should share state (rare; that is becoming a different pattern). A const PIMPL (unique_ptr<const Impl> is wrong for mutation; use a const method that reads impl_). The "d-pointer" in Qt is PIMPL plus optional binary-compatible extension slots.
Document that copying is deep or deleted. A shallow copy of the pointer would share Impl and surprise value-semantic users.
See the OOP pimpl page for the destructor-in-the-.cpp rule and the code layout.
Code Examples
handle.hpp
// handle.hpp
#pragma once
#include <memory>
#include <string>
class Handle {
public:
explicit Handle(std::string name);
Handle(Handle&&) noexcept;
Handle& operator=(Handle&&) noexcept;
~Handle();
const std::string& name() const;
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};#include <utility>
#include <string>
#include <memory>
struct Handle::Impl { std::string name; };
Handle::Handle(std::string name)
: impl_(std::make_unique<Impl>(Impl{std::move(name)})) {}
Handle::Handle(Handle&&) noexcept = default;
Handle& Handle::operator=(Handle&&) noexcept = default;
Handle::~Handle() = default;
const std::string& Handle::name() const { return impl_->name; }
int main() {
Handle h{"ada"};
return h.name() == "ada" ? 0 : 1;
}Common Gotchas
Defaulting the destructor in the header
`sizeof(Impl)` is unknown; `unique_ptr` cannot delete. Define it in the `.cpp`.
Accidental shared state
if you copy the `unique_ptr` (you cannot) or switch to `shared_ptr` without saying so, two handles mutate one `Impl`.
This is not a virtual interface
adding `virtual` methods on `Handle` is a different pattern. PIMPL can contain a strategy inside `Impl` if you want both.
Allocation on every construction
do not PIMPL a `Point`.