Pimpl 习语作为模板基础 class

Pimpl idiom as template base class

我目前正在研究 pimpl 习语,并且有非常好的教程如何实现它(例如 here)。但我从来没有见过它作为基本模板实现的 class 是这样的:

#ifndef PIMPL_H
#define PIMPL_H

template <class T>
class Pimpl
{
public:
    explicit Pimpl();
    explicit Pimpl(T *ptr);
    virtual ~Pimpl() = 0;

    Pimpl(const Pimpl<T> &other);
    Pimpl &operator=(const Pimpl<T> &other);

protected:
    T *d_ptr;
};

template<class T>
Pimpl<T>::Pimpl() : d_ptr(new T)
{

}

template<class T>
Pimpl<T>::Pimpl(T *ptr) : d_ptr(ptr)
{

}

template<class T>
Pimpl<T>::~Pimpl()
{
    delete d_ptr;
    d_ptr = 0;
}

template<class T>
Pimpl<T>::Pimpl(const Pimpl<T> &other) : d_ptr(new T(*other.d_ptr))
{

}

template<class T>
Pimpl<T> &Pimpl<T>::operator=(const Pimpl<T> &other)
{
    if (this != &other) {
        delete d_ptr;
        d_ptr = new T(*other.d_ptr);
    }

    return *this;
}

#endif // PIMPL_H

然后可以在您喜欢的任何 class 中使用:

#ifndef OBJECT_H
#define OBJECT_H

#include "pimpl.h"

class ObjectPrivate;

class Object : public Pimpl<ObjectPrivate>
{
public:
    Object();
    virtual ~Object();

    /* ... */
};

#endif // OBJECT_H

目前我在一个小示例项目(构建为共享库)中使用它,我遇到的唯一问题是 MSVC 警告 ObjectPrivate 缺少析构函数(参见 C4150).此警告仅发生,因为 ObjectPrivate 是前向声明的,因此在编译时 Pimpl::~Pimpl() 中的删除运算符不可见。

有人发现这种方法有什么问题吗? :-)


所以现在有一个final version based on the discussion below on GitHub (big thanks to StoryTeller). The repository也包含一个简单的用法示例

But i have never seen it implemented as a base template class

弗拉基米尔·巴托夫做到了:https://github.com/yet-another-user/pimpl

Does anyone see any sort of problems with this approach?

您需要认真对待警告。如果你的 ObjectPrivate 实际上有一个非平凡的析构函数(就像包含一个 std::string 成员一样简单),你有未定义的行为,析构函数可能不会被调用。

这通常表明由于某种原因,析构函数在错误的位置实例化。确保派生的 class 的所有 all 构造函数和析构函数的定义都放在 ObjectPrivate 的完整定义之后。这包括隐式复制和移动构造函数,它们可能是触发示例代码中警告的原因。是的,这意味着您必须显式声明这些特殊函数(因此,如果需要,还可以声明复制和移动赋值运算符),但至少您可以使用默认定义。

不知道Vlad的图书馆有没有同样的问题

此外,在析构函数中取消指针是没有意义的,可能会被一些现代编译器优化掉。

是的,有几个问题,据我所知。

  1. 你的 class 本质上是一个 mixin。这与动态多态性无关,因此没有人会在指向 Pimpl<ObjectPrivate> 的指针上调用 delete。删除虚拟析构函数。它引入了永远不需要的开销。你想要的只是静态多态。

  2. 您使用 new 分配对象并使用 delete 释放。我不会使用您的模板,因为该分配方案并不总是适合我的应用程序。为了让你的class真正有用

  3. ,你必须给出一个自定义分配方案的方法
  4. 您的赋值运算符不是异常安全的。如果 T 的构造函数抛出异常,您将丢失之前保存的数据。 IMO 在这种情况下最好使用 copy and swap 习语。

(1)和(2)的解决方法是增加更多的模板参数,其中第一个是CRTP。这将允许您将您不知道如何执行的操作推送到继承您的 mixin 的 class 上。它可以通过定义自己的 makeunmakeclone 来覆盖它们。这些都将被静态绑定。

template <class Handle, class Impl>
class Pimpl
{
private:
    Impl* _make() const
    { return ((Handle const*)this)->make(); }

    void _unmake(Impl *p) const
    { ((Handle const*)this)->unmake(p); }

    Impl* _clone(Impl *p) const
    { return ((Handle const*)this)->clone(p); }

    void swap(Pimpl &other) {
        Impl *temp = d_ptr;
        d_ptr = other.d_ptr;
        other.d_ptr = temp;
    }

public:
    explicit Pimpl();
            ~Pimpl();

    Pimpl(const Pimpl &other);
    Pimpl &operator=(const Pimpl &other);

    // fall-backs
    static Impl* make()          { return new Impl; }
    static void  unmake(Impl* p) { delete p; }
    static Impl* clone(Impl* p)  { return new Impl(*p); }

protected:

    Impl *d_ptr;
};

template<class Handle, class Impl>
Pimpl<Handle, Impl>::Pimpl() :
  d_ptr(_make())
{

}

template<class Handle, class Impl>
Pimpl<Handle, Impl>::~Pimpl()
{
    _unmake(d_ptr);
    d_ptr = 0;
}

template<class Handle, class Impl>
Pimpl<Handle, Impl>::Pimpl(const Pimpl &other) :
  d_ptr(_clone(other.d_ptr))
{

}

template<class Handle, class Impl>
Pimpl<Handle, Impl> &Pimpl<Handle, Impl>::operator=(const Pimpl &other)
{
    Pimpl copy(other);
    swap(copy);

    return *this;
}

Live Example

现在你的头文件可以干净地编译了。只要 Object 的析构函数不是内联定义的。当它内联时,编译器必须在包含 object.h 的任何地方实例化模板的析构函数。

如果是在cpp文件中定义,定义在ObjectPrivate之后,那么~Pimpl的实例化会看到私有部分的完整定义。

进一步的改进想法:

  1. 保护特殊成员。毕竟,只有派生的 Handle class 才应该调用它们。

  2. 添加对移动语义的支持。

我正在使用的现代版本:


///////////////////////////////
// Header File
template <typename impl_t>
class Pimpl {
  public:
    Pimpl() = default;
    virtual ~Pimpl() = default;
    Pimpl(std::shared_ptr<impl_t> handle) : handle(handle) {}

    std::shared_ptr<impl_t>
    get_handle() const {
      return handle;
    }
  protected:
    std::shared_ptr<impl_t> handle;
};

class object_impl;
class object : public Pimpl<object_impl> {
/* whatever constructors you want*/
public:
  object(int x);
}

///////////////////////////////
// Cpp File
class object_impl {
public:
  object_impl(int x) : x_(x) {}
private:
  int x_;
}

object::object(int x) : Pimpl(std::make_shared<object_impl>(x)) {}