Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

In C++, is it safe/portable to use static member function pointer for C API callbacks?

0
Posted

In C++, is it safe/portable to use static member function pointer for C API callbacks?

0

It is not safe per the C++ standard. As stated in this SO posting: A C callback function implemented in C++ must be extern “C”. It may seem to work as a static function in a class because class-static functions often use the same calling convention as a C function. However, doing that is a bug waiting to happen (see comments below), so please don’t – go through an extern “C” wrapper instead. 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”.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123