无法理解如何向std :: list< std :: unique_ptr< classname>>添加新对象。 [英] Cannot understand how to add new object to std::list<std::unique_ptr<classname>>

查看:183
本文介绍了无法理解如何向std :: list< std :: unique_ptr< classname>>添加新对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对unique_ptr的std :: list有一个奇怪的问题。

I have strange issue with std::list of unique_ptr's.

slFlyingMonster类是从slMonster类派生的。

Class slFlyingMonster is derived from class slMonster.

以下代码有效:

std::unique_ptr<slMonster> ptr(new slFlyingMonster(md));

但是此代码:

std::list<std::unique_ptr<slMonster>> mMonsters;
mMonsters.push_back(new slFlyingMonster(md));

抛出错误:


错误1错误C2664:'void

std :: list>,std :: allocator >>> :: push_back(const
std :: unique_ptr< _Ty,std: :default_delete< _Ty >>&)':无法将
参数1从'slFlyingMonster *'转换为
'std :: unique_ptr>&&'

"Error 1 error C2664: 'void
std::list>,std::allocator>>>::push_back(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : cannot convert argument 1 from 'slFlyingMonster *' to 'std::unique_ptr> &&'"

据我所知,出了点问题,例如std :: list.push_back()与=不同,但我无法弄清楚如何正确添加新类作为unique_ptr列出。任何建议都将非常受欢迎。

While I understand, that something is wrong, like std::list.push_back() is not the same as =, but I cannot figure out how to correctly add new class as unique_ptr to list. Any suggestions would be very welcome.

推荐答案

在有以下情况时使用 push_back 列表包含的类型的对象,并且您想推送其副本。通常,如果您还没有这样的对象(就您的情况而言,则没有),最好直接在列表中初始化新对象—使用 emplace_back 代替:

Use push_back when you have an object of the type which your list contains, and you want to push its copy. Normally, if you don't have such an object yet (in your case, you don't), you're better off initialising a new object directly in the list — using emplace_back instead:

std::list<std::unique_ptr<slMonster>> mMonsters;
mMonsters.emplace_back(new slFlyingMonster(md));

但是,正如@SebastianRedl在评论中正确指出的那样,上述内容存在不能成为例外的问题-安全。如果在 std :: list 内对新节点的内部分配抛出异常,则新的 slFlyingMonster 实例将被泄漏。当参数之一是不受保护的资源(例如拥有内存的原始指针)时, emplace_back 不是正确的选择。

However, as @SebastianRedl correctly pointed out in the comments, the above has a problem of not being exception-safe. If the internal allocation of a new node inside std::list throws, the new slFlyingMonster instance would be leaked. emplace_back is not the correct choice when one of the arguments is an unprotected resource (such as a raw pointer owning memory).

因此,您实际上想构造一个包装器智能指针并将其推入列表。在C ++ 14中,您可以使用 std :: make_unique

So you actually want to construct a wrapper smart pointer and push it into the list. In C++14, you can do this with std::make_unique:

std::list<std::unique_ptr<slMonster>> mMonsters;
mMonsters.push_back(std::make_unique<slFlyingMonster>(md));

使用普通C ++ 11,您可以实现自己的 make_unique ,或显式创建智能指针:

With plain C++11, you can either implement your own make_unique, or explicitly create the smart pointer:

std::list<std::unique_ptr<slMonster>> mMonsters;
mMonsters.emplace_back(std::unique_ptr<slMonster>(new slFlyingMonster(md)));

这篇关于无法理解如何向std :: list&lt; std :: unique_ptr&lt; classname&gt;&gt;添加新对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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