Passing state to function pointer

Woofmao

I am using a function in an external library that has this type:

int libraryFunction(int* fp(int))

However, I need the callback to also be passed an ostream, and the library function does not have any overloads that allow functors, lambdas, or passing extra arguments. Is there a way to get the ostream into the callback?

Guillaume Gris

If the library API does not support function object or passing a custom state pointer, then you will have to store the state of your object (here an ostream) on your side.

The function being stateless, there is no other choice than storing the state or a pointer to the state at a compile time known location.

The pitfall with this is that you have to ensure the lifetime of the state matches the lifetime of the callback in the library. The ostream must not be destroyed to early.

Another problem with this is that unless you can pass some kind of index to your function, you will have to declare a separate function for each state you need.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related