如何将const std::shared_ptr插入到std::映射中

How to insert a const std::shared_ptr into a std::map

本文关键字:std 插入 映射 ptr shared const      更新时间:2023-10-16

考虑以下代码:

#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&amp;'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));