unique_ptr
cannot be copied or assigned. It needs to be move copied or move assigned. You can achieve this by use of std::move
:
std::unique_ptr<A> a = std::move(vec_a.at(0));
The call to std::move
essentially allows you to treat the RHS as an rvalue, which can then be used in unique_ptr
'a move copy constructor.
Note that after this, you will have to be careful with the first element of vec_a
.
If you don't want to take ownership of the pointer away from the vector, simply access the managed pointer directly:
A* a = vec_a.at(0).get();
You have to be careful not to use a
after the object it points to has ceased to exist.