简体   繁体   中英

Can std::bind1st be used to convert void (*)(void*,int) to void (*)(int)?

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 ?

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, eg,

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, eg, in the C-like interface a void* which is just passed through, didn't think too hard about the interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM