2016-04-19 32 views
0

考慮下面的代碼:g++ -std=c++11 <filename>.cpp如何插入一個const的std :: shared_ptr的到一個std ::地圖

#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; 
} 

你必須編譯它。

它提供了以下錯誤:

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。由於這種變化,我沒有預料到會有什麼影響。

+2

'std :: make_pair'的要點是讓編譯器推導出類型。如果你想提供類型,使用'std :: pair '。 – Jarod42

+0

將來,請發佈[MCVE],它們實際上會重現您遇到的確切問題。在人們爲您的問題提供解決方案並將其更改爲新問題之後編輯您的問題就是糟糕的形式。並且@PatrykWertka在編輯之前設法解決了您的問題,但是您繼續編輯問題以產生*不同*問題,PatrykWertka的解決方案*仍然解決。 – Yakk

回答

5

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); 
+3

'std :: make_pair'應該有一個名爲'class stop_passing_arguments_to_me = I_am_not_kidding'的默認的第一個模板類型實參,它帶有一個'static_assert(std :: is_same :: value,「不提供類型參數給std ::手動make_pair,如果你想這樣做,請直接使用std :: pair,你用我錯了。「);'但也許這太微妙了。 – Yakk

+0

@Yakk是的,諷刺在這裏做得非常好;但更好的人可以解釋*爲什麼傳遞類型參數給出了確切的錯誤。 – Chani

+0

@wild這可能是未指定的行爲,但特定的錯誤是由於完美轉發的失敗。通過傳遞'X'作爲一個類型,arg是'X &&',一個右值,並且你通過了一個左值。上面的評論是因爲這個'make_pair'錯誤是非常普遍的。這可能是因爲你從一個只複製到一個可移動的make_pair而引入的,因爲猜測。 – Yakk

3
#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: note: cannot convert ‘vp_int8’ (type ‘ConstDataTypePtr {aka std::shared_ptr >}’) to type ‘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) 
+0

你是對的。之前我使用的是錯誤的類型。你能再看看這個問題嗎? (我已經更新了)。我仍然遇到錯誤。 – Chani

3

你給出了錯誤類型的的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>部分)

編輯:或不提供模板PARAMS所有的,因爲有人在評論建議。

+0

嗨,我非常抱歉,我之前在代碼中出現了這個錯誤。但是,仍然存在問題。你能再看看嗎?我已經更新了代碼和錯誤。 – Chani

+0

你說得對。刪除模板參數將刪除錯誤。但是,您能否告訴我在這種情況下什麼纔是正確的模板參數? – Chani

+0

它包含在我的答案 - 「」是正確的模板參數。 –

0

不要在make_pair函數中提到模板中的類型。

m_nameToType.insert(std::make_pair("int8_t", vp_int8)); 
相關問題