Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:44:07+00:00 2026-05-26T21:44:07+00:00

I’m playing around with creating a utility class for the pimpl idiom, however I

  • 0

I’m playing around with creating a utility class for the pimpl idiom, however I have some problem I hoped to get some help with:

This is what I’ve got:
[sehe: see also rev.1 here: https://gist.github.com/1379569/9f6cca5703e6195da65e34103393d901dde3b1bf]

pimpl.h

template<typename T>
class pimpl
{
    template <typename> friend class pimpl;

    pimpl(const pimpl& other);
    pimpl(pimpl&& other);
    pimpl& operator=(const pimpl& other);
    pimpl& operator=(pimpl&& other);    
protected:

    ~pimpl();

    struct implementation;
    std::unique_ptr<implementation> impl_;
public:
    pimpl();

    template<typename P0>
    pimpl(P0&& p0);

    pimpl(const T& other);
    pimpl(T&& other);

    void swap(T& other);
};

pimpl_impl.h

// #include "pimpl.h" // TODO add header guards...

template<typename T>
pimpl<T>::pimpl() : impl_(new implementation()){}

template<typename T>
template<typename P0>
pimpl<T>::pimpl(P0&& p0) : impl_(new implementation(std::forward<P0>(p0))){}

template<typename T>
pimpl<T>::~pimpl(){}

template<typename T>
pimpl<T>::pimpl(const T& other) : impl_(new implementation(*((pimpl&)other).impl_)){}

template<typename T>
pimpl<T>::pimpl(T&& other) : impl_(std::move(((pimpl&)other).impl_)){}

template<typename T>
void pimpl<T>::swap(T& other)
{
    impl_.swap(other.impl_);
}

And this is how you use it:

A.h

#include "pimpl.h"

class A : public pimpl<A>
{
    A();
    A(const A& other);
    A(A&& other);
    virtual ~A();
    void foo();
};

A.cpp

#include "A.h"
#include "pimpl_impl.h"

template<>
struct pimpl<A>::implementation
{
     void foo()
     {
     }
};

A::A() : pimpl(){}
A::A(const A& other) : pimpl(other){}
A::A(A&& other) : pimpl(std::move(other)){}
A::~A(){}
void A::foo(){impl_->foo();}

This works quite well, however it quickly breaks down when you put inheritance into the picture:

class B : public pimpl<B>, public A
{
    ...
};

B::B() : pimpl() {} // pimpl ambiguous

pimpl has become ambiguous…

Any ideas as to how to avoid this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:44:07+00:00Added an answer on May 26, 2026 at 9:44 pm

    While I wouldn’t recommend stuffing pimpl into a curious base class because of Liskov, I don’t see any real problem to make it work;

    Like @Nim suggested: it all seems to pan out nicely when doing

    class B : public pimpl<B>, public A
    {
        B() : pimpl<B>() {} 
    };
    

    Gist updated with full sample code that works with B:

    • https://gist.github.com/1379569
    • See it live on http://ideone.com/te49B

    B.h

    #include "A.h"
    
    class B : public pimpl<B>, public A
    {
      public:
        B();
        B(const B& other);
        B(B&& other);
        virtual ~B();
        void bar();
    };
    

    B.cpp

    #include "pimpl_impl.h"
    #include "B.h"
    
    template<>
    struct pimpl<B>::implementation
    {
         void bar()
         {
         }
    };
    
    B::B() : pimpl<B>(){}
    B::B(const B& other) : pimpl<B>(other){}
    B::B(B&& other) : pimpl<B>(std::move(other)){}
    B::~B(){}
    void B::bar(){ pimpl<B>::impl_->bar();}
    

    main.cpp:

    #include "B.h"
    
    int main()
    {
        A a;
        B b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.