对于Windows HANDLE,使用std :: unique_ptr [英] Using std::unique_ptr for Windows HANDLEs

查看:445
本文介绍了对于Windows HANDLE,使用std :: unique_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用std :: unique_ptrs以异常安全的方式管理Windows HANDLE。



首先我尝试:

  struct HandleDeleter 
{
void operator()(HANDLE handle)
{
if(handle)
{
FindVolumeClose(handle)
}
}
}
typedef std :: unique_ptr< HANDLE,HandleDeleter> unique_vol_handle_t;

后来在我的代码中,当我尝试使用它:



unique_vol_handle_t volH(FindFirstVolumeW(buffer,MAX_GUID_PATH));



Visual Studio 2012RC:

  1>错误C2664:'std :: unique_ptr <_Ty,_Dx> :: unique_ptr(std :: nullptr_t)throw()':无法将参数1从'HANDLE'转换为'std :: nullptr_t'
1& with
1> [
1> _Ty = HANDLE,
1> _Dx = VolumeHandleDeleter
1> ]
1> nullptr只能转换为引用上述volH声明行的指针或句柄类型



搜索一段时间后,我发现博客文章基本上说,添加:



typedef HANDLE pointer; / code>



到结构声明的顶部,一切都会很好。



我不相信,但我试过它,它确实解决了错误。我很困惑如何定义一个类型(没有引用它)可能会产生这样的区别。



两个问题:



1)您能解释原始错误吗?我不明白为什么编译器引用 std :: nullptr_t / nullptr



2) typedef解决这个(或至少看起来)?

解决方案

unique_ptr的实现检查删除器上是否存在 :: pointer 类型。如果删除器有一个 :: pointer 类型,那么这个类型被用作指针 typedef在 unique_ptr



根据cppreference.com, unique_ptr ::指针类型定义为


std :: remove_reference< D> :: type :: pointer 如果该类型存在,否则 T *



I am attempting to use std::unique_ptrs to manage Windows HANDLEs in an exception-safe manner.

First I tried:

struct HandleDeleter
{
    void operator()( HANDLE handle )
    {
        if( handle )
        {
            FindVolumeClose( handle )
        }
    }
}
typedef std::unique_ptr< HANDLE, HandleDeleter > unique_vol_handle_t;

Later in my code when I try to use it:

unique_vol_handle_t volH( FindFirstVolumeW( buffer, MAX_GUID_PATH ) );

I get the following error from Visual Studio 2012RC:

1>          error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'HANDLE' to 'std::nullptr_t'
1>          with
1>          [
1>              _Ty=HANDLE,
1>              _Dx=VolumeHandleDeleter
1>          ]
1>          nullptr can only be converted to pointer or handle types

referencing the volH declaration line, immediately above.

After searching for some time, I found a blog article which basically says, add:

typedef HANDLE pointer;

to the top of the struct declaration, and all will be well.

I didn't believe it, but I tried it and it did resolve the error. I'm puzzled how defining a type (without even referencing it) could make such a difference.

Two questions:

1) Can you explain the original error? I don't understand why the compiler is referring to std::nullptr_t/nullptr.

2) How is it that the typedef resolves this (or at least appears to)? Is there a less 'spooky action at a distance' solution to this?

解决方案

The implementation of unique_ptr checks for the presence of a ::pointer type on the deleter. If the deleter has a ::pointer type then this type is used as the pointer typedef on the unique_ptr. Otherwise a pointer to the first template argument is used.

According to cppreference.com, the unique_ptr::pointer type is defined as

std::remove_reference<D>::type::pointer if that type exists, otherwise T*

这篇关于对于Windows HANDLE,使用std :: unique_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆