Search code examples
c++design-patternspimpl-idiom

Other reasons or purposed for using the Pimpl Idoim


As the title suggests, I was wondering what other reasons, purposes or uses are there for the PImpl idiom other than reducing rebuild times.

Quoting an example from here:

(thread locals need to be behind a pimpl wall; they can't be exported directly

I don't understand what the user means, but I gather there are other interesting uses for the PImpl idiom,


Solution

  • From the book "Effective C++ Third Edition" by Scott Meyers (Item 31)

    "The key to this separation is replacement of dependencies on definitions with dependencies on declarations. That’s the essence of minimizing compilation dependencies: make your header files self-sufficient whenever it’s practical, and when it’s not, depend on declarations in other files, not definitions."

    For example in this case, moving the thread local variable to the definition file there is one less compilation dependency because the thread_local.hpp is not included in the declaration file.