【发布时间】:2017-09-27 23:05:43
【问题描述】:
我有 2 个数据结构,其中包含一组 unique_ptr 像这样
std::vector<std::unique_ptr<Entity>> tmpEnts;
std::set<std::unique_ptr<Entity>> tmpSet;
我正在尝试执行如下所示的插入,但在编译时出现错误。我是智能指针的新手。
tmpSet.insert(tmpEnts.begin(), tmpEnts.end());
tmpEnts其实是一个简化,它实际上驻留在std::map<uint, std::vector<std::unique_ptr<Entity>>> entities
编辑
这个错误占用了很多空间,但我认为这里有些东西我还不太明白。
/home/evgen/workspace/test/ecs/src/entity_manager.cpp:41:22: required from here
/usr/include/c++/7.2.0/ext/new_allocator.h:136:4: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Entity; _Dp = std::default_delete<Entity>]’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7.2.0/memory:80:0,
from /home/evgen/workspace/test/ecs/src/entity.hpp:4,
from /home/evgen/workspace/test/ecs/src/entity_manager.cpp:4:
/usr/include/c++/7.2.0/bits/unique_ptr.h:388:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
【问题讨论】:
-
疯狂猜测我假设您的编译器尝试使用复制
.insert()范围,如果在std::unique_ptr上执行它是禁止的 -
是的,我也有类似的想法,我尝试使用 std::move。得到同样的错误。
标签: c++ c++11 unique-ptr