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
4
votes
3 answers

Why binary compatibility?

I am learning PIMPL idiom. One of its advantage is binary compatibility. I am wondering what the advantages of binary compatibility are. Thanks!
user1899020
  • 13,167
  • 21
  • 79
  • 154
4
votes
2 answers

Is it legal in C++11 to inherit from private nested type?

What I am trying to do is to have a variable-size POD as a Pimpl in my library class: // header file class foo { public: // ctors, copy, move, dtor, etc. private: struct impl; // forward-declared impl* pimpl; // pointer to private…
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
4
votes
1 answer

Hiding library dependencies from library users

Consider I'm writting a static library. Let it has a class Foo // mylib.h #include class Foo { // ... private: type_from_dependent_library x; } As you can see this library (let call it mylib)…
maverik
  • 5,508
  • 3
  • 35
  • 55
3
votes
0 answers

C++ PIMPL using std::unique_ptr and rule of five

If having a c++ class with a pimpl using std::unique_ptr and solving the fact that the pimpl class is incomplete in the header by declaring my own destructor (I know i could also provide a custom deleter, but lets go with the dtor for this one). If…
joaerl
  • 1,012
  • 1
  • 10
  • 21
3
votes
2 answers

Is there any reason not to use pimpl to implement move support in C++?

Obviously, pimpl is not strictly necessary, but if the class is designed the "normal" way, then, it seems that moving is not going to give you the full intended benefit: moving is supposed to be cheap; in particular, it should be much faster than…
allyourcode
  • 21,871
  • 18
  • 78
  • 106
3
votes
2 answers

Pimpl idiom and swap

I have several classes that are based on the PIMPL idiom (where a unique_ptr refers to the actual implementation struct). I haven't added a friend swap function (as described here) as, to my knowledge, the standard std::swap uses move-semantics…
Ben
  • 1,519
  • 23
  • 39
3
votes
1 answer

How to avoid downcasting in this specific class hierarchy design?

I've got an assignment to create a sort of a multi-platform C++ GUI library. It wraps different GUI frameworks on different platforms. The library itself provides an interface via which the user communicates uniformly regardless of the platform he's…
mrdecompilator
  • 155
  • 1
  • 8
3
votes
3 answers

How do you exchange private data with Pimpl without exposing your internals?

If you have an object B that needs a copy of a private member of an object A, and the private member is hidden by a Pimpl, how do you make it happen without exposing your internals? // Foo.h class Foo { private : struct impl ; impl * pimpl…
joey_89
  • 35
  • 2
3
votes
1 answer

Compile error with gcc when in-class initializing unique_ptr of incomplete type to nullptr

I'm writing some code using pimpl idiom with unique_ptr. When I tried to use in-class initialization to set unique_ptr to nullptr by default, gcc gave a compile error, while clang and msvc both successfully compiled the code. And if I didn't use…
X. Sun
  • 315
  • 3
  • 15
3
votes
2 answers

pimpl: Avoiding pointer to pointer with pimpl

In this question I asked "pimpl: shared_ptr or unique_ptr" I've been convinced that the proper usage of the pimpl idiom is to use a unique_ptr, not a shared_ptr. It should act to the user as if there is no pointer at all, whereas quite clearly the…
Clinton
  • 22,361
  • 15
  • 67
  • 163
3
votes
3 answers

Pimpl idiom usage in Qt, searching for laconic way

My problem with Qt & pimpl is not actually a problem, more a request for best-practice advice. So: we've got quite a large project with lots of GUI and other Qt classes. Readability of headers is required for fine collaboration, reducing…
MasterAler
  • 1,614
  • 3
  • 23
  • 35
3
votes
1 answer

Pimpl Idiom Memory Usage

In my new workplace, the code has a heavy use of Pimpl idiom and the reason is to reduce the compile time. But I have a basic query - Doesn't pimpl require dynamic allocation of memory? So, effectively we are allocating more memory in heap than…
3
votes
1 answer

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…
Torrie Merk
  • 105
  • 1
  • 8
3
votes
2 answers

create a pointer to new class which contains string

I think new or smart pointer needs the size of memory. If a class contains string, I allocate the class. Then I assign a new value to the string, is it over the memory? // a.hpp #include #include #include class A…
Aristotle0
  • 317
  • 2
  • 9
3
votes
2 answers

Why is this type incomplete when using the PIMPL idiom?

I'm using the PIMPL idiom, and specifically I'm using the template provided from this post. Given the set of classes below and compiling with VS2015 Update 3, I'm getting compile errors: Error C2027 use of undefined type 'C::C_impl' (compiling…
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88