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

How to get debug information for an abstract(?) pimpl in C++?

I have a wrapper class that delegates its work to a pimpl, and the pimpl is a pointer to a baseclass/interface with no data that is specialized in several different ways. Like this: class Base { void doStuff=0; }; class Derived { int x,y; …
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
2
votes
1 answer

How to check that PIMPL does not recompile client class

I try to understand the PIMPL idiom. I have several files, let's say "Implementation.cpp/Implementation.h" implement the PIMPL idiom: it contains a public interface and a private implementation. "Client.cpp/Client.h" use the public…
2
votes
3 answers

why constant function of implement class not be accessed in PIMPL?

I want to have a try in PIMPL in C++. In my case, I am using operator() to access private member. The interface class A and implement class AImpl all have operator() const and operator(). The code shown as follows: #include class…
Xu Hui
  • 1,213
  • 1
  • 11
  • 24
2
votes
0 answers

std::unique_ptr of an incomplete pimpl type surprisingly compiles fine when its host class is used polymorphically

I have a class foo that derives from base, and hence have a virtual destructor. Class foo implements the pimpl idiom with a std::unique_ptr of the incomplete type foo::impl. Like you can see in the code below, it doesn't compile as expected if I try…
2
votes
1 answer

When to use Pimpl pattern over Nested class in C++ or vice versa?

In C++ ,most of developers are using pimpl idiom or opaque pointers to hide the private data/implementation from the public API, for an example : => first case ## Opaque Pointer and PIMPL idiom ## // in header file class Node; class Graph { …
Buddhika Chaturanga
  • 957
  • 2
  • 14
  • 29
2
votes
1 answer

C++ Forward declaration before class or before member in the Pimple idiom?

Most Pimpl examples look as follows: UPDATE: both cases fail, i.e. with and without namespaces. See answer from R Sahu at https://stackoverflow.com/a/57103016/2712726 . class Impl must be qualified with class name Simpl // Simple.h #include…
2
votes
1 answer

Pimpl framework comments/suggestions requested

I've basically implemented a proposal, my question is, has it been done, and if so, where? And/or is there a better way to do what I'm doing? Sorry about the length of this post, I didn't know a better way to explain my approach other than providing…
Clinton
  • 22,361
  • 15
  • 67
  • 163
2
votes
1 answer

Is it possible to use an rvalue reference as pimpl handle?

I want to create a class with a Pimpl (Private implementation). Normally you would do: class A { private: class B; B* _pimpl = nullptr; } and then I would define it in the .cpp file. But I have to use dynamic allocation. Would it be possible…
Noel
  • 23
  • 2
2
votes
6 answers

How to omit private non-virtual methods from class definition?

Lets say I have something like the following: a.hpp: class B; class A { private: std::unique_ptr b_; } a.cpp: #include struct B { something_complicated x; } something_complicated& A::get_complicated() { return…
Clinton
  • 22,361
  • 15
  • 67
  • 163
2
votes
2 answers

Inner class depending on a template argument

Consider next example : #include #include template< int N, typename T > struct B { struct C; }; template< typename T > struct B< 0, T >::C { typedef T type; }; template< int N, typename T > struct B< N, T >::C { …
BЈовић
  • 62,405
  • 41
  • 173
  • 273
2
votes
2 answers

Explicitly calling the copy constructor of an object inside unique_ptr

I am using the pimpl idiom with a const std::unique_ptr to hold the class implementation. My class needs to support copy construction and copy assignement. What I'd like to do is manually call the copy constructor of the impl class inside the…
scx
  • 3,221
  • 1
  • 19
  • 37
2
votes
0 answers

DLL exported class: Abstract base class and pimpl?

I was looking into creation of DLL's using C++ lately and stumbled by accident into the discussion of "Abstract Base Classes vs. Pimpl Idiom" when it comes to ABI compatibility. I'm pretty new to dealing with DLLs so please be patient if I get a…
Rafael Pasquay
  • 328
  • 4
  • 11
2
votes
1 answer

C++17 custom iterator in a pimpl idiom context

I want to know how to have a c++ class to be iterable (stl compatible) without exposing the implementation ? The structure of the project is like : Stream class Stream { public: Stream(); [...] StreamIterator iter() { …
MaxC2
  • 343
  • 3
  • 10
2
votes
2 answers

Template instantiations and pimpl idiom with unique_ptr

I read this answer by Howard Hinnant (Is std::unique_ptr required to know the full definition of T?) and then this answer (How is a template instantiated?) and I was just thinking. If you have a class like so class Something { Something();…
Curious
  • 20,870
  • 8
  • 61
  • 146