将std :: make_unique与自定义删除器一起使用 [英] Using std::make_unique with a custom deleter
问题描述
在将 std :: unique_ptr
与自定义删除器一起使用时,我希望使用 std :: make_unique
而不是原始的。我正在使用VC ++ 2013。在我看来,如果使用自定义删除器,则无法使用 std :: unique_ptr
。我是想念东西还是真的是这样?
其他信息:
我正在使用 std :: unique_ptr< HANDLE,custom_deleter>
来为打开的COM端口保持Windows HANDLE。
我可以为此编写一个定制的RAII类,这并不困难,但是我看到使用 std会多么困难/困难/不好: unique_ptr
。
make_unique $ c $的要点c>封装了使用
new
从给定的构造函数参数创建 T
的概念并使用 delete
销毁它。
如果要使用自定义删除器,还必须指定如何创建该对象,然后通过使用具有嵌入功能的maker函数将不再有任何收获。
我写了一些自定义示例某些唯一资源句柄的生成器功能
这篇文章中。 p>
In using std::unique_ptr
with a custom deleter I desire to use std::make_unique
rather than a raw new. I am using VC++2013. It appears to me that there is no way to use std::unique_ptr
if you are using a custom deleter. Did I miss something or is this really the case?
Additional Information:
I am using a std::unique_ptr<HANDLE, custom_deleter>
to hold a Windows HANDLE for an opened COM port.
I could write a custom RAII class for this, and it wouldn't be terribly difficult, but I was seeing how hard/difficult/bad it would be to use std::unique_ptr
.
The whole point of make_unique
is to encapsulate the notion of "use new
to create a T
from given constructor arguments and use delete
to destroy it".
If you wanted a custom deleter, you would also have to specify how to create the object, and then there would be nothing more gained from having the emplacing maker function.
I wrote some examples of custom maker functions for certain unique resource handles in this post.
这篇关于将std :: make_unique与自定义删除器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!