Pimpl成语作为模板基类

0x2*_*648 2 c++ templates pimpl-idiom

我目前正在研究pimpl习惯用法,并且有很好的教程介绍如何实现它(例如here)。但我从未见过将其实现为这样的基本模板类:

#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
Run Code Online (Sandbox Code Playgroud)

然后可以将其用于您想要插入的任何类:

#ifndef OBJECT_H
#define OBJECT_H

#include "pimpl.h"

class ObjectPrivate;

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

    /* ... */
};

#endif // OBJECT_H
Run Code Online (Sandbox Code Playgroud)

当前,我在一个小的示例项目(作为共享库构建)中使用它,而我唯一的问题是MSVC警告缺少ObjectPrivate的析构函数(请参阅C4150)。只会出现此警告,因为ObjectPrivate是预先声明的,因此在编译时对Pimpl ::〜Pimpl()中的delete操作符不可见。

有人看到这种方法有什么问题吗?:-)


因此,现在有一个基于GitHub上以下讨论的最终版本(非常感谢StoryTeller)。该存储库还包含一个简单的用法示例。

Sto*_*ica 5

是的,正如我所看到的,有几个问题。

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

  2. 您使用分配对象,new然后使用释放对象delete。我不会使用您的模板,因为该分配方案并不总是适合我的应用程序。您必须提供一种自定义分配方案的方法,以使您的类真正有用。

  3. 您的赋值运算符并非异常安全。如果构造函数T抛出异常,则会丢失先前保存的数据。IMO在这种情况下最好使用复制和交换习惯用法。

(1)和(2)的解决方案是添加更多模板参数,其中第一个用于CRTP。这将允许您将不知道如何执行的操作推送到继承mixin的类上。它可以通过定义自己的覆盖它们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;
}
Run Code Online (Sandbox Code Playgroud)

Live Example

现在您的头文件可以干净地编译了。只要Object没有内联定义for的析构函数。object.h在内联时,编译器必须实例化模板的析构函数,无论其包含在何处。

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

进一步改进的想法:

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

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