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

Viewing a pimpl from DLL in debugger

I am using the pimpl idiom to hide the implementation details of an interface so that I can have some measure of ABI protection. I'm not that well versed on the ins and outs of MS...using Linux for most of my development career. I'm unable to view…
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
9
votes
1 answer

Is pimpl idiom better than using always unique_ptr as member variables?

In my workplace we have this convention: almost every class (with very few exceptions) is implemented with unique_ptrs, raw pointers or references as member variables. This is because of compilation times: in this way you just need a forward…
Dundo
  • 714
  • 8
  • 12
9
votes
7 answers

How does the pimpl idiom reduce dependencies?

Consider the following: PImpl.hpp class Impl; class PImpl { Impl* pimpl; PImpl() : pimpl(new Impl) { } ~PImpl() { delete pimpl; } void DoSomething(); }; PImpl.cpp #include "PImpl.hpp" #include "Impl.hpp" void PImpl::DoSomething()…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
8
votes
6 answers

What patterns do you use to decouple interfaces and implementation in C++?

One problem in large C++ projects can be build times. There is some class high up in your dependency tree which you would need to work on, but usually you avoid doing so because every build takes a very long time. You don't necessarily want to…
Tobias
  • 6,388
  • 4
  • 39
  • 64
8
votes
4 answers

Is there any way to limit repetitive boilerplate when using the PIMPL idiom?

I have something like the following: // foo.h: class foo { public: foo(); ~foo(); // note: the param type repetition here is only incidental, assume the // functions can't easily be made to share type signatures void bar(a b, c…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
8
votes
4 answers

Is it possible to write an agile Pimpl in c++?

I've been playing with the Pimpl idiom and reaping all sorts of benefits from it. The only thing I haven't been too keen on is the feeling I get when I define the functions. Once in the header (P def) Once at the top of the .cpp (Impl def) Once…
user1684306
8
votes
2 answers

Pimpl + QSharedPointer - Destructor = Disaster

Yesterday I ran into misery which took me 24 hours of frustration. The problem boiled down to unexpected crashes occurring on random basis. To complicate things, debugging reports had absolutely random pattern as well. To complicate it even more,…
Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
7
votes
3 answers

C++: Tool to reduce compile-time dependencies automatically

After reading about the pimpl idiom I was horrified! Isn't there a tool out there that can inspect a .h/.cpp file and deduce what dependencies could be waivered?
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
7
votes
2 answers

Pimpl with unique_ptr : Why do I have to move definition of constructor of interface to ".cpp"?

The code would work file as long as I don't move the definition of constructor (of B) to the header B.h. B.h class Imp; //<--- error here class B{ public: std::unique_ptr imp; B(); //<--- move definition to here will compile…
javaLover
  • 6,347
  • 2
  • 22
  • 67
7
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
  • 843
  • 7
  • 16
7
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
  • 349
  • 3
  • 10
7
votes
2 answers

keeping private parts outside c++ headers: pure virtual base class vs pimpl

I recently switched back from Java and Ruby to C++, and much to my surprise I have to recompile files that use the public interface when I change the method signature of a private method, because also the private parts are in the .h file. I quickly…
skrebbel
  • 9,841
  • 6
  • 35
  • 34
7
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
  • 29,792
  • 6
  • 64
  • 93
7
votes
3 answers

Why should a pimpl be declared as a struct and not a class?

The canonical form of the pimpl idiom (from Herb Sutter's "Exceptional C++") is as follows: class X { public: /* ... public members ... */ protected: /* ... protected members? ... */ private: /* ... private members? ... */ struct XImpl; …
lindelof
  • 34,556
  • 31
  • 99
  • 140
7
votes
2 answers

Complete encapsulation without malloc

I was experimenting with C11 and VLAs, trying to declare a struct variable on the stack with only an incomplete declaration. The objective is to provide a mechanism to create a variable of some struct type without showing the internals (like the…
Mabus
  • 1,418
  • 12
  • 20
1 2
3
21 22