Can C++ functions marked as Extern “C” throw?

2019-04-03 04:46发布

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.

It would look something like this:

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

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

标签: c++ extern-c
4条回答
一夜七次
2楼-- · 2019-04-03 05:09

For GCC the answer seems inconclusive.

The MSVC documentation, however is relatively clear on the subject:

  • /EHa and /EHs ... tells the compiler to assume that functions declared as extern "C" may throw an exception.
  • /EHsc ... tells the compiler to assume that functions declared as extern "C" never throw a C++ exception

So for Visual-C++ it depends on the compiler options whether you get defined behavior.

查看更多
forever°为你锁心
3楼-- · 2019-04-03 05:12

"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.

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.

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

查看更多
祖国的老花朵
4楼-- · 2019-04-03 05:14

it will compile but it is undefined behavior to throw from function marked as having C linkage. C doesn't have exceptions, therefore in general you should just return an error code and/or provide a function that returns the information about the last error.

#include <exception>
extern "C" void foo() {throw std::exception();}

compiles well

查看更多
姐就是有狂的资本
5楼-- · 2019-04-03 05:26

Here is answer for your question: http://yosefk.com/c++fqa/mixing.html#fqa-32.6

Basically you won't be able to catch it. (but why you won't just compile it and try? :))

查看更多
登录 后发表回答