c++ - Inserting a vector of unique_ptr into another vector -
i have vector of unique_ptr's , want append them vector of unique_ptrs. simple insert:
std::vector<std::unique_ptr<foo>> bar; bar.push_back(std::unique_ptr<foo>(new foo(1))); std::vector<std::unique_ptr<foo>> baz; baz.push_back(std::unique_ptr<foo>(new foo(2))); bar.insert(bar.end(), baz.begin(), baz.end());
however gives me compile errors similar this:
/usr/include/c++/4.8/bits/stl_algobase.h:335: error: use of deleted function 'std::unique_ptr<_tp, _dp>& std::unique_ptr<_tp, _dp>::operator=(const std::unique_ptr<_tp, _dp>&) [with _tp = foo; _dp = std::default_delete<foo>]' *__result = *__first; ^
is there convenient way insert or have iterate on baz , push_back on bar? i'm using gcc 4.8.1.
thanks
unique_ptr
not assignable normal assignment operator (the error says it's deleted). can move them:
bar.insert(bar.end(), std::make_move_iterator(baz.begin()), std::make_move_iterator(baz.end()) );
of course, transfers ownership of managed object , original pointers have nullptr
value.
Comments
Post a Comment