I try to move some unique_ptr in a vector during it declaration and I get a error. I think that I make a copy without knowing it.
I don't understand why I get the problem at the declaration while it works very well during a push_back.
I simplified the problem in few lines.
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
int main() {
unique_ptr<int> i1 = make_unique<int>(142);
unique_ptr<int> i2 = make_unique<int>(242);
unique_ptr<int> i3 = make_unique<int>(342);
vector<unique_ptr<int>> v;
//Those lines work
v.push_back(move(i1));
v.push_back(move(i2));
v.push_back(move(i3));
//ERROR
vector<unique_ptr<int>> v2 {move(i1), move(i2), move(i3)};
return 0;
}
The error is :
use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const
std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp =
std::default_delete<int>]'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
What am I missing ?
Thanks !