【发布时间】:2013-06-15 08:44:00
【问题描述】:
我有这段代码将映射从 into 初始化为 unique_ptr。
auto a = unique_ptr<A>(new A());
map<int, unique_ptr<A>> m;
m[1] = move(a);
我可以使用统一初始化吗?我试过了
map<int, unique_ptr<A>> m {{1, unique_ptr<A>(new A())}};
但是我遇到了一个错误。
部分错误信息是
In instantiation of 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {const std::pair<const int, std::unique_ptr<A, std::default_delete<A> > >&}; _Val = std::pair<const int, std::unique_ptr<A> >]': ... In file included from /opt/local/include/gcc48/c++/memory:81:0,
from smart_pointer_map.cpp:3: /opt/local/include/gcc48/c++/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
【问题讨论】:
-
错误是什么?程序在运行时崩溃了吗?
标签: c++ c++11 map smart-pointers uniform-initialization