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
Many C APIs that take function pointer callbacks have a void* parameter for user state. If you've got one of those, you're in luck - you can use an exterm C function that treats the user data as some sort of reference or key to lookup the functor, then execute it.
Otherwise, no.
I would say no, because a C++ functor has an overloaded operator ()
which is a member function, and would thus require a member function pointer. This is a totally different data type than a normal C function pointer, since it cannot be invoked without an instance of the class. You'd need to pass a normal function or a static member function to the C library. Since an overloaded ()
operator can't be static, you can't do it. You'd need to pass the C-library a normal, non-member function or static member function, from which you can then invoke the C++ functor.
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):
template<class T>
int function_wrapper(int a, int b) {
T function_object_instance;
return funcion_object_instance( a, b );
}
This would do for all function that take two ints and return an int.
It depends if this is a static or instance method, if it is static then you can pass through the function as className::functionName, if it is an instance method it is fair more complicated, because you obviously need to tie to a certain instance but can't do it in the same way as you would with delegates in C# etc.
The best way I've found of doing this is to create a holding class which is instantiated with the instance of the object as well as the function pointer, the holding class can then invoke the function directly.
No, of course. The signature of your C function take an argument as function.
void f(void (*func)())
{
func(); // Only void f1(), void F2(), ....
}
All tricks with functors are used by template functions:
template<class Func>
void f (Func func)
{
func(); // Any functor
}
GCC allows you to convert member function pointers to plain function pointers (the first argument of the function called by the plain function pointer then is this
).
Check out the respective link in the manual.
This requires the -Wno-pmf-conversions
flag in order to silence the respective warning for the decidedly non-standard feature. Very convenient for interfacing C style libraries with C++ style programming. When the member function pointer is a constant, this does not even need to generate any code at all: the API would use that argument order anyway.
If you already have a functor, flattening the functor in that manner will likely mean flattening its operator()
, giving you a function that has to be called with a functor class pointer itself as its first argument. Which does not necessarily help all that much but at least has C linkage.
But at least when you are not going through functors this is helpful and provides a no-nonsense C linkage replacement for std::mem_fn
from <functional>
.