I cannot move a std::vector<std::unique_ptr<..>>
from a function:
MSVC complains (C2280) about attempting to reference a deleted function.
How would this work?
#include <vector>
#include <iostream>
#include <memory>
using namespace std;
class foo {
public:
int i;
};
vector<unique_ptr<foo>> test() {
vector<unique_ptr<foo>> ret{};
auto f = make_unique<foo>();
f->i = 1;
ret.push_back(move(f));
return move(ret);
}
int main(int argc, char** argv) {
auto t = test();
for (auto j : t) {
// fails here: --^
cout << j->i << endl;
}
getchar();
}
The complete error message reads:
'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
return move(ret);
justreturn ret;
is enough. – hellowfor
loop is working? What you're attempting to do is copying the values from the vector. – Some programmer dude