Pimpl成语作为模板基类

时间:2017-05-11 11:16:46

标签: 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

然后可以在你喜欢的任何课程中使用pimpl:

#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()中的delete运算符不可见。

有没有人看到这种方法存在任何问题?: - )

所以现在有一个 final version 基于下面关于GitHub的讨论(非常感谢 StoryTeller )。 repository还包含一个简单的用法示例。

3 个答案:

答案 0 :(得分:4)

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

  1. 你的课程本质上是一个混音。它不是关于动态多态,所以没有人会在指向Pimpl<ObjectPrivate>的指针上调用delete。删除虚拟析构函数。它引入了永远不需要的开销。你想要的只是静态多态。

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

  3. 您的赋值运算符不是异常安全的。如果T的构造函数抛出,则会丢失先前保存的数据。在这种情况下,IMO最好使用copy and swap成语。

  4. (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;
    }
    

    Live Example

    现在你的标题可以干净地编译。只要Object的析构函数没有内联定义。当它是内联的时,编译器必须在包含object.h的地方实例化模板的析构函数。

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

    进一步改进的想法:

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

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

答案 1 :(得分:2)

  

但我从未见过将它作为基本模板类实现

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

  

有没有人看到这种方法存在任何问题?

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

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

我不知道弗拉德的图书馆是否有同样的问题。

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

答案 2 :(得分:0)

我正在使用的现代版本:


///////////////////////////////
// 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)) {}