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

前端 未结 4 1829
难免孤独
难免孤独 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: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 
    extern "C" void foo() {throw std::exception();}
    

    compiles well

提交回复
热议问题