Smart Pointers and the Pointer to Implementation Idiom -- Andreas Fertig
A post I wrote back in 2023 When an empty destructor is required resulted in feedback that I'd like to address in today's post.
Smart Pointers and the Pointer to Implementation Idiom
by Andreas Fertig
From the article:
In the 2023 post, I briefly mentioned PImpl idiom. I did not intend to make it the theme of the post. However, I got various questions about PImpl and smart pointers.
The goal of PImpl is to hide implementation details from clients. Since you can declare a pointer of an unknown class, you can shift the entire implementation of such an anonymous class into a
.cpp
file. That way, no client can see any details. Another benefit is that changes to that.cpp
file result in only minor recompiles. Maintaining the same in a header file would cause all.cpp
files, including this header, to recompile. At least the speed-up part is since C++20's modules are no longer necessary. And as long as you don't want to hide classified implementation in the.cpp
file modules, it also gives you the ability to mark constructs as private.