【发布时间】:2018-05-09 02:09:02
【问题描述】:
为什么我不能将 map 的大括号初始化与 unique_ptr 一起使用?
编辑:我在 c++17 模式下使用 gcc 7.2。
我可以使用 make_pair 和 [] 运算符创建并插入无序映射。
std::unordered_map<std::string, std::unique_ptr<A>> map;
map.insert(std::make_pair("hello",std::make_unique<A>()));
map["foo"] = std::make_unique<A>();
但我不知道为什么在使用大括号时会失败。
map.insert({"foo", std::make_unique<A>()}); // Error
错误:使用已删除的函数'std::pair<_t1 _t2>::pair(const std::pair<_t1 _t2>&)
【问题讨论】:
-
你使用的是C++14,因为之前没有
std::make_unique。 -
afaik 插入副本(unique_ptr 不可复制),使用 emplace
标签: c++ dictionary c++14 std unique-ptr