Any way to initialize a unique_ptr vector?
You cannot go from the initializer list because the elements const
. §8.5.4 [dcl.init.list] / p5:
A type object
std::initializer_list<E>
is constructed from list initializer as if the implementation had allocated an array ofN
elements of typeconst E
, where N is the number of elements in the initializer list. Each element of this array is initialized with a corresponding element in the initializer list and an Object isstd::initializer_list<E>
created to indicate that it is an array.
You can only copy a copy, but you cannot copy unique_ptr
as it only moves.
You will need to use push_back
or emplace_back
etc. to fill the vector after you create it.
source to share