#include <map>
#include <string>
#include <memory>
#include <vector>
using namespace std;
// Used in the example
struct Resource {};
int main(int argc, char** argv) {
// I was able to get the following map running fine
// int -> { string -> unique_ptr }
map<int, map<string, unique_ptr<Resource>>> data;
map<string, unique_ptr<Resource>> toBeInserted;
toBeInserted["key"] = unique_ptr<Resource>(new Resource);
// data[1] = toBeInserted; // error
data[1] = std::move(toBeInserted); // ok
// But the issue happens when it's a vector of unique_ptrs
// int -> { string -> { [unique_ptr] } }
map<int, map<string, vector<unique_ptr<Resource>>>> otherData;
vector<unique_ptr<Resource>> list;
list.push_back(unique_ptr<Resource>(new Resource));
list.push_back(unique_ptr<Resource>(new Resource));
map<string, vector<unique_ptr<Resource>>> _toBeInserted;
_toBeInserted["key"] = std::move(list); // ok
// But I cant insert _toBeInserted back to the original map.
// The compilation errors are all related to the unique_ptr
otherData[1] = std::move(_toBeInserted); // Can't do this
}
I2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPG1lbW9yeT4KI2luY2x1ZGUgPHZlY3Rvcj4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBVc2VkIGluIHRoZSBleGFtcGxlCnN0cnVjdCBSZXNvdXJjZSB7fTsKCmludCBtYWluKGludCBhcmdjLCBjaGFyKiogYXJndikgewogIC8vIEkgd2FzIGFibGUgdG8gZ2V0IHRoZSBmb2xsb3dpbmcgbWFwIHJ1bm5pbmcgZmluZQogIC8vIGludCAtPiB7IHN0cmluZyAtPiB1bmlxdWVfcHRyIH0KICBtYXA8aW50LCBtYXA8c3RyaW5nLCB1bmlxdWVfcHRyPFJlc291cmNlPj4+IGRhdGE7CgogIG1hcDxzdHJpbmcsIHVuaXF1ZV9wdHI8UmVzb3VyY2U+PiB0b0JlSW5zZXJ0ZWQ7CiAgdG9CZUluc2VydGVkWyJrZXkiXSA9IHVuaXF1ZV9wdHI8UmVzb3VyY2U+KG5ldyBSZXNvdXJjZSk7CiAgLy8gZGF0YVsxXSA9IHRvQmVJbnNlcnRlZDsgLy8gZXJyb3IKICBkYXRhWzFdID0gc3RkOjptb3ZlKHRvQmVJbnNlcnRlZCk7IC8vIG9rCgoKICAvLyBCdXQgdGhlIGlzc3VlIGhhcHBlbnMgd2hlbiBpdCdzIGEgdmVjdG9yIG9mIHVuaXF1ZV9wdHJzCiAgLy8gaW50IC0+IHsgc3RyaW5nIC0+IHsgW3VuaXF1ZV9wdHJdIH0gfQogIG1hcDxpbnQsIG1hcDxzdHJpbmcsIHZlY3Rvcjx1bmlxdWVfcHRyPFJlc291cmNlPj4+PiBvdGhlckRhdGE7CgogIHZlY3Rvcjx1bmlxdWVfcHRyPFJlc291cmNlPj4gbGlzdDsKICBsaXN0LnB1c2hfYmFjayh1bmlxdWVfcHRyPFJlc291cmNlPihuZXcgUmVzb3VyY2UpKTsKICBsaXN0LnB1c2hfYmFjayh1bmlxdWVfcHRyPFJlc291cmNlPihuZXcgUmVzb3VyY2UpKTsKCiAgbWFwPHN0cmluZywgdmVjdG9yPHVuaXF1ZV9wdHI8UmVzb3VyY2U+Pj4gX3RvQmVJbnNlcnRlZDsKICBfdG9CZUluc2VydGVkWyJrZXkiXSA9IHN0ZDo6bW92ZShsaXN0KTsgLy8gb2sKCiAgLy8gQnV0IEkgY2FudCBpbnNlcnQgX3RvQmVJbnNlcnRlZCBiYWNrIHRvIHRoZSBvcmlnaW5hbCBtYXAuCiAgLy8gVGhlIGNvbXBpbGF0aW9uIGVycm9ycyBhcmUgYWxsIHJlbGF0ZWQgdG8gdGhlIHVuaXF1ZV9wdHIgCiAgb3RoZXJEYXRhWzFdID0gc3RkOjptb3ZlKF90b0JlSW5zZXJ0ZWQpOyAvLyBDYW4ndCBkbyB0aGlzIAp9
In file included from /usr/include/c++/4.9/memory:64:0,
from prog.cpp:3:
/usr/include/c++/4.9/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<Resource>; _Args = {const std::unique_ptr<Resource, std::default_delete<Resource> >&}]':
/usr/include/c++/4.9/bits/stl_uninitialized.h:75:53: required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Resource>*, std::vector<std::unique_ptr<Resource> > >; _ForwardIterator = std::unique_ptr<Resource>*; bool _TrivialValueTypes = false]'
/usr/include/c++/4.9/bits/stl_uninitialized.h:125:41: required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Resource>*, std::vector<std::unique_ptr<Resource> > >; _ForwardIterator = std::unique_ptr<Resource>*]'
/usr/include/c++/4.9/bits/stl_uninitialized.h:278:63: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Resource>*, std::vector<std::unique_ptr<Resource> > >; _ForwardIterator = std::unique_ptr<Resource>*; _Tp = std::unique_ptr<Resource>]'
/usr/include/c++/4.9/bits/stl_vector.h:324:32: required from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<Resource>; _Alloc = std::allocator<std::unique_ptr<Resource> >]'
/usr/include/c++/4.9/bits/stl_pair.h:127:17: required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > >; _Args = {std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::unique_ptr<Resource, std::default_delete<Resource> >, std::allocator<std::unique_ptr<Resource, std::default_delete<Resource> > > > >&}; _Tp = std::_Rb_tree_node<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >]'
/usr/include/c++/4.9/bits/alloc_traits.h:253:4: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/4.9/bits/stl_tree.h:1143:66: required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, _Arg&&) [with _Arg = std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > >&; _Key = std::basic_string<char>; _Val = std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > >; _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr = std::_Rb_tree_node_base*]'
/usr/include/c++/4.9/bits/stl_tree.h:1603:33: required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Arg&&) [with _Arg = std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > >&; _Key = std::basic_string<char>; _Val = std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > >; _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >]'
/usr/include/c++/4.9/bits/stl_tree.h:1843:37: required from 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; _Key = std::basic_string<char>; _Val = std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > >; _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >]'
/usr/include/c++/4.9/bits/stl_map.h:706:11: required from 'void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >; _Key = std::basic_string<char>; _Tp = std::vector<std::unique_ptr<Resource> >; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >]'
/usr/include/c++/4.9/bits/stl_map.h:317:54: required from 'std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(std::map<_Key, _Tp, _Compare, _Alloc>&&) [with _Key = std::basic_string<char>; _Tp = std::vector<std::unique_ptr<Resource> >; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<std::unique_ptr<Resource> > > >]'
prog.cpp:35:16: required from here
/usr/include/c++/4.9/bits/stl_construct.h:75:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Resource; _Dp = std::default_delete<Resource>]'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
In file included from /usr/include/c++/4.9/memory:81:0,
from prog.cpp:3:
/usr/include/c++/4.9/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^