samedi 27 avril 2019

Why does std::vector allow the use of a throwable move constructor for the type it contains?

Apparently, std::move_if_noexcept() will call the move constructor, even if it is not marked as noexcept if there is no copy constructor available. As std::vector is using this function on reallocation, that could leave the vector and possibly the application in an undetermined state.

From cpprefeerence.com:

Notes

This is used, for example, by std::vector::resize, which may have to allocate new storage and then move or copy elements from old storage to new storage. If an exception occurs during this operation, std::vector::resize undoes everything it did to this point, which is only possible if std::move_if_noexcept was used to decide whether to use move construction or copy construction. (unless copy constructor is not available, in which case move constructor is used either way and the strong exception guarantee may be waived)

Why would this be allowed?

Aucun commentaire:

Enregistrer un commentaire