[英]Can std::bind1st be used to convert void (*)(void*,int) to void (*)(int)?
I have a function void f (void*, int);
我有一个函数
void f (void*, int);
, that is used as a callback function. ,用作回调函数。 The caller expects
void (*)(int)
. 调用者期望
void (*)(int)
。 Can I use std::bind1st
to convert one to another? 我可以使用
std::bind1st
将一个转换为另一个吗? Is there any way to do this without using C++ 11 std::bind
, just std::bind1st
? 有没有办法在不使用C ++ 11
std::bind
情况下执行此操作,只需要std::bind1st
?
No. Although std::bind1st()
creates a function object with a call operator taking and int
, it is not a void(*)(int)
. std::bind1st()
。虽然std::bind1st()
使用调用操作符take和int
创建一个函数对象,但它不是 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, 将
void(*)(void*, int)
转换为void(*)(int)
的唯一方法是使用转发函数从全局资源中获取void*
,例如,
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. 任何提供不采用用户定义的上下文的回调的人,例如,在类似C的界面中,只是通过的
void*
,并没有想到界面太难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.