我试图使用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;
Run Code Online (Sandbox Code Playgroud)
稍后在我的代码中尝试使用它时:
unique_vol_handle_t volH( FindFirstVolumeW( buffer, MAX_GUID_PATH ) );
我从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
Run Code Online (Sandbox Code Playgroud)
引用上面的volH声明行.
搜索了一段时间后,我发现了一篇博文,基本上说,添加: …