In this code
vector<std::string> vec = { "long string", "exterminate" };
an initializer_list
of std::string
is created and each element in copied into vec
. This is inefficient and makes it impossible to initialize vectors with move only types. Why can't the constructor perfectly forward the arguments?
If that was true you could do this:
vector<unique_ptr<int>> vec = {
make_unique<int>(5), make_unique<int>(55), make_unique<int>(-4)
};
Instead of this:
vector<unique_ptr<int>> vec;
vec.push_back(make_unique<int>(5));
vec.push_back(make_unique<int>(55));
vec.push_back(make_unique<int>(-4));
Furthermore, if vector
cares about copying elements in its constructor, why are you allowed to move elements with push_back
?