std :: bad_alloc之后std :: vector的状态 [英] state of std::vector after std::bad_alloc

查看:520
本文介绍了std :: bad_alloc之后std :: vector的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找在线参考,以查看几个std容器的异常安全性.

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

对于std::vector,是否保持push_back调用之前的状态?我认为向量的所有对象仍然有效(不调用析构函数).在push_back引发std::bad_alloc异常后,什么保证提供std::vector?

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?

推荐答案

如果抛出,向量不会改变.甚至不是capacity().

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

根据[container.requirements.general]:

According to [container.requirements.general]:

除非另有说明(请参见23.2.4.1、23.2.5.1、23.3.3.4和23.3.6.5),否则在此定义的所有容器类型 条款符合以下附加要求:

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:

[...]

-如果push_back()或push_front()函数引发异常,则该函数无效.

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

这篇关于std :: bad_alloc之后std :: vector的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆