Implementing the pimpl idiom
pimpl stands for pointer to implementation (but is also known as the Cheshire cat idiom or the compiler firewall idiom) and is an opaque pointer technique that enables the separation of the implementation details from an interface. This has the advantage that it enables changing the implementation without modifying the interface and, therefore, avoiding the need to recompile the code that is using the interface. This has the potential of making libraries using the pimpl idiom on their ABIs to be backward-compatible with older versions when only implementation details change. In this recipe, we will see how to implement the pimpl idiom using modern C++ features.
Getting ready
The reader is expected to be familiar with smart pointers and std::string_view
, both of which were discussed in previous chapters of this book.
To demonstrate the pimpl idiom in a practical manner, we will consider the following class, which we will then refactor following...