Created
November 20, 2011 00:04
-
-
Save sehe/1379569 to your computer and use it in GitHub Desktop.
ronag pimpl helper http://stackoverflow.com/questions/8198236/pimpl-helper-ambiguous-with-inheritence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pimpl_impl.h" | |
#include "A.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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pimpl.h" | |
struct A : public pimpl<A> | |
{ | |
A(); | |
A(const A& other); | |
A(A&& other); | |
virtual ~A(); | |
void foo(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "A.h" | |
int main() | |
{ | |
A a; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test: *.cpp | *.h | |
g++ -std=c++0x -O0 -g $^ -o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <memory> | |
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pimpl.h" | |
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_); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment