All the STL containers that implement resize use copies to populate the new elements even if the source of the copy is a default constructed object?
Why is it done this way?
I see no advantage and some cost.
As context, I ran across this while looking for a random access container for elements that can’t be copied.
It saves on complexity. We certainly need the copy-construction case, and default-construction can be modeled as replicating a default-constructed object.
The performance penalty is negligible. Writing zeroes is about the same speed as copying zeroes. The compatibility penalty is nil since all containers require copyability anyway. Default-construction on the other hand isn’t required.
If you really want to use standard containers with non-copyable objects, look into C++0x and in-place construction with
emplace. However, there is no method toemplacemultiple elements at once. (If you’re usingdeque, there shouldn’t be much performance penalty to anemplaceloop vs.resizethough.)