logoalt Hacker News

zabzonktoday at 7:41 AM6 repliesview on HN

Hmm. Do people use PIMPL that much (I have used it, but rarely) that we need std library support (and testing, documentation, understanding)? Just asking.


Replies

dvratiltoday at 7:55 AM

It's often used in libraries where you need to guarantee ABI compatibility. Fixing a bug or implementing a feature may require adding a new member into the class, which would change its size (thus break ABI compatibility). PIMPL is the typical solution here, since the inner/impl class is not part of the public ABI.

I also like to use it sometimes to "hide" private methods and their documentation into PIMPL, so the public header is kept clean.

show 2 replies
flohofwoetoday at 7:50 AM

This std::indirect thingie looks more like a general helper for any data 'dangling off' an object, not limited to pimpl.

Not sure how much pimpl is used in reality, but it's a pretty ok solution to speed up build times (apart from unity builds), because it avoids having to include headers that are only needed for the private state into the public interface header.

feverzsjtoday at 7:48 AM

Yes, if you actually care compile times.

show 3 replies
green7eatoday at 7:48 AM

I remember using it all the time for the Windows headers because they pollutes the compilation unit like you wouldn't believe — the rule was to only include them in c/cpp files.

show 1 reply
neonz80today at 7:47 AM

They didn't add PImpl support, they added std::indirect which can be used for PImpl among other things.

seanhuntertoday at 7:47 AM

Back when I used to write C++ it was used all over the place. Admittedly that was a log time ago.

show 1 reply