LLVM如何使用C ++的标准容器没有例外? [英] How can LLVM use C++'s standard containers without exceptions?

查看:294
本文介绍了LLVM如何使用C ++的标准容器没有例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 LLVM编码标准 ,LLVM不使用[...]异常。然而,LLVM确实使用C ++的标准容器,例如 std :: vector

According to the LLVM Coding Standards, "LLVM does not use [...] exceptions". However, LLVM does make use of C++'s standard containers, such as std::vector.

为LLVM使用标准容器没有例外?如何处理容器通常 throw 的情况?例如,如果 std :: vector :: push_back 无法分配内存,并且不能 throw std :: bad_alloc ,会发生什么?

How is it possible for LLVM to use the standard containers without exceptions? How does it handle a situation in which a container would normally throw? For example, what happens if std::vector::push_back cannot allocate memory and cannot throw std::bad_alloc?

推荐答案

LLVM处理达到的状态,会立即崩溃时抛出异常。如果实现/编译设置使用启用异常,则抛出一个异常,并找到没有catch处理程序并调用 std :: terminate 。如果实现/编译设置禁用例外,实现必须提供一些替代行为。大多数会以某种方式立即崩溃。

LLVM treats reaching a state which would throw an exception as an immediate crash. If the implementation / compilation settings used enable exceptions, one is thrown unwinds and finds no catch handler and calls std::terminate. If the implementation / compilation settings disable exceptions, the implementation must provide some alternative behavior. Most will crash immediately in one way or another.

LLVM上的开发人员用这些设置测试他们的代码,并小心避免可能引发的情况。

Developers on LLVM test their code with exactly these settings and are careful to avoid circumstances that could potentially throw.

一种不可能直接避免的情况是分配失败。 LLVM简单地不支持分配可能失败的平台,用户必须捕获 bad_alloc 。如果平台在任何时候都无法分配内存,LLVM就会崩溃。

One circumstance which is impossible to avoid directly is allocation failures. LLVM simply does not support platforms where allocations may fail and the user have to catch bad_alloc. If the platform fails to allocate memory at any point, LLVM will crash.

事实证明,目前绝大多数非嵌入式平台使用某种形式的 overcommit 。并且由于LLVM如何设计的性质,我们没有特别有用的机制来适当地响应分配内存的失败。因此,它被认为是一个致命的和不可恢复的错误,无论我们是否启用异常,我们将在那时终止该过程。

It turns out that the vast majority of non-embedded platforms today use some form of overcommit. And due to the nature of how LLVM is engineered, we have no particularly useful mechanism available to respond gracefully to a failure to allocate memory. As a consequence, it is considered a fatal and irrecoverable error, and whether we enable exceptions or not, we're going to terminate the process at that point.

这篇关于LLVM如何使用C ++的标准容器没有例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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