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

C++ pimpl idiom: return impl pointer in getter of API class

I use the pimpl idiom for classes in the public API of my library to benefit from it's properties like ABI stability. In my non-public code it would be convenient to have access to the impl object to directly operate on it. Is it considered bad…
Jus Gru
  • 106
  • 4
5
votes
0 answers

include a class that has std::unique_ptr as a field, while "T" is incomplete type

I created a tiny test case for std::unique with incomplete type B. Test.h #pragma once #include class B; //<--- compile error here class Test{ std::unique_ptr bPtr; //#1 need to move destructor's implementation to…
javaLover
  • 6,347
  • 2
  • 22
  • 67
5
votes
2 answers

PIMPL and stack allocation

So I've been thinking about PIMPL and stack allocation. I've been writing a library and decided to use PIMPL to hide the private member of the class. That means I would have a class declared like this class Foo { private: class Handle; …
Anthony
  • 12,177
  • 9
  • 69
  • 105
5
votes
3 answers

Is this a good place to use PIMPL pattern?

I'm working on a library that defines a client interface for some service. Under the hood I have to validate the data provided by users and then pass it to "engine" process using Connection class from another library (note: the Connection class…
chalup
  • 8,358
  • 3
  • 33
  • 38
5
votes
1 answer

C++ Pimpl Idiom Incomplete Type using std::unique_ptr

I apologize for the large amount of code required to demonstrate the issue. I am having a problem using the pimpl idiom with std::unique_ptr. Specifically the problem seems to occur when one class (which has pimpl'ed implementation) is used as…
Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58
5
votes
2 answers

Should I use PIMPL everywhere?

My current project involves writing a C++ API and I have decided to use the PIMPL idiom. Should I use the PIMPL idiom everywhere in my project, for example I need to create a custom class that inherits from std::exception, should I design this class…
Soapy
  • 557
  • 14
  • 29
5
votes
7 answers

Private members in pimpl class?

Is there any reason for the implementation class as used in the pimpl idiom to have any private members at all? The only reason I can really think of is to protect yourself from yourself -- i.e. the private members serve to enforce some kind of…
Anne
  • 480
  • 5
  • 14
5
votes
1 answer

Alternative PImpl Idiom - advantages vs disadvantages?

The traditional PImpl Idiom is like this: #include struct Blah { //public interface declarations private: struct Impl; std::unique_ptr impl; }; //in source implementation file: struct Blah::Impl { //private…
LB--
  • 2,506
  • 1
  • 38
  • 76
4
votes
2 answers

CMake: transitive dependency linking of static libs "in-place" instead of appending

the title may be a bit too short to be clear enough. We have a complex C/C++-project which is built and linked in a lot of separate targets as static libraries. So my problem is that the target_link_libraries does transitive linking but in a wrong…
NetoBF
  • 127
  • 6
4
votes
5 answers

Is there any advantage to the pimpl idiom with a templated class?

It is my understanding that the primary benefit of the pimpl idiom is to hide the data members in the implementation file instead of the header. However, templates need to be fully defined in the header in order for the compiler to instantiate them…
Oscar Korz
  • 2,457
  • 1
  • 18
  • 18
4
votes
2 answers

Are methods in the pimpl inlined?

Considering next simple example: The header: // a.hpp #ifndef A_HPP #define A_HPP #include class A { public: A(); int foo(); private: struct Imp; std::auto_ptr< Imp > pimpl; }; #endif // A_HPP The implementation : //…
BЈовић
  • 62,405
  • 41
  • 173
  • 273
4
votes
1 answer

Is changing the pointer type of a private member variable in an interface class binary compatible?

class Type1; class Type2; class __declspec(dllexport) Foo { public: Foo(); private: Type1 * m_p1; Type2 * m_p2; }; Can I replace Type1 with Type3 without breaking binary compatibility? Background: Unfortunately, this class does not use…
Fabian
  • 4,001
  • 4
  • 28
  • 59
4
votes
2 answers

Should setters of PIMPL classes be const member functions?

I use "pointer to private implementation" classes often. The setter methods of those classes can technically be const member functions, such as this: class MyPrivateClass { public: int something = 1; }; class MyClass { public: // TODO:…
J B
  • 311
  • 4
  • 12
4
votes
2 answers

Pimpl with smart pointers in a class with a template constructor: weird incomplete type issue

When using smart pointers with the pImpl idiom, as in struct Foo { private: struct Impl; boost::scoped_ptr pImpl; }; the obvious problem is that Foo::Impl is incomplete at the point where the destructor of Foo is generated.…
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
4
votes
0 answers

Minimizing the amount of header files needed using the Builder/Fluent pattern

I am experimenting with the Builder/Fluent style of creating objects trying to extend some ideas presented in a course. One element I immediately didn't like with my test implementation was the large number of additional header files the client…
ajgio23
  • 55
  • 5