未定义的行为会在定义为永不引发异常的函数中引起异常吗? [英] Can undefined behaviour cause an exception in a function defined as never throwing an exception?

查看:80
本文介绍了未定义的行为会在定义为永不引发异常的函数中引起异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 delete的描述[] 表示:

I was looking at a description of delete[] and it says:


异常安全性

无掷保证:此函数从不抛出异常。

Exception safety
No-throw guarantee: this function never throws exceptions.

但是它增加了:


请注意, ptr 的无效值会导致未定义的行为。

Notice that an invalid value of ptr causes undefined behavior.

我怀疑UB可以包含引发异常,并且保证仅对有效指针有效,但是我只是想检查一下。

I suspect that UB can include throwing an exception and the guarantee is only good for valid pointers, but I just thought I'd check.

推荐答案

未定义行为表示语言定义不会告诉您代码所执行的操作。因此,是的,任何事情都可能发生,包括跳过 noexcept 说明符。毕竟,没有规则……(好吧,您的编译器可能会提供规则,但是依靠它是不可移植的)。

"Undefined behavior" means that the language definition does not tell you what the code does. So, yes, anything can happen, including blowing past a noexcept specifier. After all, there are no rules... (well, your compiler may provide rules, but relying on that is not portable).

这篇关于未定义的行为会在定义为永不引发异常的函数中引起异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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