我可以抛出从传统的C回调C ++异常? [英] Can I throw C++ exceptions from legacy C callbacks?

查看:101
本文介绍了我可以抛出从传统的C回调C ++异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用了一些C库C ++ code。 C库需要C语言的回调。我在C ++ code写的回调,现在我以某种方式需要从它报告错误(但它返回void)。我不知道是否可以从由C ++ code使用一个C回调抛出一个异常?

I have C++ code that uses some C libraries. The C libraries take C language callbacks. I wrote a callback in my C++ code and now I somehow need to report error from it (but it returns void). I wonder if I can throw an exception from a C callback that is used from C++ code?

这是我很难理解。

谢谢,博大Cydo。

推荐答案

是的,你应该能够做到这一点。但是,请记住这是完全有可能的C库未写入异常是安全的,并因此你可能会留下一些该库的结构在一些不一致的状态。这完全取决于具体的图书馆和特定的回调函数。如果不出意外,该库将可能不得不使用C ++考虑支持从一开始就认为回调写的。

Yes, you should be able to do so. However, keep in mind it's entirely likely that the C library was not written to be exception safe, and as a result you might leave some of that library's structures in some inconsistent state. It depends entirely on the specific library and on the specific callback function. If nothing else, that library would probably have to be written with C++ support in mind from the beginning with respect to that callback.

这篇关于我可以抛出从传统的C回调C ++异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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