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.

281 questions
172
votes
11 answers

Is the PIMPL idiom really used in practice?

I am reading the book "Exceptional C++" by Herb Sutter, and in that book I have learned about the PIMPL idiom. Basically, the idea is to create a structure for the private objects of a class and dynamically allocate them to decrease the compilation…
Renan Greinert
  • 3,198
  • 3
  • 17
  • 28
139
votes
11 answers

Why should the "PIMPL" idiom be used?

Backgrounder: The PIMPL Idiom (Pointer to IMPLementation) is a technique for implementation hiding in which a public class wraps a structure or class that cannot be seen outside the library the public class is part of. This hides internal…
JeffV
  • 47,302
  • 31
  • 96
  • 120
120
votes
10 answers

Pimpl idiom vs Pure virtual class interface

I was wondering what would make a programmer to choose either Pimpl idiom or pure virtual class and inheritance. I understand that pimpl idiom comes with one explicit extra indirection for each public method and the object creation overhead. The…
Arkaitz Jimenez
  • 20,424
  • 9
  • 68
  • 99
62
votes
2 answers

How do I use unique_ptr for pimpl?

Here is a simplification of what I'm seeing when I try to use unique_ptr for pimpl. I chose unique_ptr because I really want the class to own the pointer - I want the lifetimes of the pimpl pointer and the class to be the same. Anyway, here is the…
emsr
  • 13,551
  • 6
  • 45
  • 59
57
votes
1 answer

How to use the Qt's PIMPL idiom?

PIMPL stands for Pointer to IMPLementation. The implementation stands for "implementation detail": something that the users of the class need not to be concerned with. Qt's own class implementations cleanly separate out the interfaces from the…
Kuba hasn't forgotten Monica
  • 88,505
  • 13
  • 129
  • 275
53
votes
4 answers

Should I use shared_ptr or unique_ptr

I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr or std::unique_ptr. I understand that std::unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively…
Clinton
  • 20,364
  • 13
  • 59
  • 142
48
votes
8 answers

The Pimpl Idiom in practice

There have been a few questions on SO about the pimpl idiom, but I'm more curious about how often it is leveraged in practice. I understand there are some trade-offs between performance and encapsulation, plus some debugging annoyances due to the…
Ryan Emerle
  • 14,410
  • 8
  • 47
  • 67
30
votes
7 answers

Pimpl idiom without using dynamic memory allocation

we want to use pimpl idiom for certain parts of our project. These parts of the project also happen to be parts where dynamic memory allocation is forbidden and this decision is not in our control. So what i am asking is, is there a clean and nice…
erelender
  • 5,817
  • 29
  • 47
26
votes
2 answers

Pimpl - Why can make_unique be called on an incomplete type

Why does the make_unique call compile? Doesn't make_unqiue require its template argument to be a complete type ? struct F; int main() { std::make_unique(); } struct F {}; The question orignated from my "problem" with my PIMPL…
AF_cpp
  • 549
  • 5
  • 15
26
votes
3 answers

Free function versus member function

What is the advantage of having a free function (in anonymous namespace and accessible only in a single source file) and sending all variables as parameters as opposed to having a private class member function free of any parameters and accessing…
Jana
  • 4,806
  • 5
  • 20
  • 28
26
votes
2 answers

What constitutes a valid state for a "moved from" object in C++11?

I've been trying to wrap my head around how move semantics in C++11 are supposed to work, and I'm having a good deal of trouble understanding what conditions a moved-from object needs to satisfy. Looking at the answer here doesn't really resolve my…
Pillsy
  • 9,461
  • 38
  • 68
21
votes
2 answers

Does the GotW #101 "solution" actually solve anything?

First read Herb's Sutters GotW posts concerning pimpl in C++11: GotW #100: Compilation Firewalls (Difficulty: 6/10) GotW #101: Compilation Firewalls, Part 2 (Difficulty: 8/10) I'm having some trouble understanding the solution proposed in GotW…
Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
21
votes
3 answers

std::unique_ptr pimpl in dll generates C4251 with visual studio

This is not a breaking issue but I like to clean my code from warnings so this is getting on my nerves. I have been using the c++11 version of pimpl idiom to hide the class implementation for my library the usual way. // dll header class…
kittikun
  • 1,645
  • 21
  • 31
19
votes
2 answers

Why is "error: invalid application of 'sizeof' to an incomplete type using unique_ptr" fixed by adding an empty destructor?

I am Pimpling off the class STFT. Compiles just fine with this in the header: class STFT; // pimpl off to prevent point name clash class Whatever { private: STFT* stft; and this in the implementation: #include "STFT.h" Whatever::Whatever() :…
learnvst
  • 13,927
  • 13
  • 65
  • 108
19
votes
5 answers

The pImpl idiom and Testability

The pImpl idiom in c++ aims to hide the implementation details (=private members) of a class from the users of that class. However it also hides some of the dependencies of that class which is usually regarded bad from a testing point of view. For…
Rimo
  • 447
  • 3
  • 8
1
2 3
18 19