我正在尝试将 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 ==========