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
6
votes
2 answers

How to measure pimpl candidates?

The pimpl (also: compiler firewall) idiom is used to shorten compile times, at the cost of readability and a little runtime performance. At the moment a project takes to long to compile, how to measure the best pimpl candidates? I have my experience…
richelbilderbeek
  • 299
  • 2
  • 10
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
  • 11,371
  • 11
  • 72
  • 126
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,381
  • 1
  • 30
  • 56
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,742
  • 3
  • 19
  • 30
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
  • 31,246
  • 61
  • 222
  • 356
5
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,709
  • 4
  • 27
  • 40
5
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…
5
votes
2 answers

Pimpl with smart pointers in a class with a template constructor: weird incomplete type issue

When using smart pointers with the pImpl idiom, as in struct Foo { private: struct Impl; boost::scoped_ptr pImpl; }; the obvious problem is that Foo::Impl is incomplete at the point where the destructor of Foo is generated.…
Alexandre C.
  • 52,206
  • 8
  • 116
  • 189
5
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…
5
votes
1 answer

Pimpl idiom using shared_ptr working with incomplete types

I'm reading Effective Modern C++ by Scott Meyers and he's discussing the use of the pimpl idiom and pointing to the implementation class with unique_ptr, but there is an issue of special member functions (such as destructors) requiring the type to…
SergeantPenguin
  • 773
  • 3
  • 16
5
votes
2 answers

PIMPL and stack allocation

So I've been thinking about PIMPL and stack allocation. I've been writing a library and decided to use PIMPL to hide the private member of the class. That means I would have a class declared like this class Foo { private: class Handle; …
Anthony
  • 11,352
  • 9
  • 65
  • 100
5
votes
3 answers

Is this a good place to use PIMPL pattern?

I'm working on a library that defines a client interface for some service. Under the hood I have to validate the data provided by users and then pass it to "engine" process using Connection class from another library (note: the Connection class…
chalup
  • 7,894
  • 3
  • 31
  • 37
5
votes
2 answers

Should I use PIMPL everywhere?

My current project involves writing a C++ API and I have decided to use the PIMPL idiom. Should I use the PIMPL idiom everywhere in my project, for example I need to create a custom class that inherits from std::exception, should I design this class…
Soapy
  • 517
  • 15
  • 24
5
votes
1 answer

Is there a way to combine the benefits of compiler firewalls (Pimpl) and default-copyability?

Suppose I have a class with a private member, which is an implementation detail that clients of the class don't care about. This class is a value type and we want it to be copyable, eg #include // some header that pulls in many…
the_mandrill
  • 27,460
  • 4
  • 58
  • 90