PIMPL
PIMPL (pointer to implementation) hides a class's private data behind a pointer to an incomplete type defined in the .cpp file. The header stays stable when the internals change, so callers do not recompile.
Detailed Explanation
A C++ class definition lives in a header and lists every member. sizeof(User) is computed from that list. Add a std::unordered_map to User and every translation unit that included user.hpp must be recompiled, even if they only called User::name(). The layout changed. Include what the map includes, and those headers now sit in every caller too.
PIMPL splits the type. The header holds a public API and a std::unique_ptr<Impl> where Impl is only forward-declared. The .cpp defines struct User::Impl { ... } and implements the methods by forwarding to impl_. Callers see a pointer-sized object (plus any public members you chose to keep). Adding a field to Impl changes one .cpp.
That is the compilation firewall. It is also an ABI firewall: a shared library can grow Impl without changing sizeof(User) in the application, as long as the public API and the pointer stay put.
The costs are real. Every construction allocates. Every call through impl_ is a pointer chase (usually one, and the methods can still be inlined inside the .cpp). The type is no longer trivial; you must define or delete the special members. Because Impl is incomplete in the header, unique_ptr<Impl> cannot generate a destructor there: the default deleter needs sizeof(Impl) to call delete. Declare ~User(); in the header and define User::~User() = default; in the .cpp after Impl is complete. The same applies to move operations if they are defaulted: default them in the .cpp.
Copying a PIMPL type is a deep copy of Impl if you want value semantics, or deleted if the type is move-only. Write the copy constructor in the .cpp where you can *impl_.
When to use it: a widely included type whose private members change often, or a library type whose layout must stay stable across versions. When not to: a small value type used in inner loops (Point, Cents). Paying an allocation per Point is the wrong trade.
The pattern has other names: compiler firewall, Cheshire Cat, handle/body. std::unique_ptr is the modern handle. Raw new/delete in the constructor and destructor is the old one; do not go back to it.
A lighter alternative for a single optional resource is unique_ptr<T> to a public T that is just forward-declared. PIMPL is the same idea applied to the whole private section.
Code Examples
user.hpp
// user.hpp
#pragma once
#include <memory>
#include <string>
class User {
public:
explicit User(std::string name);
User(User&&) noexcept;
User& operator=(User&&) noexcept;
User(const User&) = delete;
User& operator=(const User&) = delete;
~User(); // defined in the .cpp
const std::string& name() const;
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};user.cpp
// user.cpp
#include "user.hpp"
#include <utility>
struct User::Impl {
std::string name;
};
User::User(std::string name)
: impl_(std::make_unique<Impl>(Impl{std::move(name)})) {}
User::User(User&&) noexcept = default;
User& User::operator=(User&&) noexcept = default;
User::~User() = default;
const std::string& User::name() const { return impl_->name; }Common Gotchas
Defaulting `~User()` in the header
`unique_ptr` tries to delete an incomplete `Impl` and the build fails with "invalid application of sizeof". Declare the destructor in the header; default it in the `.cpp`.
Moves defaulted in the header have the same incomplete-type problem
declare `User(User&&) noexcept;` and default it next to the destructor.
Const methods need `impl_` to be usable
`unique_ptr` has a const `get`. You may also store `const Impl*` accessors. Do not make `impl_` `mutable` just to silence const; that is a different bug.
Allocating on every construction
a factory that builds millions of PIMPL objects will show up in a profiler. Keep hot value types inline.