Does std::vector *have* to move objects when growing capacity? Or, can allocators “reallocate”?

后端 未结 3 1517
广开言路
广开言路 2020-11-27 04:33

A different question inspired the following thought:

Does std::vector have to move all the elements when it increases its capacity?

3条回答
  •  醉酒成梦
    2020-11-27 04:58

    Yep, you're right that the standard allocator interface doesn't provide optimizations for memcpy'able types.

    It's been possible to determine whether a type can be memcpy'd using boost type traits library (not sure if they provide it out of the box or one would have to build a composite type discriminator based on the boost ones).

    Anyway, to take advantage of realloc() one would probably create a new container type that can explicitly take advantage of this optimization. With current standard allocator interface it doesn't seem to be possible.

提交回复
热议问题