我試圖將unique_ptr的unordered_map移動到另一個映射中,但在下面得到編譯錯誤。將unique_ptr的unordered_map移動到另一個
#include <memory>
#include <unordered_map>
#include <string>
int main()
{
std::unordered_map<int, std::unique_ptr<int>> aMap;
std::unordered_map<int, std::unique_ptr<int>> bMap;
std::unique_ptr<int> ptr(new int);
*ptr = 10;
aMap.insert(std::make_pair(0, std::move(ptr)));
std::move(aMap.begin(), aMap.end(), bMap.end());
return 0;
}
1>------ Build started: Project: Testing, Configuration: Debug Win32 ------
1>Build started 08.07.2012 23:54:16.
1>InitializeBuildStatus:
1> Creating "Debug\Testing.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> main.cpp
1>d:\progs\visual studio 2010\vc\include\utility(260): error C2166: l-value specifies const object
1> d:\progs\visual studio 2010\vc\include\utility(259) : while compiling class template member function 'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(std::pair<_Ty1,_Ty2> &&)'
1> with
1> [
1> _Ty1=const int,
1> _Ty2=std::unique_ptr<int>
1> ]
1> d:\coding\testing\main.cpp(12) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1> with
1> [
1> _Ty1=const int,
1> _Ty2=std::unique_ptr<int>
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.64
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我不認爲StackOverflow是有史以來最重要的東西,更不用說重新開始 – Fraser 2012-07-08 22:40:13
我想我現在是延遲:) – bitgregor 2012-07-08 22:59:09
我相信部分問題是插入和make_pair的組合正試圖調用複製唯一ptr的構造函數,它不存在。如果您將unique_ptr更改爲shared_ptr,它將會編譯到您的std :: move(aMap.begin()...也就是說,您在make_pair的爭論中對std :: move有正確的想法,就好像您一樣這是獨立的,但它工作,但失敗,並嘗試調用一個Map.insert上的複製構造函數。 – Trickfire 2012-07-08 23:08:15