duong_dajgja duong_dajgja - 3 years ago 91
C++ Question

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download