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

查看:170
本文介绍了没有例外,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天全站免登陆