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
19
votes
5 answers

Pimpl idiom with inheritance

I want to use pimpl idiom with inheritance. Here is the base public class and its implementation class: class A { public: A(){pAImpl = new AImpl;}; void foo(){pAImpl->foo();}; private: AImpl* pAImpl; }; class AImpl { …
Igor
  • 26,650
  • 27
  • 89
  • 114
19
votes
6 answers

Could C++ have not obviated the pimpl idiom?

As I understand, the pimpl idiom is exists only because C++ forces you to place all the private class members in the header. If the header were to contain only the public interface, theoretically, any change in class implementation would not have…
G S
  • 35,511
  • 22
  • 84
  • 118
18
votes
3 answers

pimpl for a templated class

I want to use the pimpl idiom to avoid having users of my library need our external dependencies (like boost, etc) however when my class is templated that seems to be impossible because the methods must be in the header. Is there something I can do…
David
  • 27,652
  • 18
  • 89
  • 138
17
votes
1 answer

What are the rules for noexcept on default defined move constructors?

Especially in connection with std::vector it is important that types are noexcept movable when possible. So when declaring a move constructor = default like in struct Object1 { Object1(Object1 &&other) =…
mfuchs
  • 2,190
  • 12
  • 20
16
votes
1 answer

Why does = default member initializer request instantiation of unique_ptr destructor while {} does not?

This is a follow up of this question: Does PIMPL idiom actually work using std::unique_ptr? The full example uses multiple files, so for the sake of this question I will reduce it here. The full working example is here:…
16
votes
1 answer

Heap-free pimpl. Incorrect or superstition?

Edit: This question dates from before C++17. These days std::launder or equivalent should be added to the line noise. I don't have time to update the code to match right now. I am aspiring to separate interface from implementation. This is primarily…
Jon Chesterfield
  • 2,251
  • 1
  • 20
  • 30
16
votes
3 answers

Is the pimpl idiom used in c#?

I come from a C# background and have recently started learning C++. One of the things I've encountered is the pimpl idiom. I've done C# development for some large firms, but never came across it. Maybe this is wrong, but my understanding is that…
Legion
  • 3,922
  • 8
  • 51
  • 95
12
votes
2 answers

What does "d" stand for in d-pointer?

Qt makes heavy use of the PIMPL idiom in their development process: https://wiki.qt.io/D-Pointer As I've read here: "The name 'd-pointer' stems from Trolltech's Arnt Gulbrandsen, who first introduced the technique into Qt, making it one of the first…
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
12
votes
3 answers

delegating into private parts

Sometimes, C++'s notion of privacy just baffles me :-) class Foo { struct Bar; Bar* p; public: Bar* operator->() const { return p; } }; struct Foo::Bar { void baz() { std::cout << "inside baz\n"; …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
12
votes
3 answers

Automate pimpl'ing of C++ classes -- is there an easy way?

Pimpl's are a source of boilerplate in a lot of C++ code. They seem like the kind of thing that a combination of macros, templates, and maybe a little external tool help could solve, but I'm not sure what the easiest way would be. I've seen…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
12
votes
1 answer

Java programming idiom : Private implementation class

I found this construct in some code. Is there any benefit to have a private static class implement A? This reminded me of the Pimpl idiom in C++. Is there any benefit to using the Pimpl idiom in Java? public abstract class A { public void…
Chip
  • 3,226
  • 23
  • 31
11
votes
3 answers

What are the pros and cons of using d-pointers?

d-pointers are heavily used in Qt, they are an implementation of pimpl idiom. I know advantages and disadvantages of pimpl idiom. But I have missed the advantages of d-pointers implementation. Here and here are the samples of d-pointers. Isn't it…
Dmitriy
  • 5,357
  • 8
  • 45
  • 57
11
votes
4 answers

Hide implementation by using a pointer (Pimpl idiom)

Is it somehow possible, to accomplish the following: x.hpp - this file is included by many other classes class x_impl; //forward declare class x { public: //methods... private: x_impl* impl_; }; x.cpp - the…
emesx
  • 12,555
  • 10
  • 58
  • 91
10
votes
2 answers

Implementing pImpl with minimal amount of code

What kind of tricks can be used to minimize the workload of implementing pImpl classes? Header: class Foo { struct Impl; boost::scoped_ptr self; public: Foo(int arg); ~Foo(); // Public member functions go…
Tronic
  • 10,250
  • 2
  • 41
  • 53
10
votes
1 answer

Pimpl with smart ptr - Why constructor/destructor needed

Lets consider following example (using c++11) A.hpp: #include class A { public: //A(); //~A(); private: struct AImpl; std::unique_ptr pImpl; }; main.cpp: #include "A.hpp" int main() { A a; } Using default…
meddle0106
  • 1,292
  • 1
  • 11
  • 22
1
2
3
21 22