将std :: make

编程入门 行业动态 更新时间:2024-10-24 18:22:11
本文介绍了将std :: make_unique与自定义删除器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在将 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 封装了使用 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

本文发布于:2023-10-24 19:37:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!