gpt4 book ai didi

c++ - Pimpl 习惯用法作为模板基类

转载 作者:行者123 更新时间:2023-12-02 12:15:39 30 4
gpt4 key购买 nike

我目前正在研究pimpl idiom,并且有非常好的教程介绍如何实现它(例如 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() 中的删除运算符不可见。

有人发现这种方法有任何问题吗?:-)

<小时/>

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

最佳答案

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

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

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

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

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

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

现在你的 header 可以干净地编译了。只要析构函数为 Object未内联定义。当它是内联时,编译器必须实例化模板的析构函数,无论 object.h已包含在内。

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

进一步的改进想法:

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

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

关于c++ - Pimpl 习惯用法作为模板基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43913849/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com