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

using pimpl pattern in templated class. How to allocate a unique_ptr

My colleague insists our project should be able to use a template like this: template class Foo { public: Foo(T t, B b); // some more functions private: struct pimpl; std::unique_ptr m_impl; }; I'm…
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
0
votes
0 answers

How to use pimpl idiom with templated functions

I have a class which has a template member functions and the private members needs to be put in the implementation class by pimpl idiom approach. Foo.hpp #include class Foo { public: private: class FooImpl; …
0
votes
1 answer

looking for a design pattern for complex numbers class in c++

I'm using complex numbers for learning design patterns. I'm currently using an abstract class: namespace abstract{ class complex{ public: virtual ~complex() = 0; virtual double re() const = 0; // some…
0
votes
1 answer

How can I offload dependency injected template class providing templated functions to pimpl class

I have an application class that can take in a dependent class as a template argument to the constructor. This dependent class is required to provide certain templated functions that the application class can call. I would like to offload this…
atab
  • 1
  • 2
0
votes
0 answers

How to get rid of these static_casts?

I'm trying to create a C++ class hierarchy of UI "view" classes that wrap platform-specific UI classes. My classes use the pimpl idiom to hide the implementation from the header file. The Impl structs contain another pointer, to classes in the…
Rob N
  • 15,024
  • 17
  • 92
  • 165
0
votes
0 answers

How to provide an opaque public handle in public API while still able to touch the implementation detail inside internal component?

I am refactoring a biometric recognition SDK, which public API provide feature extraction and some CRUD feature management interface like: class PublicComponent{ public: FeaturePublic extract_feature(); int add_feature(const FeaturePublic&); …
user8510613
  • 1,242
  • 9
  • 27
0
votes
3 answers

About the pimpl syntax

I have a question about the C++ usage used in the pimpl syntax. First, why is it not necessary to write pimpl( new impl ) as pimpl( new my_class::impl ) Second, why is the lifetime of new impl extended even though it is a temporary…
gahhu
  • 31
  • 3
0
votes
2 answers

PIMPL, POD, visibility of the implementation class, will its destructor get called?

Wikipedia claims, in the article on opaque pointers, that The d-pointer is the only private data member of the class and points to an instance of a struct (which must be a POD since its destructor is not visible) This is not required in PIMPL and…
John
  • 6,433
  • 7
  • 47
  • 82
0
votes
1 answer

C++ abi compatability without pimpl using abstract class

Suppose I have a class B_Impl which inherits and implements a pure abstract class B (not containing any data-fields). Suppose class A uses B_Impl via B* only. If I add a field to B_Impl.h (clearly, not included by A), will ABI compatability be…
JenyaKh
  • 2,040
  • 17
  • 25
0
votes
1 answer

Downcasting an implementation using an interface template

I'm implementing a generic interface that can bridge different implementations of some base class. The interface utility is written as follows: // InterfaceUtils.h // // Base object class class IBaseObject { virtual ~IBaseObject() =…
Martin
  • 141
  • 1
  • 11
0
votes
1 answer

Alternative to Pimpl

I am required to provide a solution to the following problem: A class is published as a library and made available to the world. It is designed in a way which does not use the pimpl approach. Two new data members need to be defined in this class.…
Mariah
  • 101
  • 1
  • 7
0
votes
0 answers

How to call member functions of an IMPL from an injected strategy pattern

So I have been tasked with implementing a strategy pattern on an existing code base. The code below is a very simplified version of the code that I am using for demonstration purposes. I don't have a lot of leeway to re-architect the WorldTraversal…
0
votes
1 answer

move operation with pimpl idiom

In the following code I am attempting to use a move assignment within the PIMPL idiom, but the code does not compile. struct.hpp: #pragma once #include struct A { std::unique_ptr m_x; A(int x); …
francesco
  • 7,189
  • 7
  • 22
  • 49
0
votes
1 answer

Storing a class that uses the PIMPL idiom in a std::vector

I am writing an application that needs to store objects of a class that uses the PIMPL idiom in a std::vector. Because the class uses std::unique_ptr to store a pointer to it's implementation and std::unique_ptr is not copyable, the class itself is…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
0
votes
1 answer

Finding a way to use PIMPL with external constant definitions for the size of an array inside the implementation

We have the following situation: We are using a processor which has defined sections inside its RAM, which must be used by a special implementation using the PIMPL-Principle. For example the private Implementation would look like this: Impl(…
NetoBF
  • 127
  • 6