Questions tagged [pimpl-idiom]

The PIMPL idiom, also known as the compilation firewall or Cheshire Cat technique, is a "private implementation" technique useful in C++ and other similar statically compiled languages.

The idiom makes use of an opaque pointer to another object (usually of a nested class type) which provides the implementation. Member functions on the outer object forward to the implementation object, which is defined in a separate source file so that the implementation is not visible in the header defining the outer class. This is a special case of the Bridge pattern.

The term Cheshire Cat (because the body disappears leaving only a smile) is older, but the more common name PIMPL idiom was popularized by Herb Sutter in GOTW #24 (and revisited in GOTW #100 and #101).

Links:

Separating Interface and Implementation in C++ compares the PIMPL idiom to other related techniques.

318 questions
6
votes
3 answers

pimpl-idiom in template; which smart pointer?

I usually use a boost::scoped_ptr for pimpl's (for one reason because then I don't get surprises if I forget to deal with the copy constructor) With templates however I can't just put the destructor in the cpp file where the impl is fully defined…
odinthenerd
  • 5,422
  • 1
  • 32
  • 61
6
votes
4 answers

Pimpl idiom and internal object collaboration without friend declaration

I'm implementing several classes using the pimpl idiom and am coming across some design issues. Firstly, I've always seen pimpl done like this class Object { public: Visible(); ~Visible(); .. etc .. private: class ObjectImpl…
ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43
6
votes
5 answers

C++ advice from Code Complete on encapsulation?

In the section on "Good Encapsulation" in Code Complete, it is recommended to hide private implementation details. An example is given in C++. The idea is basically to completely separate the interface from the implementation, even in the class…
voithos
  • 68,482
  • 12
  • 101
  • 116
6
votes
7 answers

Hiding a C++ class in a header without using the unnamed namespace

I am writing a C++ header in which I define a class A { // ... }; that I would like to hide from the outside world (because it may change or even be removed in future versions of this header). There is also a class B in the same header that has…
Bjoern
  • 63
  • 1
  • 3
6
votes
5 answers

Is pimpl compatible with anonymous namespaces?

I am trying to use the pimpl pattern and define the implementation class in an anonymous namespace. Is this possible in C++? My failed attempt is described below. Is it possible to fix this without moving the implementation into a namespace with a…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
6
votes
1 answer

Implementing pImpl based wrapper around a class using variadic template functions

Summary I'm writing a library and a client application. In the library I'm trying to write a wrapper around another statically linked third-party library (specifically, spdlog) and am trying to use the pImpl idiom to completely hide it from the…
6
votes
2 answers

Will the upcoming addition of modules in c++ fix/alleviate the need for the pimpl idiom?

The pimpl idiom, as far as I can tell, hides a private implementation behind a forward declared symbol name so it can be declared and used in the private cpp module. Example: https://cpppatterns.com/patterns/pimpl.html As far as I can tell, because…
JeffV
  • 52,985
  • 32
  • 103
  • 124
6
votes
2 answers

How to create a private static const string when using the pimpl idiom

Background I have been learning how to implement the pimpl idiom using the newer c++11 method described by Herb Sutter at this page: https://herbsutter.com/gotw/_100/ I'm trying to modify this example by adding a member variable to the private…
hexsorcerer
  • 103
  • 7
6
votes
4 answers

Putting all methods in class definition

When I use the pimpl idiom, is it a good idea to put all the methods definitions inside the class definition? For example: // in A.h class A { class impl; boost::scoped_ptr pimpl; public: A(); int foo(); } // in A.cpp class…
Amnon
  • 7,652
  • 2
  • 26
  • 34
6
votes
5 answers

C++ pimpl idiom wastes an instruction vs. C style?

(Yes, I know that one machine instruction usually doesn't matter. I'm asking this question because I want to understand the pimpl idiom, and use it in the best possible way; and because sometimes I do care about one machine instruction.) In the…
Rob N
  • 15,024
  • 17
  • 92
  • 165
6
votes
5 answers

How to return a generic iterator (independent of particular container)?

I'd like to design a class Foo that stores various data of different types and returns iterators over them. It's supposed to be generic, so the user of Foo does not know how the data is stored (Foo could be using std::set or std::vector or…
Frank
  • 64,140
  • 93
  • 237
  • 324
6
votes
1 answer

Move of class with pimpl won't compile

In the following example, how is it possible that ~CImpl is called correctly but when the class needs to be moved, the compiler says it has an incomplete type? If the declaration of Impl is moved to the header it works, my question is how come the…
piotr
  • 5,657
  • 1
  • 35
  • 60
6
votes
1 answer

Inner class, pimpl and a friend class - disagreeing compilers

I was mucking about in some old library code, with the basic objective of refactoring it. This old code does not exactly comply to best practices and beauty (yes - friends are bad, and it has been removed after discovering the below - as it was an…
6
votes
4 answers

Pimpl idiom pointing to configurable implemenation

I've read that Pimpl is good for binary compatibility and interfaces are good for being able to easily switch out implementation. I need to combine both of these techniques to allow my application to be able to switch the underlying implementation…
Chris Andrews
  • 1,881
  • 3
  • 21
  • 31
5
votes
1 answer

Must provide destructor in the PIMPL

// main_pimpl_sample.cpp #include "pimpl_sample.hpp" using namespace std; int main() { pimpl_sample p; return 0; } // pimpl_sample.cpp #include "pimpl_sample.hpp" struct pimpl_sample::impl { }; pimpl_sample::pimpl_sample() : pimpl_(new…
q0987
  • 34,938
  • 69
  • 242
  • 387