I believe the issue is with your vector
constructor call (2: fill constructor):
std::vector<std::unique_ptr<int []>> vec(5, nullptr);
Here, you're essentially calling vector(size_t(5), std::unique_ptr<int[]>(nullptr))
. Note that this creates a temporary instance of std::unique_ptr
, implicitly converted/constructed from your nullptr
argument. The vector
constructor is then supposed to copy this value you pass to it n
times to fill out the container; since you can't copy any unique_ptr
(even a null one), you get your compiler error from within that constructor's code.
If you're immediately replacing those initial nullptr
values, you should just construct an empty vector
and push_back
your new elements:
std::vector<std::unique_ptr<int []>> vec; // default constructor
vec.push_back(std::make_unique<int []>(3)); // push the elements (only uses the move
vec.push_back(std::make_unique<int []>(4)); // constructor of the temporary)
...
To initialize a vector
with some number of null values, omit the second parameter:
std::vector<std::unique_ptr<int []>> vec(5);
This will construct each unique_ptr
with the default constructor, not requiring any copying.
std::vector<std::vector<int>>
? – Galik