I'm trying to use a C library in a C++ app and have found my self in the following situation (I know my C, but I'm fairly new to C++). On the C side I have a collection of functions that takes a function pointer as their argument. On the C++ side I have objects with a functor which has the same signature as the function pointer needed by the C function. Is there any way to use the C++ functor as a function pointer to pass to the C function?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
A C callback function written in C++ must be declared as an
extern "C"
function - so using a functor directly is out. You'll need to write some sort of wrapper function to use as that callback and have that wrapper call the functor. Of course, the callback protocol will need to have some way of passing context to the function so it can get to the functor, or the task becomes quite tricky. Most callback schemes have a way to pass context, but I've worked with some brain-dead ones that don't.See this answer for some more details (and look in the comments for anecdotal evidence that the callback must be
extern "C"
and not just a static member function):I found this "gem" using google. Apparently possible but I sure wouldn't recommend it. Direct link to example source code.
Hm, maybe you could write a free template function that wraps around your function-objects. If they all have the same signature, this should work. Like this (not tested):
This would do for all function that take two ints and return an int.
I don't think you can:
operator()
in a function object is really a member function, and C doesn't know anything about those.What you should be able to use are free C++ functions, or static functions of classes.
You cannot directly pass a pointer to a C++ functor object as a function pointer to C code (or even to C++ code).
Additionally, to portably pass a callback to C code it needs to be at least declared as an
extern "C"
non-member function. At least, because some APIs require specific function call conventions and thus additional declaration modifiers.In many environments C and C++ have the same calling conventions and differ only in name mangling, so any global function or static member will work. But you still need to wrap the call to
operator()
in a normal function.If your functor has no state (it is an object just to satisfy some formal requirements etc):
you can write a normal extern "C" function which creates the functor and executes its operator().
If your functor has state, eg:
and the C callback function takes some kind of user state parameter (usually void *):
you can write a normal extern "C" function which casts its state parameter to your functor object:
and use it like this:
Otherwise the only normal way to do it (normal as in "without generating machine code at runtime" etc.) is to use some static (global) or thread local storage to pass the functor to an extern "C" function. This limits what you can do with your code and is ugly but will work.
No, of course. The signature of your C function take an argument as function.
All tricks with functors are used by template functions: