r/cpp • u/PhilipTrettner • Sep 16 '25
VImpl: A Virtual Take on the C++ PImpl Pattern
https://solidean.com/blog/2025/the-vimpl-pattern-for-cpp/It's probably not super original but maybe some people will appreciate the ergonomics! So basically, classic pimpl is a lot of ceremony to decouple your header from member dependencies. VImpl (virtual impl) is solving the same issue with very similar performance penalties but has almost no boilerplate compared to the original C++ header/source separation. I think that's pretty neat so if it helps some people, that'd be great!
37
Upvotes
14
u/JVApen Clever is an insult, not a compliment. - T. Winters Sep 16 '25
Isn't that just putting all your data in an inner class (implemented in cpp) and have a unique_ptr to it?
12
u/_Noreturn Sep 16 '25
there is also iPPimpl, In-place pointer to implementation.
which stores a buffer suitable aligned for the object you want to store
this avoids the dynamic allocation but it ofc requires knowing the layout and size which isn't always easy, you can do a cmake step that calculates the sizes and puts them in a header.
here is a library which does this
https://github.com/vittorioromeo/SFML/blob/master/include%2FSFML%2FGraphics%2FGraphicsContext.hpp#L147-L148
19
u/bratzlaff Sep 16 '25
We do this at my work also, but this is just interfaces and an implementation of the factory pattern.
44
u/anonymouspaceshuttle Sep 16 '25
This is just the factory pattern with a fancy name.
7
u/Capable_Pick_1588 Sep 16 '25
Interesting! The inhouse framework in my workplace uses this a lot. It also makes creating mocks easy since everything is virtual already
4
u/AntiProtonBoy Sep 17 '25
What is the advantage of your approach, say, over something like this?
Implis a private struct inLogger. The guts, implementation details and dependencies forLogger::Implare never publicly visible and is contained theLogger.hpptranslation unit.2
u/mredding Sep 18 '25
Here's something closer to how I would do it. The header would contain:
And the source would contain:
The biggest difference is that the
loggermethods aren't virtual - they don't have to be because we don't have to rely on dynamic polymorphism. Instead, we rely on the fact that we can absolutely guarantee all instances ofloggerare in fact alogger_impl, which means we canstatic_castthiswith complete safety. Since the base class isn't polymorphic - we don't have another choice, anyway.The
loggerctor isprivateand the only access to it is thefriend class logger_impl. This means the client has no access to it. Even the factory method has no access to it. The only means of creating aloggeris through thecreatemethod, which returns alogger_impl.The advantage of the
static_castis that we don't have to pay the run-time cost of adynamic_cast(which is effectively nothing anyway, and amortized by the branch predictor), and we can avoid the complexity of having to check the result for null, and superfluous error code we know will never ever be executed anyway.The base class is just an interface, and is the closest thing C++ gets to a C opaque pointer style API.
2
u/conundorum Sep 21 '25
Ooh, this is clean. I put together an actual polymorphic PIMPL a few days ago, and getting it right without clogging up everything else is messy. Requires a secondary tag type, and possibly a template include file & copy-paste boilerplate (if you want to keep implementation details out of the base IMPL's header). Possibly also a secondary deleter type, if you want to maximise privacy; befriending a custom deleter removes the need to make anything
public.(I was also experimenting with maximising implementation privacy to extreme extents, so polyPIMPLs might be easier to implement if you don't mind making some of the implementation details
public. It wouldn't be as clean as your version, though.)1
u/john_wind Sep 21 '25
cppdb uses this pattern.
https://github.com/melpon/cppdb
I hate it :-D
0
u/ptrnyc Sep 16 '25
Marking the overridden methods 'final' might make that virtual call free.
0
u/pstomi Sep 16 '25
I like it a lot!
0
u/SlightlyLessHairyApe Sep 17 '25
This is all well and good, but it's kind of highlighting the key problem that in C++ the interface of a type and its implementation are defined in the same textual block. A class/structure definition cannot be split into multiple pieces with different logical purposes (e.g. public, private).
It also highlights the fact that you cannot refer to the public interface (and the name) of a thing without bringing its entire implementation unless whoever vends it also vends a forward declaration (hello
<iosfwd>) which is even more tedium.Both are just basic language problems and both idioms are, in that sense, fighting the language, plus forcing the compiler and runtime to do more work, although I do suspect that in the simple case most of them will do decent devirtualization.
My personal preference (YMMV) is not to fight the language but wait for things like modules and class extensions to let things be grouped in the right places and the right ways.
-5
u/tartaruga232 MSVC user, r/cpp_modules Sep 16 '25
I don't read white on black sorry. Really hurts my eyes.
-34
u/arihoenig Sep 16 '25
Anything based on run-time virtual dispatch is a defective design. Run-time virtual dispatch is both poorly performing and completely insecure. It has no place in software engineering discourse in 2025.