pmj*_*bin 1 c++ unique-ptr c++11
我试图使用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;
}
Run Code Online (Sandbox Code Playgroud)
当我使用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!=’
Run Code Online (Sandbox Code Playgroud)
该~unique_ptr程序的代码如下:
// Destructor.
~unique_ptr() noexcept
{
auto& __ptr = std::get<0>(_M_t);
if (__ptr != nullptr)
get_deleter()(__ptr);
__ptr = pointer();
}
Run Code Online (Sandbox Code Playgroud)
根据我的说法,对nullptr的检查没有意义,因为原始指针类型是int(并且不是int*由于覆盖它HandleDeleter).奇怪的是,此代码在GCC 4.6.1下编译时没有错误.执行时,示例显示"1已被删除!" 正如所料.
我想知道是否有任何细节我忽略或者它是否真的是GCC的unique_ptr的STL实现中的一个错误.
谢谢,
PMJ
正如我在评论中所说,如果你传递的删除类型有一个嵌套的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并且b表示类型(可能const)的值P,并且np表示类型的值(可能const)std::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
Run Code Online (Sandbox Code Playgroud)
现在,最简单的解决方案是将您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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1706 次 |
| 最近记录: |