238

我正在使用 pimpl-idiom std::unique_ptr

class window {
  window(const rectangle& rect);

private:
  class window_impl; // defined elsewhere
  std::unique_ptr<window_impl> impl_; // won't compile
};

但是,我在第 304 行中收到关于使用不完整类型的编译错误<memory>

sizeof' ' 对不完整类型 ' uixx::window::window_impl'的无效应用

据我所知,std::unique_ptr应该可以与不完整的类型一起使用。这是 libc++ 中的错误还是我在这里做错了什么?

4

7 回答 7

312

以下是一些std::unique_ptr不完整类型的示例。问题在于破坏。

如果你使用 pimpl with unique_ptr,你需要声明一个析构函数:

class foo
{ 
    class impl;
    std::unique_ptr<impl> impl_;

public:
    foo(); // You may need a def. constructor to be defined elsewhere

    ~foo(); // Implement (with {}, or with = default;) where impl is complete
};

因为否则编译器会生成一个默认的,它需要一个完整的声明foo::impl

如果你有模板构造函数,那么即使你不构造impl_成员,你也会被搞砸:

template <typename T>
foo::foo(T bar) 
{
    // Here the compiler needs to know how to
    // destroy impl_ in case an exception is
    // thrown !
}

在命名空间范围内, usingunique_ptr也不起作用:

class impl;
std::unique_ptr<impl> impl_;

因为编译器必须知道如何销毁这个静态持续时间对象。一种解决方法是:

class impl;
struct ptr_impl : std::unique_ptr<impl>
{
    ~ptr_impl(); // Implement (empty body) elsewhere
} impl_;
于 2012-03-31T09:08:23.727 回答
62

正如Alexandre C.所提到的,问题归结为window' 的析构函数被隐式定义在类型window_impl仍然不完整的地方。除了他的解决方案之外,我使用的另一个解决方法是在标头中声明一个 Deleter 函子:

// Foo.h

class FooImpl;
struct FooImplDeleter
{
  void operator()(FooImpl *p);
};

class Foo
{
...
private:
  std::unique_ptr<FooImpl, FooImplDeleter> impl_;
};

// Foo.cpp

...
void FooImplDeleter::operator()(FooImpl *p)
{
  delete p;
}

请注意,使用自定义 Deleter 函数会排除使用std::make_unique(可从 C++14 获得),如已在此处讨论的那样。

于 2013-07-30T14:56:15.250 回答
28

使用自定义删除器

问题是unique_ptr<T>必须T::~T()在自己的析构函数、移动赋值运算符和unique_ptr::reset()成员函数中调用析构函数(仅)。但是,必须在几个 PIMPL 情况下(已经在外部类的析构函数和移动赋值运算符中)调用(隐式或显式)这些。

正如在另一个答案中已经指出的那样,避免这种情况的一种方法是将所有需要unique_ptr::~unique_ptr(),unique_ptr::operator=(unique_ptr&&)和的操作移动unique_ptr::reset()到实际定义 pimpl 帮助器类的源文件中。

然而,这相当不方便,并且在某种程度上违背了 pimpl idoim 的观点。一个更简洁的解决方案可以避免使用自定义删除器,并且只将其定义移动到 pimple helper 类所在的源文件中。这是一个简单的例子:

// file.h
class foo
{
    struct pimpl;
    struct pimpl_deleter { void operator()(pimpl*) const; };
    std::unique_ptr<pimpl,pimpl_deleter> m_pimpl;
  public:
    foo(some data);
    foo(foo&&) = default;             // no need to define this in file.cc
    foo&operator=(foo&&) = default;   // no need to define this in file.cc
  //foo::~foo()          auto-generated: no need to define this in file.cc
};

// file.cc
struct foo::pimpl
{
  // lots of complicated code
};
void foo::pimpl_deleter::operator()(foo::pimpl*ptr) const { delete ptr; }

除了单独的删除器类,您还可以将自由函数或static成员foo与 lambda 结合使用:

class foo {
    struct pimpl;
    struct deleter {
        operator()(pimpl*) const;
    };
    std::unique_ptr<pimpl,deleter> m_pimpl;
};
于 2015-08-28T10:52:53.100 回答
17

可能您在使用不完整类型的类中的 .h 文件中有一些函数体。

确保在类窗口的 .h 中只有函数声明。window 的所有函数体都必须在 .cpp 文件中。对于 window_impl 也是如此......

顺便说一句,您必须在 .h 文件中显式添加 windows 类的析构函数声明。

但是您不能将空的 dtor 正文放入头文件中:

class window {
    virtual ~window() {};
  }

必须只是一个声明:

  class window {
    virtual ~window();
  }
于 2014-04-12T15:56:19.280 回答
3

为了添加其他关于自定义删除器的回复,在我们的内部“实用程序库”中,我添加了一个帮助头来实现这种常见模式(std::unique_ptr类型不完整,只有某些 TU 知道,例如避免长编译时间或提供只是对客户的不透明句柄)。

它为此模式提供了通用的脚手架:调用外部定义的删除器函数的自定义删除器类,unique_ptr具有此删除器类的 a 的类型别名,以及用于在具有完整定义的 TU 中声明删除器函数的宏类型。我认为这有一些普遍的用处,所以这里是:

#ifndef CZU_UNIQUE_OPAQUE_HPP
#define CZU_UNIQUE_OPAQUE_HPP
#include <memory>

/**
    Helper to define a `std::unique_ptr` that works just with a forward
    declaration

    The "regular" `std::unique_ptr<T>` requires the full definition of `T` to be
    available, as it has to emit calls to `delete` in every TU that may use it.

    A workaround to this problem is to have a `std::unique_ptr` with a custom
    deleter, which is defined in a TU that knows the full definition of `T`.

    This header standardizes and generalizes this trick. The usage is quite
    simple:

    - everywhere you would have used `std::unique_ptr<T>`, use
      `czu::unique_opaque<T>`; it will work just fine with `T` being a forward
      declaration;
    - in a TU that knows the full definition of `T`, at top level invoke the
      macro `CZU_DEFINE_OPAQUE_DELETER`; it will define the custom deleter used
      by `czu::unique_opaque<T>`
*/

namespace czu {
template<typename T>
struct opaque_deleter {
    void operator()(T *it) {
        void opaque_deleter_hook(T *);
        opaque_deleter_hook(it);
    }
};

template<typename T>
using unique_opaque = std::unique_ptr<T, opaque_deleter<T>>;
}

/// Call at top level in a C++ file to enable type %T to be used in an %unique_opaque<T>
#define CZU_DEFINE_OPAQUE_DELETER(T) namespace czu { void opaque_deleter_hook(T *it) { delete it; } }

#endif
于 2018-02-13T14:19:10.157 回答
2

可能不是最佳解决方案,但有时您可以使用shared_ptr代替。当然,这有点矫枉过正,但是……至于 unique_ptr,我可能还要再等 10 年,直到 C++ 标准制定者决定使用 lambda 作为删除器。

另一边。根据您的代码,可能会发生,在销毁阶段 window_impl 将不完整。这可能是未定义行为的原因。看到这个: 为什么,真的,删除一个不完整的类型是未定义的行为?

所以,如果可能的话,我会用虚拟析构函数为你的所有对象定义一个非常基础的对象。而且你几乎很好。您只需要记住,系统将为您的指针调用虚拟析构函数,因此您应该为每个祖先定义它。您还应该将继承部分中的基类定义为虚拟(有关详细信息,请参阅this)。

于 2018-11-30T08:01:17.847 回答
-1

使用extern template

使用std::unique_ptr<T>whereT是不完整类型的问题是unique_ptr需要能够删除T各种操作的实例。该类unique_ptr用于std::default_delete<T>删除实例。因此,在理想世界中,我们只需

extern template class std::default_delete<T>;

以防止std::default_delete<T>被实例化。然后,声明

template class std::default_delete<T>;

在完成的地方T实例化模板。

这里的问题是default_delete实际上定义了不会被实例化的内联方法。所以,这个想法行不通。但是,我们可以解决这个问题。

首先,让我们定义一个不内联调用运算符的删除器。

/* --- opaque_ptr.hpp ------------------------------------------------------- */
#ifndef OPAQUE_PTR_HPP_
#define OPAQUE_PTR_HPP_

#include <memory>

template <typename T>
class opaque_delete {
public:
  void operator() (T* ptr);
};

// Do not move this method into opaque_delete, or it will be inlined!
template <typename T>
void opaque_delete<T>::operator() (T* ptr) {
  std::default_delete<T>()(ptr);
}

此外,为了便于使用,定义一个opaque_ptr与 结合unique_ptr的类型opaque_delete,并且类似于std::make_unique,我们定义make_opaque

/* --- opaque_ptr.hpp cont. ------------------------------------------------- */
template <typename T>
using opaque_ptr = std::unique_ptr<T, opaque_delete<T>>;

template<typename T, typename... Args>
inline opaque_ptr<T> make_opaque(Args&&... args)
{
  return opaque_ptr<T>(new T(std::forward<Args>(args)...));
}

#endif

该类型opaque_delete现在可以与extern template构造一起使用。这是一个例子。

/* --- foo.hpp -------------------------------------------------------------- */
#ifndef FOO_HPP_
#define FOO_HPP_

#include "opaque_ptr.hpp"

class Foo {
public:
  Foo(int n);
  void print();
private:
  struct Impl;
  opaque_ptr<Impl> m_ptr;
};

// Do not instantiate opaque_delete.
extern template class opaque_delete<Foo::Impl>;

#endif

由于我们阻止opaque_delete实例化此代码,因此编译时不会出错。为了让链接器满意,我们opaque_delete在我们的foo.cpp.

/* --- foo.cpp -------------------------------------------------------------- */

#include "foo.hpp"
#include <iostream>

struct Foo::Impl {
  int n;
};

// Force instantiation of opaque_delete.
template class opaque_delete<Foo::Impl>;

其余方法可以如下实现。

/* --- foo.cpp cont. -------------------------------------------------------- */
Foo::Foo(int n)
  : m_ptr(new Impl)
{
  m_ptr->n = n;
}

void Foo::print() {
  std::cout << "n = " << m_ptr->n << std::endl;
}

这种解决方案的优点是,一旦opaque_delete定义,所需的样板代码就相当小。

于 2021-08-05T17:42:37.720 回答