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

前端 未结 4 1811
难免孤独
难免孤独 2020-12-30 22:52

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

相关标签:
4条回答
  • 2020-12-30 23:17

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

    0 讨论(0)
  • 2020-12-30 23:22

    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.

    0 讨论(0)
  • 2020-12-30 23:24

    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

    0 讨论(0)
  • 2020-12-30 23:35

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

    0 讨论(0)
提交回复
热议问题