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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.