问题描述
在C ++中,安全/可移植使用静态成员函数指针为C API回调吗?是一个静态成员函数的ABI与C函数相同?
根据C ++标准是不安全的。如中所述:
根据在该答案中提出的意见,存在真实问题,试图
使用C ABI回调 externC
。
编辑:从标准中添加一些支持引号(强调我的):
3.5 程序和链接:
:
所以如果回调的代码使用C语言绑定的回调, C ++程序)也必须如此。
In C++, is it safe/portable to use static member function pointer for C API callbacks? Is the ABI of a static member function the same as a C function?
It is not safe per the C++ standard. As stated in this SO posting:
And according to comments made by Martin York in that answer there are real-world problems trying to do so on some platforms.
Make your C ABI callbacks extern "C"
.
Edit: Adding some supporting quotes from the standard (emphasis mine):
3.5 "Program and linkage":
And
7.5 "Linkage specifications":
So if the code making the callback is using C language bindings for the callback, then the callback target (in the C++ program) must as well.
这篇关于在C ++中,它是安全/可移植的使用静态成员函数指针为C API回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!