PImpl
"Pointer to implementation" or "pImpl" is a C++ programming technique[1] that removes implementation details of a class from its object representation by placing them in a separate class, accessed through an opaque pointer:
// widget.h (interface) class widget { // public members private: struct impl; // forward declaration of the implementation class // One implementation example: see below for other design options and trade-offs std::experimental::propagate_const< // const-forwarding pointer wrapper std::unique_ptr< // unique-ownership opaque pointer impl>> pImpl; // to the forward-declared implementation class }; // widget.cpp (implementation) struct widget::impl { // implementation details };
This technique is used to construct C++ library interfaces with stable ABI and to reduce compile-time dependencies.
Explanation
Because private data members of a class participate in its object representation, affecting size and layout, and because private member functions of a class participate in overload resolution (which takes place before member access checking), any change to those implementation details requires recompilation of all users of the class.
pImpl breaks this compilation dependency; changes to the implementation do not cause recompilation. Consequently, if a library uses pImpl in its ABI, newer versions of the library may change the implementation while remaining ABI-compatible with older versions.
Trade-offs
The alternatives to the pImpl idiom are
- inline implementation: private members and public members are members of the same class
- pure abstract class (OOP factory): users obtain a unique pointer to a lightweight or abstract base class, the implementation details are in the derived class that overrides its virtual member functions
Compilation firewall
In simple cases, both pImpl and factory method break compile-time dependency between the implementation and the users of the class interface. Factory method creates a hidden dependency on the vtable, and so reordering, adding, or removing virtual member functions breaks the ABI. The pImpl approach has no hidden dependencies, however if the implementation class is a class template specialization, the compilation firewall benefit is lost: the users of the interface must observe the entire template definition in order to instantiate the correct specialization. A common design approach in this case is to refactor the implementation in a way that avoids parametrization, this is another use case for the C++ Core Guidelines T.61 Do not over-parametrize members and T.84 Use a non-template core implementation to provide an ABI-stable interface
For example, the following class template does not use the type T in its private members or in the body of push_back
template<class T> class ptr_vector { void **beg, **end, **cap; public: void push_back(T* p) { if (end == cap) reallocate(end - beg + 1); *end++ = p; } };
Therefore, private members can be transferred to implementation as-is, and push_back can forward to an implementation that does not use T in the interface either:
// header (ptr_vector.h) class ptr_vector_base { struct impl; // does not depend on T std::unique_ptr<impl> pImpl; protected: void push_back_fwd(void*); ... // see implementation section for special member functions }; template<class T> class ptr_vector : private ptr_vector_base { public: void push_back(T* p) { push_back_fwd(p); } }; // source (ptr_vector.cpp) struct ptr_vector_base::impl { void **beg, **end, **cap; void push_back(void* p) { if (end == cap) reallocate(end - beg + 1); *end++ = p; } void reallocate(size_t sz) { ... } }; void ptr_vector_base::push_back_fwd(void* p) { pImpl->push_back(p); } ptr_vector_base::ptr_vector_base() : pImpl(std::make_unique<impl>()) {}
Runtime overhead
- Access overhead: In pImpl, each call to a private member function indirects through a pointer. Each access to a public member made by a private member indirects through another pointer. Both indirections cross translation unit boundaries and so can only be optimized out by link-time optimization. Note that OO factory requires indirection across translation units to access both public data and implementation detail, and offers even fewer opportunities for the link time optimizer due to virtual dispatch.
- Space overhead: pImpl adds one pointer to the public component and, if any private member needs access to a public member, another pointer is either added to the implementation component or passed as a parameter for each call to the private member that requires it. If stateful custom allocators are supported, the allocator instance also has to be stored.
- Lifetime management overhead: pImpl (as well as OO factory) place the implementation object on the heap, which imposes significant runtime overhead at construction and destruction. This may be partially offset by custom allocators, since allocation size for pImpl (but not OO factory) is known at compile time.
On the other hand, pImpl classes are move-friendly; refactoring a large class as movable pImpl may improve performance of algorithms that manipulate containers holding such objects, although movable pImpl has an additional source of runtime overhead: any public member function that is permitted on a moved-from object and needs access to private implementation incurs a null pointer check.
This section is incomplete Reason: Microbenchmark?) |
Maintenance overhead
Use of pImpl requires a dedicated translation unit (a header-only library cannot use pImpl), introduces an additional class, a set of forwarding functions, and, if allocators are used, exposes the implementation detail of allocator use in the public interface.
Since virtual members are part of the interface component of pImpl, mocking a pImpl implies mocking the interface component alone. A testable pImpl is typically designed to allow full test coverage through the available interface.
Implementation
As the object of the interface type controls the lifetime of the object of the implementation type, the pointer to implementation is usually std::unique_ptr.
Because std::unique_ptr requires that the pointed-to type is a complete type in any context where the deleter is called, the destructor and the copy/move assignment operators must be user-declared and defined out-of-line, in the implementation file, where the implementation class is complete. This suppresses the move constructor, which has to be defined or defaulted.
Because when const member function calls a function through a non-const member pointer, the non-const overload of the implementation function is called, the pointer has to be wrapped in std::experimental::propagate_const or equivalent.
All private data members and all private non-virtual member functions are placed in the implementation class. All public, protected, and virtual members remain in the interface class (see GOTW #100 for the discussion of the alternatives).
If any of the private members needs to access a public or protected member, a reference or pointer to the interface may be passed to the private function as a parameter. Alternatively, the back-reference may be maintained as part of the implementation class.
If non-default allocators are intended to be supported for the allocation of the implementation object, any of the usual allocator awareness patterns may be utilized, including allocator template parameter defaulting to std::allocator and constructor argument of type std::pmr::memory_resource*.
Example
demonstrates a pImpl with const propagation, with back-reference passed as a parameter, without allocator awareness, and move-enabled without runtime checks
#include <iostream> #include <memory> #include <experimental/propagate_const> // interface (widget.h) class widget { class impl; std::experimental::propagate_const<std::unique_ptr<impl>> pImpl; public: void draw() const; // public API that will be forwarded to the implementation void draw(); bool shown() const { return true; } // public API that implementation has to call widget(int); ~widget(); // defined in the implementation file, where impl is a complete type widget(widget&&) = default; // Note: calling draw() on moved-from object is UB widget(const widget&) = delete; widget& operator=(widget&&); // defined in the implementation file widget& operator=(const widget&) = delete; }; // implementation (widget.cpp) class widget::impl { int n; // private data public: void draw(const widget& w) const { if(w.shown()) // this call to public member function requires the back-reference std::cout << "drawing a const widget " << n << '\n'; } void draw(const widget& w) { if(w.shown()) std::cout << "drawing a non-const widget " << n << '\n'; } impl(int n) : n(n) {} }; void widget::draw() const { pImpl->draw(*this); } void widget::draw() { pImpl->draw(*this); } widget::widget(int n) : pImpl{std::make_unique<impl>(n)} {} widget::~widget() = default; widget& widget::operator=(widget&&) = default; // user (main.cpp) int main() { widget w(7); const widget w2(8); w.draw(); w2.draw(); }
Output:
drawing a non-const widget 7 drawing a const widget 8
Notes
This section is incomplete Reason: note connection to value-semantic polymorphism |