是否可以将C ++函数标记为Extern"C"?扔? [英] Can C++ functions marked as Extern "C" throw?

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

问题描述

我有要使用extern "C"声明的C ++函数,即使它们仅在C ++代码中调用.是的,我知道这很奇怪,但是我想这样做是为了保持一致性,因为我们混合使用了C和C ++声明.我只想确保将C ++函数声明为extern "C"不会影响抛出的行为.

I've got C++ functions that I want to declare using extern "C" even though they are only called in C++ code. Yes, I know this is strange but it's something I would like to do for consistency since we have mixed C and C++ declarations. I just want to make sure that declaring a C++ function as extern "C" won't affect the behavior of throwing.

它看起来像这样:

extern "C" void foo() {throw exception;}

int bar()
{
    try
    {
        foo();
    } catch (exception e) { return 1; }
}

推荐答案

"C ++函数是否可以标记为Extern"C"?"

"Can C++ functions marked as Extern "C" throw?"

,从某种意义上说,语言和编译器都不会阻止您这样做.

Yes, in the sense that neither the language nor the compiler will prevent you from doing so.

,从某种意义上讲,如果抛出,则将是不确定的行为,因为C ++异常会跨越语言边界.

No, in the sense that if you throw, it would be an undefined behaviour, as the C++ exception crosses language boundaries.

在实践中:不要这样做.捕获异常并将其转换为错误代码,或者将其翻译为其他语言可以理解的方式.

In practice: do not do it. Catch the exception and translate it into an error code, or a means the other language can understand.

因此,最重要的是:请勿引发标记为extern "C"的函数的异常.

So the bottomline is: do NOT throw exception from functions marked as extern "C".

这篇关于是否可以将C ++函数标记为Extern"C"?扔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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