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

2019-04-03 04:32发布

问题:

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; }
}

回答1:

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



回答2:

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.



回答3:

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



回答4:

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? :))



标签: c++ extern-c