I have a class member variable as
vector<std::unique_ptr<T> > v;
and a member function where I want to use a unique_ptr
element of v
"addressed" by an iterator
argument. Which one is better?
void mem_fun(vector<std::unique_ptr<T> >::iterator it) {
std::unique_ptr<T> p;
p = std::move(*it);
...
}
Or
void mem_fun(vector<std::unique_ptr<T> >::iterator it) {
std::unique_ptr<T>& p = *it;
...
}
From what I know, it seems the second way just kind of violates the "uniqueness" of unique_ptr
. But can std::move()
move *it
(a reference)? BTW, who truly owns the unique_ptr
pointers, the class, the member vector, any member function, or what else?