I’m trying to move a unordered_map of unique_ptr’s into another map, but getting the compile error below.
#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 ==========
I think this is an issue that you can’t add elements to a map or set by copying to the end. A more simple example that illustrate this is
to get around this, you need an insert_iterator, so
or
Internally, the insert iterator will attempt to call ‘insert’ in the passed in container, so rather than trying to do ‘*(bMap.end()) = *(aMap.begin())’, it will call ‘bMap.insert(pos, *(aMap.begin()))’, though pos is not extremely important for this situation.