Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a vector of unique_ptr into another vector

I have a vector of unique_ptr's and I want to append them to another vector of unique_ptrs. I would normally do a 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 this gives me compile errors similar to 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 a convenient way to insert or do I have to iterate over baz and push_back on bar? I'm currently using gcc 4.8.1.

Thanks

like image 565
Chris Avatar asked Jan 31 '14 18:01

Chris


People also ask

Can I have a vector of unique_ptr?

This means that you can't make copies of a unique_ptr (because then two unique_ptr s would have ownership), so you can only move it. D.R. Since there can be only one, one should also be able to pass a temporary directly to the vector: vec. push_back(std::unique_ptr<int>(new int(1))); .

Can unique_ptr be moved?

A unique_ptr can only be moved. This means that the ownership of the memory resource is transferred to another unique_ptr and the original unique_ptr no longer owns it. We recommend that you restrict an object to one owner, because multiple ownership adds complexity to the program logic.


2 Answers

unique_ptr is not assignable with normal assignment operator (the error says it's deleted). You can only move them:

bar.insert(bar.end(),
    std::make_move_iterator(baz.begin()),
    std::make_move_iterator(baz.end())
);

Of course, this transfers the ownership of the managed object and original pointers will have nullptr value.

like image 153
jrok Avatar answered Oct 02 '22 17:10

jrok


You can't copy them; you'll have to move them.

std::move(baz.begin(), baz.end(), std::back_inserter(bar));
like image 39
Mike Seymour Avatar answered Oct 02 '22 17:10

Mike Seymour