用最少量的代码实现pImpl

Tro*_*nic 10 c++ boost pimpl-idiom

可以使用哪种技巧来最小化实现pImpl类的工作量?

标题:

class Foo {
    struct Impl;
    boost::scoped_ptr<Impl> self;
public:
    Foo(int arg);
    ~Foo();
    // Public member functions go here
};
Run Code Online (Sandbox Code Playgroud)

执行:

struct Foo::Impl {
    Impl(int arg): something(arg) {}
    // All data members and private functions go here
};

Foo::Foo(int arg): self(new Impl(arg)) {}
Foo::~Foo() {}

// Foo's public functions go here (and they refer to data as self->something)
Run Code Online (Sandbox Code Playgroud)

您如何使用Boost,可能继承,CRTP或其他技巧来避免尽可能多的样板代码?运行时性能不是问题.

ami*_*mit 5

Loki实施pimpl 可能是一个很好的答案.另见DDJ文章.