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
3
votes
1 answer

Pimpl, private class forward declaration, scope resolution operator

Consider these two classes that employ the Pimpl idiom: ClassA: Pimpl class forward declaration and variable declaration on separate lines ClassA.h: #include class ClassA { public: ClassA(); ~ClassA(); void SetValue( int value…
Quokka
  • 174
  • 1
  • 3
  • 10
3
votes
1 answer

unique_ptr pimpl and incomplete types

This is not a dupe of std::unique_ptr with an incomplete type won't compile. Consider the code below: #include struct X { X(); ~X(); struct Impl; std::unique_ptr up_; }; struct Impl {}; // fully visible…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
3
votes
1 answer

Why in C++ the size of a class must be always known by its users?

Let's say that a class is completely defined in its .cpp file, so that in the source file you can find: The constructor defined The desctructor defined Every method defined Than why its private member variables must still be in the header…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
3
votes
2 answers

Differences between these two PIMPL approaches

So when trying to get in touch with the PIMPL idiom one finds two common ways of doing it: Using forward declaration outside a class: class PimplClass; class VisibleClass { private: PimplClass* d_ptr; }; Using forward declaration inside a…
binaryguy
  • 1,167
  • 3
  • 12
  • 29
3
votes
2 answers

ABI in pimpl idiom with unique_ptr

My goal is to provide abi compatibility for my new library. I look toward the using of unique_ptr instead of raw pointers. But I'm afraid that if I update standard library, I may break abi. Is it true? Is there any guaranty of abi-stability for…
Bikineev
  • 1,685
  • 15
  • 20
3
votes
1 answer

Debugging PIMPL in Visual Studio(2012)

I've got a DLL with following class: Cat.h: #ifndef CAT_H #define CAT_H class __declspec( dllexport ) Cat { class CatImpl; // Not defined here CatImpl *cat_; // Handle public: Cat(); int a; }; #endif Cat.cpp: #include…
stiopa
  • 137
  • 9
3
votes
0 answers

Spotting compilation-time bottlenecks in order to compilation firewall efficiently

I have this big C++ boostified project that takes ages to build so i'm trying to set up compilation firewalls. Now I could sprinkle pimpls or pure interfaces following my intuition but that doesn't seem very efficient... usually if i wanted to…
Gurg Hackpof
  • 1,304
  • 1
  • 13
  • 26
3
votes
2 answers

C++ Pimpl vs Pure Virtual Interface Performance

I realize there are quite a few posts on this subject, but I am having trouble finding the answer to this exact question. For function calls, which is faster, a pure-virtual interface or a pimpl? At first glance, it seems to me that the pure-virtual…
2
votes
3 answers

portable c++ alignment?

I want to apply the Pimpl idiom with local storage idiom: mytype.h class mytype { struct Impl; enum{ storage = 20; } char m_storage[ storage ]; Impl* PImpl() { return (Impl*)m_storage; } public: mytype(); ~mytype(); void myMethod();…
lurscher
  • 25,930
  • 29
  • 122
  • 185
2
votes
1 answer

pimpl, std::unique_ptr and constexpr constructor

I'm reviewing a non-compiling code where I find a design similar to this: B.h #include class A; class B { private: int val; // pImpl idiom std::unique_ptr pImpl; constexpr B(int x): val(x){}; virtual…
Oersted
  • 769
  • 16
2
votes
0 answers

unique_ptr in PImpl and ABI compatibility

Consider a standard class implementation that makes use of PImpl, but uses a std::unique_ptr to manage the implementation object's lifetime: class MyClass { public: class Impl; MyClass(); ~MyClass(); // Note: No default here void…
Raven
  • 2,951
  • 2
  • 26
  • 42
2
votes
2 answers

"multiply defined symbols found" how is my destructor being defined twice?

Question: What am I doing to cause a multiple definition symbol linker error? OSFrameworkWindows10Module.ixx module; #include export module OSFrameworkWindows10Module; export class OSFramework { private: struct impl; …
2
votes
1 answer

Constructor and destructor in c++ when using the pimpl idiom

I come from Java that has a different way in handling what's private and has to be hided regarding a class implementation and it also has a garbage collector which means there is no need for a destructor. I learned the basics of how to implement a…
Spyromancer
  • 435
  • 1
  • 3
  • 10
2
votes
2 answers

In pimpl idiom, if without destructor, compiler would bring out error: can't delete an incomplete type

I am using visual studio on windows10 using C++. And I'm learning about pimpl idiom, what confuses me is that if without destructor of employee, compiler would bring out error: can't delete an incomplete type. I don't understand why I need a…
2
votes
1 answer

Unique_ptr usage for pimpl - doesn't compile even though destructor is declared

I'm trying to use unique_ptr for a pimpl idiom. So I'm declaring a destructor inside the class so the unique_ptr deletion is not instantiated where the impl class is not defined, and then I define it in another file. This is my…
Asaf
  • 4,317
  • 28
  • 48