是否有可能在任何地方抛出异常的 STL 容器方法列表?

发布于 2024-10-04 16:20:52 字数 151 浏览 0 评论 0原文

我知道 STL 会抛出内存分配错误,或者如果包含的类型在其构造函数/赋值运算符中抛出异常。

否则,显然“一些”STL 方法可能会引发其他异常。每个人似乎都提到的示例是 vector::at(),但我在任何地方都找不到其他示例的列表。

有谁知道这样的清单吗?

I know the STL will throw on a memory allocation error or if the contained type throws in its constructor / assignment operator.

Otherwise, apparently 'a few' STL methods can throw other exceptions. The example everyone seems to mention is vector::at(), but I can't find a list of the others anywhere.

Does anyone know of such a list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所有深爱都是秘密 2024-10-11 16:20:52

不会是 100% 准确,并且适​​用于 C++03,但是基于 GCC 4.3.4 的 grep 半小时的努力包括忽略 tr1 和 ext 但包括 iostream。至关重要的是,其中一些检查可能是由于该实现更喜欢防御性编码,并且可能没有在标准中强制执行并且可以普遍使用.​​.....

  • bitset
    • std::overflow_error - .to_ulong() 当太多位无法容纳 unsigned long
    • std::out_of_range - operator[]() 尝试超过末尾
  • new
    • std::bad_alloc
  • 类型信息
    • std::bad_cast 尝试 dynamic_cast 无效
  • ios
    • std::ios_base::failure 使用异常掩码进行错误报告时
  • string
    • out_of_range - at/追加/分配/插入/删除/替换/复制/substr
    • length_error:在reserve或隐式调整大小期间尝试超过max_size()(例如分配/插入/+=等)
  • 语言环境
    • std::bad_cast 如果语言环境不包含 Facet 类型的构面
    • std::runtime_error 在各种空指针/未定义方面的情况下
  • deque/vector
    • length_error:尝试reserve()或隐式增长> max_size()
    • out_of_rangeat()
  • 地图
    • std::out_of_rangeat()

Won't be 100% accurate, and is for C++03, but a half-hour effort based on grepping through GCC 4.3.4 includes, ignoring tr1 and ext but including iostream. Crucially, some of these checks might be due to this implementation prefering more defensive coding, and might not be mandated in the Standard and available universally....

  • bitset
    • std::overflow_error - .to_ulong() when too many bits to fit in unsigned long
    • std::out_of_range - operator[]() attempt past end
  • new
    • std::bad_alloc
  • typeinfo
    • std::bad_cast on invalid dynamic_cast attempt
  • ios
    • std::ios_base::failure when using exception masks for error reporting
  • string
    • out_of_range - at/append/assign/insert/erase/replace/copy/substr
    • length_error: attempt to exceed max_size() during reserve or implicit resize (e.g. assign/insert/+= etc.)
  • locale
    • std::bad_cast if locale doesn't contain a facet of type Facet
    • std::runtime_error in various null-pointer/undefined-facet situations
  • deque/vector
    • length_error: attempt reserve() or implicitly-grow > max_size()
    • out_of_range: at()
  • map
    • std::out_of_range: at()
扭转时空 2024-10-11 16:20:52

嗯,我有一本巨大的书,名为C++ 标准,其中包含标准库中所有函数以及它们可以/不能做什么的完整描述。

Well, I have this big, gigantic book titled, The C++ Standard, that contains a complete description of all functions in the standard library and what they can/cannot do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文