jeudi 18 août 2016

How to tersely insert a default-constructed object into a std::vector

All the overloads of std::vector::insert() require a value argument, which is the item to be inserted. However, my item type is complicated and I would rather have an empty-constructed item inserted. That is, I'm looking for a way to do the following without the type name duplication.

std::vector<Long_and_complicated_type_name> v;
...
v.insert(iter, Long_and_complicated_type_name());

The same issue exists for push_back though I'm primarily interested in insert.

Aucun commentaire:

Enregistrer un commentaire