Cpp crusaders
Cpp crusaders

Reputation: 111

emplace_back calls assignment operator in vectors but not in list

As per http://www.cplusplus.com/reference/vector/vector/emplace_back/ I understood that emplace_back would create objects in place without calling the assignment operator . But in case of std::vector they call the assignment operator and they donot call the assignment operators in case of std::list.

My object is not copyable. Is there any other way to get around the problem other then by using pointers.

Also erase in vector seems to call the assignment operator, Erase in list doesnot call the assignment operator. this seemed wrong to me..

Does std does not support objects which are not copyable?

Upvotes: 0

Views: 594

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254441

vector requires the element type to be movable (not necessarily copyable) in order to maintain the elements in a contiguous array.

Insertion, at any point, might require all the elements to be moved to a new array, if the old capacity was too small. Erasing, except at the end, requires elements after the erased one(s) to be moved forward.

Other containers don't require the type to be movable, so perhaps deque (allowing insertion and removal at either end) or list (allowing insertion and removal anywhere) might be an option if you can't (or don't want to) make it movable.

Upvotes: 5

Related Questions