1

我正在尝试使用std::unique_ptr以将整数句柄存储到一些不透明的对象。为此,我定义了一个自定义删除器类型,typedef int pointer以便将原始指针类型覆盖为int而不是int*. 此过程在本网站的最后一节中进行了描述:http: //asawicki.info/news_1494_unique_ptr_in_visual_c_2010.html

这是一些示例代码,可以更好地说明我正在尝试做的事情:

#include <memory>
#include <iostream>

static void close(int p)
{
    std::cout << p << " has been deleted!" << std::endl;
}

struct handle_deleter
{
    typedef int pointer;
    void operator()(pointer p) { close(p); }
};

typedef std::unique_ptr< int, handle_deleter> unique_handle;

int main(int argc, char *argv[])
{
    unique_handle handle(1);

    return 0;
}

当我使用 GCC 4.7.2 编译此代码时,我收到以下错误:

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/memory:86:0,
                 from unique_ptr_test.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/unique_ptr.h: In instantiation of ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = int; _Dp = handle_deleter]’:
unique_ptr_test.cpp:19:23:   required from here
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/unique_ptr.h:172:2: error: invalid operands of types ‘int’ and ‘std::nullptr_t’ to binary ‘operator!=’

~unique_ptr过程的代码如下:

// Destructor.
~unique_ptr() noexcept
{
    auto& __ptr = std::get<0>(_M_t);
    if (__ptr != nullptr)
      get_deleter()(__ptr);
    __ptr = pointer();
}

据我说,对 nullptr 的检查没有意义,因为原始指针类型是int(而不是int*由于在 中覆盖它HandleDeleter)。奇怪的是,这段代码在 GCC 4.6.1 下编译没有错误。执行后,示例显示“1 已被删除!” 正如预期的那样。

我想知道是否有任何我忽略的细节,或者它是否真的是 GCC 的 unique_ptr 的 STL 实现中的错误。

谢谢,

PMJ

4

1 回答 1

7

正如我在评论中所说,如果您传递的删除器类型具有嵌套的pointertypedef,它必须满足NullablePointer 要求:

20.7.1.2 [unique.ptr.single] p3

如果类型remove_reference<D>::type::pointer存在,unique_ptr<T, D>::pointer则应是 的同义词remove_reference<D>::type::pointer。否则unique_ptr<T, D>::pointer应为 的同义词T*型式unique_ptr<T, D>::pointer应满足NullablePointer(17.6.3.3)的要求。

然后§17.6.3.3列出类型必须满足的所有要求才能成为 NullablePointer。表中列出了某些语义,其中:

u表示标识符,t表示类型的非常量左值P,表示类型的值(可能)a,表示类型的值(可能)。bconstPnpconststd::nullptr_t

Expression    Return type                         Operational semantics
P u(np);                                          post: u == nullptr
P u = np;
P(np)                                             post: P(np) == nullptr
t = np        P&                                  post: t == nullptr
a != b        contextually convertible to bool    !(a == b)
a == np       contextually convertible to bool    a == P()
np == a
a != np       contextually convertible to bool    !(a == np)
np != a

现在,最简单的解决方案是将您包装int在提供这些语义的类型中:

#include <cstddef> // std::nullptr_t

struct handle{
  handle() : value(0){}
  handle(std::nullptr_t) : value(0){}
  /*explicit*/ handle(int v) : value(v){} // make it explicit if you need it
  // special members can be generated

  handle& operator=(std::nullptr_t){ value = 0; return *this; }

  // contextual conversion to bool
  explicit operator bool() const{ return value != 0; }

  int value;
};

bool operator==(handle lhs, handle rhs){ return lhs.value == rhs.value; }
bool operator!=(handle lhs, handle rhs){ return lhs.value != rhs.value; }
// comparision against 'nullptr' is handled by the above operators
// since 'nullptr' can be implicitly converted to 'handle'
于 2012-12-28T10:05:22.723 回答