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

Incomplete type used in nested name specifier for Pimpl Idiom

I have this error for the following code incomplete type ‘Foo::Pimpl’ used in nested name specifier AnotherFoo.hpp struct AnotherFoo { void methodAnotherFoo(Foo &); }; AnotherFoo.cpp #include "Foo.hpp" #include "AnotherFoo.hpp" void…
Mihai
  • 972
  • 2
  • 13
  • 35
4
votes
2 answers

Hiding variadic template implementation

I have some 3rdParty library with a method like this: bool Invoke(const char* method, Value* args, size_t nargs) It takes an array of its inner type (convertible to any primitive c++ types) and arg count as its inner params. In my code, I wrote…
Michael
  • 145
  • 5
4
votes
4 answers

C++: Creating a shared object rather than a shared pointer to an object

boost::shared_ptr really bothers me. Certainly, I understand the utility of such a thing, but I wish that I could use the shared_ptr as an A*. Consider the following code class A { public: A() {} A(int x) {mX = x;} virtual void…
JnBrymn
  • 24,245
  • 28
  • 105
  • 147
4
votes
1 answer

Pimpl Idiom with template member function

I want to use the Pimpl Idiom but I'm having a problem that one of the member functions is template function so it has to be implemented in a header file. For example this below works fine of course //Foo.h class Foo{ struct Impl; Impl*…
etrusks
  • 363
  • 3
  • 12
4
votes
3 answers

Pimpl idiom as template base class

I'm currently researching the pimpl idiom and there are very nice tutorials how it could be implement (e.g. here). But i have never seen it implemented as a base template class like this: #ifndef PIMPL_H #define PIMPL_H template class…
0x2648
  • 83
  • 8
4
votes
1 answer

Mocking classes that use Pimpl pattern

Let's say, I create a library libFoo that exposes an API this class class Book { public: Book(string const& title, string const& author); string const& title() const; string const& author() const; private: struct…
FCR
  • 1,103
  • 10
  • 25
4
votes
0 answers

using std::unique_ptr pimpl with explicit default destructor

When defining the following class class Foo { public: Foo (void); ~Foo (void) = default; protected: class FooImpl; std::unique_ptr _impl; //... }; Foo::Foo (void) : _impl (std::make_unique ()) { } I get the…
Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
4
votes
2 answers

Pimpl idiom implementation depending on a template function

Please consider the code below. The template parameter is a handler class that must provide the function bar(). I'm using the Pimpl idiom to hide the implementation details of Foo. Prior to having a template parameter the constructor definition was…
ksl
  • 4,519
  • 11
  • 65
  • 106
4
votes
1 answer

Overwriting operator new to merge PIMPL allocations

The PIMPL idiom is often used for public API of objects which sometimes also contain virtual functions. There, a heap allocation is often used to allocate the polymorphic object which is then stored in unique_ptr or similar. A famous example of this…
milianw
  • 5,164
  • 2
  • 37
  • 41
4
votes
2 answers

Better way of using an opaque pointer for Pimpl

I'm writing a C++ wrapper library around a number of different hardware libraries for embedded systems (firmware level), using various libraries from different vendors (C or C++). The API exposed by the header files should be vendor agnostic...…
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
4
votes
1 answer

Pimpl idiom used with a class member variable

Whats the correct way of implementing this class? //Header #include class MyClass { public: static foo() static foobar(); private: class pimpl; static boost::shared_ptr m_handle; static bool…
Stick it to THE MAN
  • 5,621
  • 17
  • 77
  • 93
4
votes
5 answers

Typedef private struct prototype in source file

In my class I have the need to keep a pointer to a structure which is defined in a library I use to implement it. Since this library is only used within the implementation file I would like to avoid including it in the header directly. At the same…
Svalorzen
  • 5,353
  • 3
  • 30
  • 54
4
votes
3 answers

Pimpl-idiom in the D programming language

D has a fantastic module system which reduces compilation times dramatically compared to C++. According to the documentation D still provides opaque structs and unions in order to enable the pimpl idiom. My question is: How can I declare a nested…
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
4
votes
3 answers

PIMPL const correctness

.h public: void doStuff() const; private: struct Private; Private * d; .cpp struct XX::Private { int count; } void XX::doStuff() const { d->count = 2; // I want an error here!! } Do you need furher explanation? Update: I…
0xbaadf00d
  • 2,535
  • 2
  • 24
  • 46
4
votes
3 answers

Workarounds for the forward-declared class enumeration problem?

I am maintaining a large code base and am using a combination of forward declarations and the pImpl idiom to keep compile times down and reduce dependencies (and it works really well,) The problem I have is with classes that contain public…
Rob
  • 76,700
  • 56
  • 158
  • 197