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
.cppfile. That way, no client can see any details. Another benefit is that changes to that.cppfile result in only minor recompiles. Maintaining the same in a header file would cause all.cppfiles, 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.cppfile modules, it also gives you the ability to mark constructs as private.

Exploring how different languages solve the same problem often reveals interesting contrasts, especially when it comes to implementing powerful features like reflection. While C++26 aims to introduce introspection and code generation via P2996 and P3294, Rust’s approach using its derive macros offers a mature solution for code generation, even without introspection, highlighting different philosophies in language design and their practical applications.