state of std::vector after std::bad_alloc

北城以北 提交于 2019-12-12 11:52:44

问题


I'm trying to find a online reference to see the exception safety of several std containers.

In the case of std::vector, Does it keep the state previous to the push_back call? I would presume the vector has all its objects still valid (no destructors invoked). What guarantees offer std::vector after push_back throws a std::bad_alloc exception?


回答1:


If it throws, the vector isn't changed. Even not the capacity().

According to [container.requirements.general]:

Unless otherwise specified (see 23.2.4.1, 23.2.5.1, 23.3.3.4, and 23.3.6.5) all container types defined in this Clause meet the following additional requirements:

[...]

— if an exception is thrown by a push_back() or push_front() function, that function has no effects.



来源:https://stackoverflow.com/questions/8899814/state-of-stdvector-after-stdbad-alloc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!