痘痘成语作为模板基类

Pimpl idiom as template base class

本文关键字:基类 成语      更新时间:2023-10-16

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

#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

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

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

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


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

是的,在我看来,有几个问题。

  1. 你的类本质上是一个混合的。这与动态多态性无关,因此没有人会在指向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;
}

现场示例

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

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

进一步改进的想法:

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

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

但我

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

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

有没有人看到这种方法有什么问题?

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

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

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

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

我正在使用的现代版本:


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