Question

I have a function void f (void*, int);, that is used as a callback function. The caller expects void (*)(int). Can I use std::bind1st to convert one to another? Is there any way to do this without using C++ 11 std::bind, just std::bind1st?

Was it helpful?

Solution

No. Although std::bind1st() creates a function object with a call operator taking and int, it is not a void(*)(int). The only way to turn a void(*)(void*, int) into a void(*)(int) is to have a forwarding function which obtains the void* from global resources, e.g.,

static void* data = 0; // probably needs to be set to a more suitable value
void f_forward(int value) {
    f(data, value);
}

Anybody providing a callback which doesn't take a user-defined context, e.g., in the C-like interface a void* which is just passed through, didn't think too hard about the interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top