Skip to content

Instantly share code, notes, and snippets.

@sehe
Created November 20, 2011 00:04
Show Gist options
  • Save sehe/1379569 to your computer and use it in GitHub Desktop.
Save sehe/1379569 to your computer and use it in GitHub Desktop.
#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();}
#include "pimpl.h"
struct A : public pimpl<A>
{
A();
A(const A& other);
A(A&& other);
virtual ~A();
void foo();
};
#include "A.h"
int main()
{
A a;
}
test: *.cpp | *.h
g++ -std=c++0x -O0 -g $^ -o $@
#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);
};
#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