如何将const std::shared_ptr插入到std::映射中
How to insert a const std::shared_ptr into a std::map
考虑以下代码:
#include <string>
#include <map>
#include <memory>
#include <utility>
#include <iostream>
typedef std::shared_ptr<const std::string> ConstDataTypePtr;
typedef std::map<std::string, ConstDataTypePtr> StrDataTypeMap;
int main()
{
StrDataTypeMap m_nameToType;
ConstDataTypePtr vp_int8(new std::string("RGH"));
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>("int8_t", vp_int8));
return 0;
}
您必须使用:g++ -std=c++11 <filename>.cpp
编译它。
它给出以下错误:
testO.cpp: In function ‘int main()’:
testO.cpp:14:88: error: no matching function for call to ‘make_pair(const char [7], ConstDataTypePtr&)’
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>("int8_t", vp_int8));
^
testO.cpp:14:88: note: candidate is:
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/include/c++/4.8.2/string:40,
from testO.cpp:1:
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
make_pair(_T1&& __x, _T2&& __y)
^
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note: template argument deduction/substitution failed:
testO.cpp:14:88: note: cannot convert ‘vp_int8’ (type ‘ConstDataTypePtr {aka std::shared_ptr<const std::basic_string<char> >}’) to type ‘std::shared_ptr<const std::basic_string<char> >&&’
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>("int8_t", vp_int8));
根据我对错误的理解,当我试图插入到映射中时,编译器期望r值。为什么?我在这里犯了什么错误?
请注意,我从一些现有的代码中创建了这个片段,这些代码是大型代码库的一部分。可能还值得一提的是,该片段取自在Windows上运行的代码库,我的任务是将其移植到Linux。原作者使用了std::tr1::shared_ptr。我将其修改为使用std::shared_ptr。我没想到会因为这个变化而受到任何影响。
std::make_pair
的全部目的是让编译器推导类型。如果要提供类型,请使用std::pair<K, V>
所以
m_nameToType.insert(std::make_pair<std::string, std::string>("int8_t", vp_int8));
应该是:
m_nameToType.insert(std::make_pair("int8_t", vp_int8));
或
m_nameToType.insert(std::pair<const std::string, ConstDataTypePtr>("int8_t", vp_int8));
或者简单地说:
m_nameToType.emplace("int8_t", vp_int8);
#include <memory>
#include <map>
#include <string>
int main() {
using shared_data = std::shared_ptr<const std::string>;
std::map<std::string, shared_data> map;
map.insert(std::make_pair(
"something",
shared_data(new std::string("something else"))
));
return 0;
}
请参阅:http://ideone.com/4AQfqd
回到你的问题上来;
testO.cpp:14:83:注意:无法将"vp_int8"(类型"ConstDataTypePtr{aka std::shared_ptr>}")转换为类型"std::basic_string&;'m_nameToType.insert(std::make_pair("int8_t",vp_int8));
您所拥有的:
std::make_pair<std::string, std::string>(some_string, TOTALLY_NOT_A_STRING)
您为std::make_pair模板提供了错误的类型。只需更改
m_nameToType.insert(std::make_pair<std::string, std::string>("int8_t", vp_int8));
进入
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>(std::string("int8_t"), vp_int8));
(注意std::make_pair<std::string, ConstDataTypePtr>
部分)
编辑:或者根本不提供模板参数,就像有人在评论中建议的那样。
不要在make_pair函数中提及模板中的类型。
m_nameToType.insert(std::make_pair("int8_t", vp_int8));
相关文章:
- 有效地使用std::unordered_map来插入或增加键的值
- 将重物插入std::map
- 如何有效地在 std::vector 中插入一对?
- 在基于范围的 for 循环期间插入 std::list 的后面
- 为 std::vector 编写自定义插入函数
- std::unordered_map 如果输入大小已知,如何优化批量插入
- C++ - 最坏情况和平均情况插入时间复杂度在 std::unordered_map <int,int>?
- 找不到最新插入到 std::map 中的键
- 在 std::map 中插入数组元素
- 有没有更快的方法可以在 std::vector 中插入元素
- 插入 std::set 作为 std::map 的键
- 通过构造函数插入 std::map
- 将带有模板的自定义类插入到 std::map 中
- 如何将 n 个连续元素插入到元素类型不可复制的 std::vector 中?
- 将唯一指针插入std::映射
- 如果迭代器没有因插入而无效,则使用std::find和C::insert()是线程安全的
- std::为插入和排序设置不同的比较器
- std::映射导致插入时出现C2664错误
- 为什么std ::插入需要复制构造性
- std::插入无序集合(或映射)的迭代器