The Pimpl idiom, also known as the Compilation Firewall or Opaque Pointer pattern, was popularized in the C++ community during the early 1990s. Originally developed to combat the long compilation times in C++, it was extensively used in frameworks like Qt. The pattern was first described by Jeff Sumner as the "Cheshire Cat" technique, named after the disappearing cat in Alice in Wonderland, because it makes the implementation details vanish from the public interface.
The pattern addresses several common C++ development challenges:
- Reduces compilation dependencies by moving implementation details to a separate file
- Maintains binary compatibility when implementation changes
- Hides implementation details from the public interface
- Reduces header file size and complexity
- Allows changing implementation without requiring client recompilation
- Protects intellectual property by keeping implementation details in compiled binary
// header file
class MyClass {
public:
MyClass();
~MyClass();
void doSomething();
private:
class Impl;
std::unique_ptr<Impl> m_pImpl;
};- Use smart pointers (std::unique_ptr) to manage implementation lifetime
- Declare special member functions explicitly
- Keep the public interface minimal and focused
- Use const references for string returns to avoid copying
- Manually managing implementation pointer (raw pointers)
- Exposing implementation details in header
- Not declaring special member functions when needed
- Unnecessary copying of data
Books that cover the pattern:
- "Modern C++ Design" by Andrei Alexandrescu
- "Effective Modern C++" by Scott Meyers
- "Large-Scale C++ Software Design" by John Lakos
Notable frameworks using Pimpl:
- Qt Framework
- Boost Libraries
- KDE Framework
The pattern is particularly useful in library development where ABI stability is important. However, it does come with a small performance overhead due to the extra indirection and shouldn't be used when that overhead is unacceptable for your use case.
This code is provided under the MIT License. Feel free to use, modify, and distribute as needed.
Contributions are welcome! Please feel free to submit a Pull Request.