It has just come to my attention that the C++ standard says that C and C++ functions have different and incompatible types, even if their type signatures are the same (for more info see this question). That means that you technically are not allowed to pass a C++ function to a C function like pthread_create()
.
I am curious if there are any platforms in use where the two ABIs are actually different (aside from the obvious name mangling differences). Specifically, does anyone know of any platforms where this C++ program will fail to compile and run?
#include <assert.h>
extern "C" int run(int (*f)(int), int x) { return f(x); }
int times2(int x) { return x * 2; }
int main(int argc, char *argv[]) {
int a = times2(argc);
// This is undefined behavior according to C++ because I am passing an
// "extern C++" function pointer to an "extern C" function.
int b = run(×2, argc);
assert(a == b);
return a;
}