When std::vector reallocate its memory array, is copy constructor or move constructor used?

后端 未结 1 1302
不知归路
不知归路 2021-01-16 15:07

When std::vector reallocate its memory array, what kind of copy / move constructor is used to copy / move elements to new houses?

相关标签:
1条回答
  • 2021-01-16 15:36

    If the move-constructor exists and is noexcept then it is used. Otherwise the copy-constructor is used.

    Using a move-constructor that might throw is undesirable as it might happen that some objects are moved to the new storage and then an exception prevents the rest of the objects being moved.

    The cppreference.com site does say that if the object is non-copyable , but has a non-noexcept move constructor, then it will use that move-constructor, with "unspecified behaviour" if an exception is thrown. I guess that means elements may be lost from the vector.

    0 讨论(0)
提交回复
热议问题