简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM