C ++标准是否要求C链接函数为"noexcept"? [英] Does the C++ standard mandate that C-linkage functions are `noexcept`?

查看:95
本文介绍了C ++标准是否要求C链接函数为"noexcept"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到标准中强制用extern "C"声明的函数为noexcept的隐式或显式内容.

I can't find anything in the standard that forces functions declared with extern "C" to be noexcept, either implicitly or explicitly.

但是,应该清楚的是C调用约定不能支持异常……是吗?

Yet, it should be clear that C calling conventions cannot support exceptions... or is it?

标准是在我错过的某个地方提到这一点吗?如果没有,为什么不呢?是否只是将其保留为各种实现细节?

Does the standard mention this, somewhere that I've missed? If not, why not? Is it simply left as an implementation detail of sorts?

推荐答案

据我所知,不能保证用"C"链接定义的函数不会引发异常.该标准允许C ++程序调用具有"C"语言链接的外部函数,并定义以C ++编写的具有"C"语言链接的函数.因此,没有什么可以阻止C ++程序调用实际上用C ++编写的具有"C"语言链接的函数(也许在另一个编译单元中,尽管这不是必需的).这将是一件奇怪的事情,但很难排除.另外,我也看不出该标准在什么地方说明这样做会导致未定义的行为(实际上,由于该标准无法定义不是用C ++编写的函数的行为,因此这是 only 用法)没有正式未定义的行为).

As far as I can tell there is no guarantee that function defined with "C" linkage will not throw exceptions. The standard allows a C++ program both to call an external function with "C" language linkage, and to define functions written in C++ that have "C" language linkage. Therefore there is nothing to prevent a C++ program from calling a function with "C" language linkage that is actually written in C++ (in another compilation unit maybe, although even this is not necessary). It would be a strange thing to do, but it is hard to rule out. Also I don't see where in the standard it says that doing so would lead to undefined behavior (in fact since the Standard cannot define the bahavior of function not written in C++, this would be the only usage where there is not formally undefined behavior).

因此,我认为假设"C"链接暗含noexcept.

As a consequence I think it would be an error to assume that "C" linkage implies noexcept.

这篇关于C ++标准是否要求C链接函数为"noexcept"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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